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[edit | edit source]
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[edit | edit source]
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[edit | edit source]
1. Type the sentence Good morning Doctor, how are you today?. Delete "Good morning".
2. Now using the single-repeat operation, delete "how are".
Motion[edit | edit source]
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 :
- 0 moves to the immediate beginning of the line
- $ moves to the immediate end of the line
- ^ moves to the first non-whitespace character of the line
^ acts in the following way, if the line was
hello how are you
and your cursor is on the u, if you would enter ^, the cursor would be upon the h.
Furthermore, the / 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 /, 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 ? command. To repeat either search, enter //, ??, or alternatively, type / or ? and hit Enter. You can also press n to jump to the next occurrence, and N to jump to the previous occurrence.
Commands and motion[edit | edit source]
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 dw for each word from the cursor position to the end of the line, or hold down x, 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 d$.
- dt; will delete until the next semicolon (This is helpful in languages like C and perl that use semicolons to finish statements).
- d2} to delete the next two paragraphs.
- d4b to delete the previous four words (alternatively, you could enter 4b4dw).