A Mug's Guide to vi
P. A. Robinson and N. G. R. Broderick
This guide is intended to introduce enough of the UNIX editor vi so that the beginner can edit files successfully. A fuller list of vi commands can be found in the guide vilist.
Starting to Edit
The command vi filename will let you edit the file called "filename", which can be either a new or old file.Commands
Commands to Move Around a File
H
Jumps to the top of the screen.
L
Jumps to the bottom of the screen.
w
Moves one word forward. 3w moves 3 words forward.
/
Slash followed by characters searches for the next occurrence of those
characters. n will search for the next one after that.
rightarrow, leftarrow, uparrow, downarrow
These keys allow you to move around the file in the directions of the arrows.
Commands to Add, Delete, or Change Text
a
Adds text after the cursor. Terminated by pressing ESC.
D
Deletes the remainder of the current line.
dd
Deletes the current line. 7dd deletes the next 7 lines following
the cursor. Holds the deleted lines in a buffer, available for
subsequent pasting using the p command.
dw
Delete the current word. 3dw deletes the next 3 words, starting
at the cursor.
i
Inserts text before the cursor. Terminated by pressing ESC.
J
Joins the next line to the end of the current line.
o
Inserts text after the current line. Terminated by ESC.
O
Inserts a line above the current line. Terminated by ESC.
p
Puts the contents of the buffer immediately following the current line.
P
Puts the contents of the buffer immediately before the current line.
r
Replaces the current character by the next character typed.
R
Replaces all characters that you type over, beginning at the cursor and
ending when you hit the ESC key.
s
5s will delete 5 characters, starting where you
are, and replace them with whatever you type. Terminated by hitting ESC.
S
3S will delete 3 lines, starting where you are, and
replace them with whatever you type. Terminated by hitting ESC.
:n,ms/x/y/g
Replaces all occurrences of "x" by "y" in all lines from
"n" to "m"
inclusive. :1,$s/x/y/g does this globally. :n,ms/x/y/c
requires you to confirm each change.
u
Undoes the last change.
w
Moves one word forward. 3w moves 3 words forward.
yy
Yanks a copy of the current line and holds it in a buffer, ready to paste
down somewhere else. 6yy yanks 6 lines, starting at the cursor.
x
Deletes the character under the cursor. 5x deletes the next 5 characters,
starting with the one under the cursor.
File Handling and Miscellaneous Commands
:q!
Exits vi without saving anything.
:w filename
Writes the file you are editing to a file called "filename".
:w! filename will overwrite "filename" if it already exists.
ZZ
Exits vi and writes the output to the file being edited.
ESC
The escape key terminates the current command, such as i or a.
\
The backslash is used to tell vi that the next character has no
special meaning. Used to nullify the special meanings of characters such
as \, ., etc.
The authors thank G. Geers for proofreading this guide.