Learning the vi Editor/Making your work easier

By now you should know the rudiments of using vi. However, to really make vi work for you, it may be helpful to know the following to make your work easier for you.

More on commands
Say you are editing a document, and you wish to delete ten lines - as of now, the only way to do this is to enter dd ten times. Or if you want to delete seven characters exactly - you would have to enter x seven times. There must be a better way!

Repetition
Fortunately, vi lets you augment most of the commands in case you want to repeat that command a certain number of times. This is done by typing in the number of times you want that command repeated, followed by the command.

So, if you want to delete ten lines, you would type 10dd. Or if you want to delete seven characters, you would type 7x.

You can also repeat the last action done by typing . (this is a single period keystroke), the single-repeat operation over the location you want to repeat the previous operation.

So if you wanted to repeat the deletion of ten lines as in the previous example, you could repeatedly press . to perform this operation over and over again.

Exercise
1. Type the sentence <tt>Good morning Doctor, how are you today?</tt>. Delete "Good morning".

2. Now using the single-repeat operation, delete "how are".

Motion
vi allows you greater flexibility over motion as well. There are a few commands to allow you to quickly jump around your document, such as :
 * <tt>0</tt> moves to the immediate beginning of the line
 * <tt>$</tt> moves to the immediate end of the line
 * <tt>^</tt> moves to the first non-whitespace character of the line

<tt>^</tt> acts in the following way, if the line was hello how are yo<tt>u</tt> and your cursor is on the u, if you would enter <tt>^</tt>, the cursor would be upon the h.

Furthermore, the <tt>/</tt> command allows you to jump directly to some pattern in the file. For example, if you're looking for the next occurrence of the word "pomegranate" in your text, if you hit <tt>/</tt>, then type in pomegranate (you need not enter insert mode) and hit enter, the cursor will jump to the next occurrence of the word, if it exists. If you want to search backwards, you would perform the same procedure, but use the <tt>?</tt> command. To repeat either search, enter <tt>//</tt>, <tt>??</tt>, or alternatively, type <tt>/</tt> or <tt>?</tt> and hit Enter. You can also press <tt>n</tt> to jump to the next occurrence, and <tt>N</tt> to jump to the previous occurrence.

Commands and motion
We know now that vi lets you enter a number to specify how many times to do something. Consider this example now: you want to delete everything after a certain point on a line - you could enter <tt>dw</tt> for each word from the cursor position to the end of the line, or hold down <tt>x</tt>, but these are cumbersome examples. vi thankfully lets you do something much faster.

With certain commands, vi allows you to specify a position, using the methods in the previous sections. The position is specified after the command. For example, to delete up to the end of the line, you would enter <tt>d$</tt>.

Other examples:
 * <tt>dt;</tt> will delete until the next semicolon (This is helpful in languages like C and perl that use semicolons to finish statements).
 * <tt>d2}</tt> to delete the next two paragraphs.
 * <tt>d4b</tt> to delete the previous four words (alternatively, you could enter <tt>4b4dw</tt>).