A quick primer to the vi editor

It's useful for all computational chemists to learn how to use vi since it can be accessed on any machine that can support XWindows.

To open a text file using vi, type:
       vi  myfile
If myfile is not a pre-existing file, vi opens up a new file.

The most important thing to remember is whether you are in Insert mode or not. Whenever you want to type new text in, press
i to get into insert mode. Once you've finished typing whatever you want, press the Esc key to get out of insert mode. (You can also use a to get into insert mode at the end of a line.)

When you're not in Insert mode, useful keys are:
    up and down arrow keys to move up and down lines
   
Ctrl-F and Ctrl-B to move forward and backward by a page
   
/ followed by some text to search for that text in your file (type n to jump to the next instance of the searched text)
   
Ctrl-G to remind yourself which file you're in and roughly where you are in the file
    dd to delete a single line, or type a number beforehand to specify the number of lines to delete
             (e.g.
16dd will delete 16 lines starting with the one you're on)
    x to delete a single character
    Y to yank a single line, or type a number beforehand to specify the number of lines to yank (essentially to copy)
             or
yG to yank to the end of the file.
    p to paste the yanked text below the current cursor position or P to paste above.

There are a variety of other commands that can be accessed by first typing a colon followed by other keys.
   
:1 to go to line 1 (you can use any number to go to that line number)
   
:$ to go to the last line
   
:w to write, i.e., save whatever changes you have made
   
:q to quit and exit the vi program
   
:q! to quit without saving any changes you made (for example if you messed up the file)

Other useful commands
    :%s/search_string/replacement_string/g to make a global replacement of search_string with replacement_string
    :e filename to read in a new file to paste your yanked lines (to get around the 50 line paste limit)

A quick web search will give you plenty of vi references and manuals.

Get some practice:
On saber3,
Copy the template.inp file from ~jkua/example/ into your current directory and rename it test.inp
Try out some of the commands above.
In particular, try:
• deleting the line (that reads Delete this line)
• pasting in all lines that begin with C from ~jkua/examples/ethanol.inp (try to do this with cut and paste - it's easier with two terminal windows open)
• changing the word Optimization to SP
• changing the value in the line SCF_CONVERGENCE from 8 to 5
• adding a new line under this that reads SCF_GUESS = READ
• adding a new line under this that reads SOLVENT_METHOD = SMD
• adding three new lines to the bottom of the file that read
$smx
SOLVENT water
$end

• quitting vi without saving the changes, and then reopening the file
• writing/saving the new file and quitting vi

Copy the myfile.run file from ~jkua/example/ into your current directory and rename it test.run
Using the search and replace, try to replace all instances of "myfile" with "test" within the file.

Take a look at the file ~jkua/example/ethanol.out (you might have copied it to your home directory already in the Unix tutorial.)
• Go to the bottom of the file and work your way backwards with Ctrl-B until you see the text "OPTIMIZATION CONVERGED". A few lines above it you will see a line that reads Final Energy is -155.08851.
• Go back to the top of the file and search for "Total energy". How many instances are there?
• Quit vi, and try to grep the lines that gave you the total and final energies so you can see the values without opening the file with vi.