Introduction to the Vi Text Editor

Norman Matloff

November 15, 1997

A text editor is a program that can be used to create and modify text files. One of the most popular editors on Unix systems is vi. (Note: I suggest that you use the enhanced vi clone vim, instead of ordinary vi. Click here for information on vim.)

As a brief introduction to vi, go through the following: First, type

        vi x

at the Unix prompt. Assuming you did not already have a file named x, this command will create one. (If you have tried this example before, x will already exist, and vi will work on it. If you wish to start the example from scratch, simply remove x first.)

The file will of course initially be empty. To put something in it, type the letter `i' (it stands for ``insert-text mode''), and type the following (including carriage returns at the end of each of the three lines):

   The quick
   brown
   fox will return.

Then hit the Escape key, to end insert-text-mode.1 Now save the file and exit vi, by typing `ZZ' (note the capitals).

The key to learning vi is to keep in mind always the difference between insert-text mode and command mode. In the latter mode, as its name implies, one issues commands, such as the ZZ above, which we issued to save the file and exit vi. The characters you type will appear on the screen if you are in insert-text mode, whereas they will not appear on the screen while you are in command mode. By far the most frequent problem new vi users have is that they forget they are in insert-text mode, and so their commands are not obeyed.

For example, suppose a new user wants to type ZZ, to save the file and exit vi, but he has forgotten to hit the Escape key to terminate insert-text mode. Then the ZZ will appear on the screen, and will become part of the text of the file--and the ZZ command will not be obeyed.

You now have a file named x. You can check its contents by typing

   more x

which will yield

   The quick
   brown                                 
   fox will return.

just as expected.

Now let's see how we can use vi again to modify that file. Type

   vi x

again, and make the following changes.

First, let's say the fox will not return: Type `/re' and hit the Return key, which instructs vi to move the cursor to the first instance of `re' relative to the current cursor position. (Note that typing only `/r' would have moved the cursor to the `r' in `brown', which is not what we want.)

Now use the `i' command again: Hit `i', then type `not ' (note the space), and then hit Escape.

Next, let's delete the word `brown'. Type `/b' to move the cursor there, and then hit `x' five times, to delete each of the five letters in `brown'. (This will still leave us with a blank line. If we did not want this, we could have used the `dd' command, which would have deleted the entire line.)

Now type `ZZ' to save the file and exit vi. Use `more' again to convince yourself that you did indeed modify the file.

You now know how to use vi to insert text, move the cursor to text, and delete text. Technically, the bare-bones set of commands introduced above is sufficient for any use of vi. However, if you limit yourself to these few commands, you will be doing a large amount of unnecessary, tiresome typing.

So, you should also learn at least some of these other frequently-used vi commands:

     h              move cursor one character to left
     j              move cursor one line down
     k              move cursor one line up
     l              move cursor one character to right
     w              move cursor one word to right
     b              move cursor one word to left
     0              move cursor to beginning of line
     $              move cursor to end of line
     nG             move cursor to line n
     ctrl-f         scroll forward one screen
     ctrl-b         scroll backward one screen
 
     i              insert to left of current cursor position (end with ESC)
     a              append to right of current cursor position (end with ESC)
     dw             delete current word (end with ESC)
     cw             change current word (end with ESC)
     r              change current character
     
     dd             delete current line
     D              delete portion of current line to right of the cursor
     x              delete current character
     ma             mark currrent character
     d`a            delete everything from the marked character to here
     p              dump out at current place your last deletion (``paste'')
     
     u              undo the last command 
     .              repeat the last command 
     
     J              combine (``join'') next line with this one
     
     :w             write file to disk, stay in vi
     :q!            quit VI, do not write file to disk,
     ZZ             write file to disk, quit vi
     
     /string        search forward for string (end with Return)
     ?string        search backward for string (end with Return)
     n              repeat the last search (``next search'')
     
     :s/s1/s2       replace (``substitute'') (the first) s1 in this line by s2
     :lr/s/s1/s2/g  replace all instances of s1 in the line range lr by s2
                    (lr is of form `a,b', where a and b are either explicit
                    line numbers, or . (current line) or $ (last line)

All of the `:' commands end with your hitting the Return key.

The `a' command, which puts text to the right of the cursor, does put you in insert-text mode, just like the `i' command does.

One of vi's advantages is easy cursor movement. Since the keys h,j,k,l are adjacent and easily accessible with the fingers of your right hand, you can quickly reach them to move the cursor, instead of fumbling around for the arrow keys as with many other editors (though they can be used in vi too).

Many of the commands can be prefixed by a number. For example, 3dd means to delete (consecutive) three lines, starting with the current one. As an another example, 4cw will delete the next four words.

The p command can be used for ``cut-and-paste'' and copy operations. For example, to move three lines from place A to place B:

1. Move the cursor to A.

2. Type `3dd'.

3. Move the cursor to B.

4. Type `p'.

The same steps can be used to copy text, except that p must be used twice, the first time being immediately after Step 2 (to put back the text just deleted).

When you are using vi, you can use the `map' and `abb' commands to save a lot of typing. For example, I often accidentally transpose two letters when I am typing fast, say typing `taht' instead of `that'. Since I do this so often, I place the command

   :map v xp

which means that the v key now performs the operations `x' and `p', (try `xp' yourself and you will see it work), in my

   ~/.exrc

file. Vi looks in that file whenever you invoke vi, so this new `v' command is available for my permanent use, automatically.

Also, I use the LaTeX word-processing package a lot, in which I often use the underline operation, denoted by

   \underline

So, in my

   ~/.exrc

file I have the line

abb ul \underline

So, whenever I type `underline' (note the space), vi automatically expands it for me, saving me typing.

For programming work, you will definitely want to make use of autoindent mode. In this mode, each time you hit Return, vi will automatically indent the next line to aligned with the previous one, making it easier to write well-structured code. (If you wish to start the new line at the far-left position anyway, simply hit Escape.) To set this mode, type

:set ai

and to leave it, type

:set noai

Arguments over the pros and cons of various editors become almost ``religious'' in their ferocity. I have tried all of the major Unix text editors, but have always come back to vi (actually an extension of vi called vim). But in the end it is a matter of taste. I have used vi as the introductory editor here because it is so prevalent in the Unix world, but you may wish to give others a try.



Footnotes

...insert-text-mode.1
The vi editor differs from many others in this respect. With editors such as joe and emacs, for instance, to insert text at the cursor position, one simply starts typing. However, that means that in order to perform most commands, one needs to use the Control key, causing jokes that heavy use of these editors causes fatigue in the finger that types the Control key.



Norm &
11/15/1997