vi (pronounced "vee eye") is an editor you can find at almost every Unix installation. It is initially somewhat hard to get used to, but it has many powerful features. The aim of this tutorial is to get you started using the vi editor. This tutorial assumes no vi experience, so you will first be exposed to the ten most basic commands. These fundamental commands are enough to perform the bulk of your editing needs, and you can expand your vi vocabulary as needed.
To invoke vi, type the letters vi followed by the name of the file you wish to create. You will see a screen with a column of tildes along the left side. vi is now in command mode. Anything you type will be understood as a command, not as text to be input. In order to input text, you must type a command. The two basic input commands are the following:
Since you are at the beginning of an empty file, it doesn't matter which of these you type. Type one of them, and then type in the following text (a poem by Augustus DeMorgan found in The Unix Programming Environment by B.W. Kernighan and R. Pike):
Great fleas have little fleas<Enter>
upon their backs to bite 'em,<Enter>
And little fleas have lesser fleas<Enter>
and so ad infinitum.<Enter>
And the great fleas themselves, in turn,<Enter>
have greater fleas to go on;<Enter>
While these again have greater still,<Enter>
and greater still, and so on.<Enter>
Note that you press the Esc key to end insertion and return to command mode.
These commands may be repeated by holding the key down. Try moving around in your text now. If you attempt an impossible movement, e.g. pressing the letter k when the cursor is on the top line, the screen will flash, or the terminal will beep. Here are more movement commands.
To move the cursor a number of positions left.
The commands that deal with lines use the modifier to refer to line numbers. The G is a good example.
has a large set of commands which can be used to move the cursor around the file. Single character movement through to direct line placement of the cursor. vi can also place the cursor at a selected line from the command line.
This command opens the file myfile.tex and places the cursor 10 lines down from the start of the file.
Try out some of the commands in this section. Very few people can remember all of them in one session. Most users use only a subset of the above commands.
The aim is to change the contents of the file and vi offers a very large set of commands to help in this process. This section will focus on adding text, changing the existing text and deleting the text. At the end of this section you will have the knowledge to create any text file desired.
When entering text, multiple lines can be entered by using the RETURN key. If a typing mistake needs to be corrected and you are on the entering text on the line in question. You can use the BACKSPACE key to move the cursor over the text. The different implementations of vi behave differently. Some just move the cursor back and the text can still be viewed and accepted. Others will remove the text as you backspace. If the text is visable and you use the ESC key when on the line you have backspaced on the text after the cursor will be cleared. Use your editor to become accustomed to its behaviour.
vi has a small set of delete commands which can be enhanced with the use of command modifiers.
On occasions you may need to undo the changes. The following commands restore the text after changes.
not only allows you to undo changes, it can reverse the undo. Using the command 5dd delete 5 lines then restore the lines with u . The changes can be restored by the u again.
vi has a number of search commands. You can search for individual characters through to regular expressions.
The main two character based search commands are f and t.
If the character you were searching for was not found, vi will beep or give some other sort of signal.
vi allows you to search for a string in the edit buffer.
When using the / or ? commands a line will be cleared along the bottom of the screen. You enter the search string followed by RETURN.
The search and replace command allows regular expression to be used over a range of lines and replace the matching string. The user can ask for confirmation before the substitution is performed. It may be well worth a review of line number representation in the ed tutorial.
The search command is very powerful when combined with the regular expression search strings. If the g directive is not included then the change is performed only on the first occurrence of a match on each line.
Make sure you are in command mode by pressing the Esc key. Now type :w. This will save your work by writing it to a disk file.
The command for quitting vi is q. If you wish to combine saving and quitting, just type :wq. There is also a convenient abbreviation for :wq - ZZ. Since much of your programming work will consist of running a program, encountering a problem, calling up the program in the editor to make a small change, and then exiting from the editor to run the program again, ZZ will be a command you use often. (Actually, ZZ is not an exact synonym for :wq - if you have not made any changes to the file you are editing since the last save, ZZ will just exit from the editor whereas :wq will [redundantly] save before exiting.)
If you just want to start all over again, you can type :q! (remember to press the Esc key first). If you omit the !, vi will not allow you to quit without saving.
vi offers powerful commands that many more modern editors do not or can not offer. The cost for this power is also the main argument against vi; the commands can be difficult to learn and read. With a little practice and time, the vi command set will become second nature.