A vi Tutorial
Jim Watts, 1990
(Additional material by Michael J. Wise)
Getting Started
After entering vi by typing
vi <filename>
e.g. vimyprog.c,
what you see is the beginning of the myprog.c
displayed on the screen.
If myprog.c
is very short, a column of tildes, like this
-
~
~
~
will appear.
These just show that there is nothing more in the file.
The bottom line of your screen will contain the name of the file
and its size; this is the command line, and we return to it later.
To move down to the bottom of myprog.c
just type
-
G
and the final screen of myprog.c
will be displayed, with the
cursor at the beginning of the last line.
To go to line <n>, type
-
nG
and the cursor will move to the beginning of line <n>.
To finish a vi session (and saving your changes) type
-
ZZ
and you will be back in the shell.
Entering Text
As in ed (and sam in the Command window),
text can be added by typing
-
a
but in vi the a does not
show on the screen.
All text entered after this,
including newlines will be displayed following the position of the cursor,
pushing the other text down.
Unlike other editors, text-entry is terminated with <esc>.
Similarly, typing
-
i
will allow text to be entered in front of the cursor.
Hint: If you are not sure what mode you are in,
type <esc> twice, after which you can be sure that
are no longer in text-entry-mode.
(Assuming it an error, vi will beep when you do this.)
Many commands in vi have a capitalized version, so
-
A
will add text at the end of the line, and
-
I
will add text at the start of the line.
In all cases of text entry, <esc> terminates text-entry mode.
Moving Around
Since vi is a screen editor which does not make
use of a mouse, the cursor has to be moved from the keyboard.
To move straight down by one line type
-
j
Moving straight up requires
-
k
Movement by one to the right needs
-
l
and to move one character left type
-
h
Note that <space> can be used instead of l to move right.
<return> moves the cursor to the beginning of the next line.
To move more than one line, just type the distance that you want to
move before the direction, e.g.
-
10j
will move 10 lines down, and
-
6h
will move 6 spaces left.
As with pattern matching in sam and ed,
^ refers to the beginning of the line, and $ to the end.
To move to the end of a line type
-
$
and typing
-
^
will move the cursor to the beginning of the line.
Note that the character moves h and l can only occur within the
current line.
You can also move by words (usually much more efficient).
To move to the beginning of the next word, type
-
w
the command
-
e
moves to the end of the next word, while
-
b
moves back to the beginning of the last word.
These word moves work between lines.
The boundaries of a word are <space>, <tab> or
<return> - the whitespace characters - and
punctuation marks (i.e. most characters
other than letters or numbers).
It is possible to move across punctuation if
W, B and E are used instead of w, b and e.
Try out the differences between the shifted and unshifted commands.
Note: What works for w, b and e, will not
work as expected for
h, j, k and l, where the
upper-case commands do ENTIRELY different things.
Cursor movements (as can most commands in vi) can
be extended by prefixing them with a count, so that
-
5w
moves the cursor forward by five words.
The shifted commands H, L and M make movements relative to the
whole screen, typing
-
H
takes the cursor to the top of the screen,
-
L
takes it to the bottom, and
-
M
takes it to the middle.
Note that K is not a command, and J does not perform a
movement, (see section 11. below).
Deletion
To delete the character under the cursor, type
-
x
the character before the cursor is deleted with
-
X
or more simply <backspace>.
When text is deleted, it is not totally lost, but is written into the
undo buffer, which is similar to the snarf buffer
in sam,
or the Macintosh clipboard.
Like these, only the text of the last delete is saved.
Thus deletions in vi are in fact cuts, which can be
pasted back (see below).
To delete more than one character, the letter d is prefixed to a
movement to delete that range. So
-
dw
deletes the next word and
-
d7E
will delete all text to the end of the seventh next group of characters.
(Remember that E skips over punctuation.)
The single character deletions x and X are just abbreviations
for dl and dh.
Whole lines of text can be deleted using
-
dd
which deletes the current line.
The command
-
D
will delete from the cursor to the end of the line.
The Undo Command
To cancel the last operation just type
-
u
and the text will be restored as it was before the last command.
The shifted command
-
U
will undo any changes that have just been made to the current line.
Pasting Text
Since deletion is actually cutting, there is a pasting command to
write the undo buffer.
The command
-
p
writes the buffer immediately after the cursor, or on the next line, if
the cut was whole lines.
Pasting in front of the cursor, (or on the line above) is done by
-
P
Copying
It is not surprising that where there is cutting there should be copying.
The copying or yanking command y, is used
similarly to d, except that the text remains and
a copy is buffered,
(just like Macintosh or snarf in sam).
So
-
y3w
will yank three words into the buffer.
Then you use p or P to put them where you want.
To yank lines yy is used.
Also the command
-
Y
will yank one line into the buffer.
Substitution
There are three substitution commands in vi: r, which changes
one character, s, which replaces one character with text
and c, which allows a range to be replaced.
To change the character under the cursor, type
-
r
followed by the new character.
To substitute text for the character under the cursor, type
-
s
followed by the text, and ending with <esc>.
It is possible to change a sequence of letters, words
or lines, using
-
c
followed by a movement, then the text, and ending with <esc>.
For example, to change the next word you type
-
cw
In other words, c is like a d (for the specified
range), followed by an a.
There are shifted versions of the substitute commands.
R allows text to be changed by typing over it, finishing with <esc>.
S substitutes text for whole lines, and C changes text from the cursor
to the end of the line.
Searching
This is similar to using the Command window in sam and in ed,
but made simpler.
To search for a pattern, type
-
/
followed by the pattern, and ending with <return>.
A backwards search uses
-
?
instead of /.
To repeat the search, type
-
n
typing
-
N
will repeat the search in the reverse direction.
It is possible to search for a single character in a line using
-
f
followed by the character.
Typing
-
F
will search from left to right.
To repeat the search, type
-
;
and to reverse it
-
, (comma)
More will be said about searching with regular expressions
in lectures.
Two Useful Commands
It is possible to repeat the last command which changed text by typing
-
. (full stop)
Also to join a line to the line following, type
-
J
Using the Command Line
If you type
-
:
the cursor will move to the bottom line, and you will be able to enter
an ed style command.
This is particularly useful for operations that are to be
done over a number of lines or over the entire file.
Repeated changes can be done from the command line with
-
<address>s/<old_pattern>/<new_pattern>/g<return>
If the g is omitted, then only the first match on each line
within the addressed range will be changed.
For example
-
.,$s/^\.br/.SP/
1,$s/ Vi / vi /g
The first command goes from the current line to the end of the
file replacing an occurance of .br appearing at the beginning
of a line with .SP.
In the second command, all instances of Vi surrounded by
single blanks are replaced by vi.
If a command causes a number of changes to be made, the number of changes
is written on the command line, and the cursor moves to the position of
the last change.
Writing Out, Quitting and Escaping
To write the file the command
-
:w
on the command line is the same as sam and ed.
The q command from ed can also be used, but
if it is necessary to leave vi without saving any changes,
you will have to use
-
:q!
The effect of writing out the file and quitting can
be achieved in one step by typing
-
:wq
on the command line (which is also what ZZ does).
It is possible to execute shell commands from within vi
(or escapes to the shells).
To do this type
-
:!
followed by the command, which will then be executed.
When the command is complete, you will be able to return to the
editing session by typing any character, typically spaceA or
<return>.
Screen Control
vi has the equivalent of a scroll bar,
but this has to be done with control characters.
To move forward in the file by one screen type
-
<ctrl>F
one screen back is
-
<ctrl>B
It is possible to move by half screens,
-
<ctrl>D
moves down, and
-
<ctrl>U
moves up.
Possible Trouble
Since you will be using control characters, you might by chance hit
<ctrl>S, which will lock the screen.
You can unlock the screen with <ctrl>Q.
Another unexpected thing that can happen is if you hit Q by mistake,
you will go into open mode, and will get : at the bottom of the screen.
Type
-
vi
to get back to the screen.
Final Word
What has been presented above is only a
subset of the available commands.
For more details, check the man entry for vi.
Another tutorial, similar to this one can be found on the Web at:
-
http://www.macom.co.il/vi