Making Formatted Documents in Vim
Disclosure: Catbirdlinux.com is a participant in the Amazon Services LLC Associates Program and others, such that this site earns advertising fees by linking to commercial offers. If you make a qualifying purchase after clicking a link on this website, the associate affiliated with this site may earn a comission at no cost to you.

Reading the various internet forums about Vim and Neovim, I found an interesting question posed by a fairly new user of Vim. He was accustomed to other editors, which open up and show a full screen but page immediately. "I can see my line numbers, why don't they automatically go to 100 instead of just a few lines?"

I chuckled a little, as did a few others in the forum, reminding the newbie that the other lines don't exist until some content is entered. He persisted, seeming to want Vim to open a new file, and for the file to have a blank first line and a sequence of 99 more newlines. Instead of dropping interest in the issue, and moving on to other topics, I decided to stick around try to write a Vim function to create the file that newbie desired.

Consider first the desired outcome, then ponder a logical way to bring it about:

  1. Open a buffer (a new document in Vim).
  2. Add 99 additional new lines.
  3. Move the cursor back to the left side of the top line.

Here is a Vim function, which runs when vim is opened. It will have finished its work before the document renders on the user's screen.

" create a 100 line document
fun! Newpage()
    let i = 0
    while i <= 98
        put=''
        let i += 1
        normal gg
    endwhile
endfun
com! NP call Newpage()
autocmd VimEnter * NP

Some other responses came in, there was some nice compact Vim code posted, which I used in an updated function:

" create a 100 line document
fun! Newpage()
    normal 99o
    normal gg
endfun
com! NP call Newpage()
autocmd VimEnter * NP

Well it sort of works, but it is indiscriminate in adding those new lines. Whoah! It is actually pretty bad, altering everything! No, that function should not run automatically. Instead, it should be a function available at the user's discretion, called on command. Remove the line starting with "autocmd" and use :NP to manually execute the function

There is another solution, which seems to work better and is more flexible. Do the file creation in a bash script, then open it in Vim. Being more familiar with Bash, I knew ways to not only create the file, but also enter a timestamp and other date. Below, I will show a creation of that blank 100 line text file, then show a similar script which instead creates a blank form with a timestamp included.

#!/bin/bash

# create a blank file
touch newfile.txt

# use a loop for adding newlines
i=0
while [  -le 99 ]
do echo '' >> newfile.txt
((i++))
done

# edit the file in Vim
vim newfile.txt

Let us try something more complex. Enter a timestamp, add some questions for a user to fill in.

#!/bin/bash

TIME=$(date +%Y-%m-%d_%H-%M)
SPACER="

"

# create a blank file
touch newfile.txt

echo -e "Journal Entry  Type of workout: 
Use your own words to describe elements of your session. " >> journal_.txt
echo -e "Hours of sleep prior: " >> journal_.txt
echo -e "Attitude and energy: " >> journal_.txt
echo -e "Stretching and warm up: " >> journal_.txt 
echo -e "Describe the sets accomplished, repetitions, etc. " >> journal_.txt 
echo -e "Describe any injuries or pain experienced. " >> journal_.txt 
echo -e "Where is your best progress? " >> journal_.txt 
echo -e "What needs more work - more weight, more reps, different exerceses? " >> journal_.txt 
echo -e "Post workout recuperation: nutrition, rest, stretching, etc: " >> journal_.txt

# edit the file in Vim
vim journal_.txt

The above is just an example of what can be done by using a scripting language for creating documents and editing them in Vim. In addition to Bash, one could just as easily use Python, Go, Ruby, Java, or another language to build the document. For that matter, Vimscript could be used as well, implemented as a plugin. Dream it up and put it into code, friends. The sky is the limit.



Tags: Vim, formatted documents, document templates
©2020-2021 Catbirdlinux.com, All Rights Reserved.
Contact Us, Privacy Policy and Affiliate Disclosure, XML Sitemap.