SMU School of Engineering
General Use UNIX Machines

Vi Tutorial

Vi operates in one of two modes: command mode and insert mode. You start up in command mode you enter insert mode when you need to add new text to a file.

Several commands are of the CONTROL type; these will be indicated as an up-carat (^) followed by a letter. So CONTROL-D will now be written as ^D. You would execute this command by holding the CONTROL key down hitting the letter D.

Vi will scream at you whenever something can't be done: you'll either hear a bell or see the screen flash.

In this tutorial, you'll learn about:

Basic Cursor Control

There are several ways you can move around the screen. One way (not the best, but the most basic) is to use the commands left, down, up and right. As you can imagine, these commands (which are given to Vi as h, j, k, and l respectively) move the cursor from where it currently is to a new place in the given direction. Here, in a more graphical form are the commands:
                           Previous line, k
                                  :
                                  :
      Backward, h .... Current cursor position .... Forward, l
                                  :
                                  :
                             Next line, j
You'll probably find it easy to think of these by placing the fingers of your right hand on these four keys. The h is the furthest to the left, the l is the furthest to the right. The j and k are for down and up, respectively. These are the basic cursor positioning commands and you'll be using them ALL the time so it would be of great benefit if you learn them now.
When you go off the top or bottom of the screen, the text beyond the edge is shifted onto the screen so that your instructions can be carried out while keeping the cursor on the screen.

If moving by characters is too slow, you can move by words. The w moves forward a word and b moves back a word. The e command is similar to the w except that it moves to the end of the word.

Another simple cursor-motion command is G (upper case) which moves to the end of the file. The command 1G should move you to the beginning of the file. More on this later. You can get back to your previous position by typing ``.

To move to the top of the visible screen try upper-case H (for Home). Upper-case M puts you in the middle of the screen. Upper-case L puts you on the last line of the screen.

Here is a summary of simple moving operations:

Like most commands in Vi, these commands can be given positive numeric arguments which cause them to be executed repeatedly. The way you give a command a repeat count is by typing the number before typing the command. The digits will not show, but they are there anyway. For instance, 8h moves back eight characters.

This also applies to the screen moving commands, ^D and ^U. When given an argument, they scroll the screen up or down by that many lines and, in fact, ^D and ^U will now only scroll by that new number of lines.

If the go-to commands are preceded by a number, they go to that line number in the file. Very useful when the compiler says there is an error on line X!

Quitting From Partial Commands

The character in Vi used to quit out of partially completed commands is ESC. For example, you can use ESC to discard a numeric argument or the beginning of a command that you don't want to finish.

Inserting

If you want to enter text, you must enter insert mode. This may be done in a number of ways, depending upon where you need to enter the new text.

Vi can only read or write lines, to or from a file, which are less than or equal to 255 characters.
Lines which are longer than the width of your terminal are usually wrapped around. Usually because some terminals operate differently than others. It is generally a good idea never to try to go beyond the end of the screen. More on this later.

You've now learned the most basic way of entering something in Vi.

Deleting

You can delete characters, words, or lines as well. Here is a summary of the delete operations:

Note that dd deletes the line itself, and makes all the other lines move up. If you give dd a repeat count, it deletes that many lines.

The text that has just disappeared is saved so that you can retrieve it. To retrieve the last killed line and put it after where the cursor currently is, type p. Use upper case P to put it above the current line.
And when you put a line back it is still in the save buffer. This means you can put several copies into different locations.

You do not have to delete lines to put them into the save buffer. Typing yy will yank a line without deleting it. This is a handy way to duplicate some lines.

What if you delete a line and would like to put it back, but delete another line first? Sorry, but only the last one is saved.

More on Inserting

The open-a-line commands only start fresh lines. What if you want to add new material to the middle of an existing line? There are two commands for this:

To change or replace existing text, you can one of the following commands:

Undoing Changes

Suppose you have just deleted a word and realized it was the wrong word. You may undo the last change (insertion or deletion) by simply typing the u command.

Repeating Changes

Any change may be repeated by hitting the period (.).

Joining Two Lines Together

The upper-case J is used to join the current line and the one below into a single line.

Files

In order to make the text you edit permanent, you must put it in a file. Otherwise, it will go away when you leave Vi. While you are editing a file in Vi, your changes are actually being made to a private scratch copy of the file. However, the changes still don't become permanent until you save the file. This is so you can have control to avoid leaving a half-changed file around when you don't want to.

The other commands for saving and reading files are unlike the other commands you have learned in that they consist of two characters. They all start with the colon character (:). There is a small series of commands that start with a colon; they all end with a RETURN.

The first command is useful when you are making many changes and would not like to loose all of your work if the computer crashed. Use this command every 5 minutes or so to prevent catastrophes.

The second is useful when you would like to save your changes in a file different than the original. (Not that often used.)

The next one is good for including information from one file into the current one.

The last one is how to go to a different file and work on it without first having to leave the editor. Make certain that you save the current file first with :w or Vi won't allow you to switch files.

To make a new file, just edit it as if it already existed. Then start typing in the text. When you ask to save the file, Vi will really create the file with the text that you have inserted. From then on, you can consider yourself to be editing an already existing file.

Text Searching

A very common thing to do is to look for a word in a file. This is easily done in Vi by first typing the slash (/), then the string you would like to find and hit RETURN. When you do you should see a highlighted slash on the very bottom of the screen.

You can do a simple "find a character only on the current line" by typing f and a single character. The upper-case F does the simple find in the reverse direction. To repeat an f or F command, use the semicolon (;).

Search And Replace

It is often necessary that we need to find a certain character sequence and replace it with a different one. This is a little cumbersome, but that's life. The command is another of the "colon" commands.

Here are some examples:

:1,$s/Pat/Chris/g

This is read as "for lines 1 to the end of the file substitute the
characters Pat for the characters Chris everywhere on the line."

You can restrict the substitution to particular lines:

:200,312s/Pat/Dale/g
By the way, the colon commands are really commands to another editor (Vi is actually built on top of the ex editor) to do the work. You should learn more about ex very soon.

Marking Your Location And Returning To It

If you move from one location to another in the file and would like to get back without using ^D and ^U the quote-quote (two single quotes) will do it for you. This is very useful when you have used G or / to go somewhere and found you went to the wrong somewhere.

Often we are working on a section of code and would like to explore other sections for a while and then return to the section of interest. One way would be to use ^G to find the line number, write it down and then finally use #G to get back. But there is an easier way!

The lower-case m marks the spot. The single quote (') returns you there. But its a little more complicated. You have to name the spot with a letter. For instance, mark the spot with ma then you can return to it using 'a.

Conclusion

You'll probably find that if you use Vi for a few days you won't be able to give it up. Initially it may give you trouble. But remember that this is the case with any editor, especially one that can do many, many things.

There is another Vi tutorial online that you can follow. Copy the files in the directory /usr/local/lib/vi_tutorials to your account and start following the instructions in the first file (1basics).

Learning Vi should not stop here. There are many things you can do with it. Find an experienced Vi hacker to show you some tricks after you've gotten used to the basics. Learn about something called the .exrc file and read through the online manual.