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:
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.
l Move forward a character
SPACE Move forward a character
h Move backward a character
w Move forward a word
b Move backward a word
j Move to next line
k Move to previous line
0 Move to beginning of line
$ Move to end of line
RETURN Move to the beginning of the next line
- Move to the beginning of the previous line
H Move to the top of the screen
M Move to the middle of the screen
L Move to the bottom of the screen
^D Move forward one screenful
^U Move backward one screenful
^L Clear screen and redisplay everything.
1G Go to beginning of file
G Go to end of file
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!
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.
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.
- To enter a new line immediately under the current
line, type the letter o. This will open an empty line and anything
you now type will appear on that line. When you are finished entering this
new data, you will want to get back to command mode. This is done by
hitting the ESC key.
- The upper-case O is similar, except that
a new line is opened above the current one.
- If you want to create a blank line in between
two lines, move to the first of the two lines and type o.
- You can delete the last character you typed while in
insert mode by typing a BACKSPACE.
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.
You can delete characters, words, or lines as well.
Here is a summary of the delete operations:
x delete the character under the cursor
dw kill the next word after the cursor
db kill the word immediately before the cursor
dd kill the entire line
D kill from the cursor position to end of line
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.
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:
i enter insert mode immediately in front of the cursor
a enter insert mode immediately after the cursor
I inserts new characters at the beginning of the line
A appends new characters to the end of the line
To change or replace existing text, you can one of
the following commands:
- Change word is cw. This changes from the
cursor to the end of the word.
- Change entire line is cc.
- The change-to-the-end-of-the-line command is the
upper-case C. This deletes the text from the cursor to the end of the
line and then does an append (the a command).
- The r command is to replace a single character
without having to first delete it and then insert a new character.
- Similar is the s command. This substitutes new
characters for the character under the cursor.
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.
Any change may be repeated by hitting the period (.).
The upper-case J is used to join the current
line and the one below into a single line.
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.
- If you type ^G, Vi will display
some useful information, like the filename, the current line number, ...
- If you would like to exit Vi and save
all your changes, the easiest thing to do it is type ZZ (two upper-case
Z's).
- If you would like to exit Vi and abandon all
your work, type the :q! command (colon, q, and exclamation mark). Be
careful doing this.
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.
:w Write the changes to the current file but don't exit
:w filename Write the changes to the named file but don't exit
:r filename Read the named file and place its contents right
after the current line
:e filename Edit a different file
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.
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.
- The n will find the next occurrence of
the string last searched for.
- To search backwards use the ? instead of /.
- If you were searching forward but wanted to search
backward without retyping the search string, simply typing ? and RETURN
(or just N) will do the trick. Also, visa-versa.
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 (;).
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.
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.
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.