A Modest vi Tutorial

1 Starting vi

vi is normally started from a Unix shell, e.g. csh or bash, simply by giving the command vi. Usually, a file name is also specified on the command line. The file does not have to exist. Thus, to start editing a file called x.txt, you issue the command

% vi x.txt
Your terminal should then look as follows:
                                                                                
~
~
~
~
~
~
~
~
~
~
~
"x.txt" [New File]

2 vi modes explained

After starting vi, you are in command mode. In this mode, vi does not insert characters you type on your keyboard into the text being edited. Instead, characters are interpreted as commands, so when you press a key, you won't see the corresponding character appear. Nonetheless, vi responds to every keypress, although keypresses may not have any immediately visible effects.

Command mode is the only mode in which you can move the cursor within your text to any position on the screen, by simply using the cursor (arrow) keys. In other modes, the cursor keys may not work as expected, and it's best not to use them in modes other than command mode.

In addition to command mode, vi has two other modes, called insert mode and command line mode. Insert mode will be introduced in the following sections, while command line mode will just briefly be mentioned at the end of this tutorial.

3 Getting from any mode to command mode

If you are uncertain in which state vi is, e.g. if you have pressed some keys and the result did not match your expectation, press the Escape key several times. This brings vi back to command mode. You may hear some beeps, but no harm can be done to the stuff you're editing. Some keyboards manufactured by Digital have no Escape key. On these, you have to use the F11 key.

You should switch back to command mode immediately whenever you have any feeling that things are not completely as you think they should be. vi's command mode is the default state from which all other features should be ventured.

4 Inserting text

The mode for inserting stuff into your text is called insert mode. You may switch to insert mode by pressing i while in command mode. In insert mode, all characters you type are inserted into the text you're editing, until you press Escape (or F11) to switch over to command mode. Thus, after starting vi with a new (i.e. nonexistent) file, as shown in section 1, you can enter some lines of text. After doing so, your screen might look like this:
I close my eyes                                                                 
Inly for a moment, and the moment's gone.
All my dreams
Pass before my eyes, a curiositz
Dust in the wind --
All they are is dust in the wind

composed by Ludwig van Beethoven
~
~
~
~

Don't forget to press Escape to return to command mode when you've finished inserting text. Otherwise, you'll end up inserting characters that were intended as commands for vi. If that happens (it surely will), press Escape and delete these bad characters, as shown in the next section.

5 Deleting text

While you're in insert mode, vi only lets you cancel characters in the current line. Once you press return, you cannot go back to the preceding line with the Backspace key. Generally, deleting characters is done by switching to command mode, positioning the cursor on the character to be deleted, and pressing x. In the example given in section 4, you could thus place the cursor on the "I" in the word "Inly" and then press x, and likewise, you can delete the "z" in "curiositz". After successfully doing this, your screen should look like:
I close my eyes                                                                 
nly for a moment, and the moment's gone.
All my dreams
Pass before my eyes, a curiosit
Dust in the wind --
All they are is dust in the wind

~
~
~
~
~

x allows you to delete single characters, but you cannot get rid of lines with it. To get rid of an entire line, e.g. the erroneous "composed by Ludwig van Beethoven" above, go to command mode, place the cursor anywhere in the line, and press dd (that is, two times the letter d, notice that this is lowercase).

6 Prepending and appending to lines

Continuing the example started in section 4, after getting rid of the misspelled letters, you may now want to add the correct ones. Getting the word "Only" completed in the second line is possible by moving the cursor to the "n" in "nly", then pressing i, as described in section 4, inserting the "O", and finally pressing Escape to get back to command mode. Notice that the insertion takes place just before the character where the cursor was located when you pressed i.

This property makes it impossible to use the same method for adding an "y" to the truncated word "curiosit", because there is no letter following the "t" on which you could place the cursor. In these situations, you can use a instead of i. Pressing a in command mode switches to insert mode just as i, with the only difference that the insertion is appended, i.e. it takes place behind the current cursor position. Thus, placing the cursor on the "t" in "curiosit", pressing a, inseting the missing "y", and pressing Escape to return to command mode completes the word "curiosity", as desired.

Once you switched to insert mode, either with a or with i, you can, of course, also add new lines by pressing the Return key. The little example given here can thus be completed by a title and a correct composer line. For adding the title, move the cursor to the first character in the first line, press i, then press Return (to insert a new line). Now, your insert cursor is in the second line, beneath the empty line you created by pressing Return. To insert into the first line, switch back to command mode (press Escape), move the cursor into the first line, press i to go into insert mode, and insert the title, "Dust in the Wind". Before returning to command mode, you can press Return to insert a blank line (just for a more appealing layout). When done with this, add the correct composer info by moving to the last line (which is blank), press a, and create a new line by pressing Return before typing "composed by Kansas". The final text should now look like this:
Dust in the Wind

I close my eyes                                                                 
Only for a moment, and the moment's gone.
All my dreams
Pass before my eyes, a curiosity
Dust in the wind --
All they are is dust in the wind

composed by Kansas
~
~

7 Exiting

Now that you worked your way throug this tutorial, you probably want to save your work. You save your file and exit the vi session by pressing ZZ (that is, two times the uppercase letter Z) while in command mode. However, if you want to quit from vi without saving, you may do so by typing :q!, followed by return. Typing : switches from command mode to command line mode. vi indicates this by displaying a command line at the bottom of the screen with : (a colon) as a prompt. Thus, just before pressing the return key, your terminal screen should look like this (when nothing was entered):
                                                                                
~
~
~
~
~
~
~
~
~
~
~
:q!
All entries into the command line have to be finished by pressing return. If you decide not to give a command and you already have typed:, press Control-U, then return. After completing a command line, vi always is in command mode.

8 Getting more info

This little tutorial just introduces the most basic functions of vi. After working through the example presented here, you're hopefully able to write notes with vi, or to use vi to make small changes to existing ASCII files. However, this is (of course) not all there is about vi. You can check out this list of vi essentials which I put together for some course a while ago. Also, have a look at the section on using the vi editor in the UNIXhelp system. And finally, there's plenty of information on vi out there in the WWW.


Jan T. Kim, kim@inb.uni-luebeck.de