My favorite vi editor commands
These are culled from the man pages and contain a few addtions. This subset lets you do pretty much anything, although mastery of
additional commands will certainly make you much more efficient. You can get around pretty well with just the arrow keys and nG. 1G takes
you to the top, and G takes you to the bottom. <ctl>-F and <ctl>-B let you page-scroll. /text searches, and the :s/dog/cat/g9999 does a global substitution.
With these and the insert and append commands (i, a, I, A) you can pretty much do almost everything, and then you can quit with a save :wq
If you want a decent screen editor for OS X or other unix platform, check out Textmate or jedit. Each is a nice alternative to emacs.
vi has two modes; you are either entering text or you
are executing commands, and you have to be in the right
mode to do one or the other. You will be in command mode
when you first start editing a file. There are commands
that switch you into input mode. There is only one key
that takes you out of input mode, and that is the <escape>
The commands to move around the file are:
h Move the cursor left one character.
j Move the cursor down one line.
k Move the cursor up one line.
l Move the cursor right one character.
<cursor-arrows>
The cursor arrow keys should work, too.
/text<carriage-return>
Search for the string ``text'' in the file, and
move the cursor to its first character.
/text<carriage-return> forward search
?text<carriage-return> reverse search
The commands to enter new text are:
a Append new text, after the cursor.
i Insert new text, before the cursor.
o Open a new line below the line the cursor is on,
and start entering text.
O Open a new line above the line the cursor is on,
and start entering text.
<escape>
Once you've entered input mode using the one of the
a, i, O or o commands, use <escape> to quit enter-
ing text and return to command mode.
The commands to copy text are:
yy Copy the line the cursor is on.
p Append the copied line after the line the cursor is
on.
The commands to delete text are:
dd Delete the line the cursor is on.
x Delete the character the cursor is on.
The commands to write the file are:
:w<carriage-return>
Write the file back to the file with the name that
:q!<carriage-return>
Quit, discarding any modifications that you may
have made.
[count] <control-B>
Page backwards count screens.
[count] <control-F>
Page forward count screens.
[count] j
Move the cursor down count lines without changing
the current column.
Move the cursor up count lines, without changing
the current column.
<escape>
Execute ex commands or cancel partial commands.
[count] $
Move the cursor to the end of a line.
& Repeat the previous substitution command on the
current line.
[count] .
Repeat the last vi command that modified text.
0 Move to the first character in the current line.
: Execute an ex command.
[count] A
Enter input mode, appending the text after the end
of the line.
M Move to the screen line in the middle of the
screen.
[count] R
Enter input mode, replacing the characters in the
current line.
U Restore the current line to its state before the
cursor last moved to it.
ZZ Write the file and exit vi.
[count] i
Enter input mode, inserting the text before the
cursor.
u Undo the last change made to the file.
ex mode commands. While in edit mode, if you type :, you go into ex mode, which is quite powerful and convenient.
Here are a few commands I find useful.
:[Nn][ext][!] [file ...] Edit the next file from the argument list. vi foo.txt bar.txt allows you to edit these 2 sequentially. :q[uit][!] End the editing session. The :q! option quits without saving, useful after a major fuckup. :[line] r[ead][!] [file] Read a file. This inserts the content of [file] at the current [or specified] line.
:[range] w[rite][!] [>>] [file] :[range] w[rite] [!] [file] :[range] wn[!] [>>] [file] :[range] wq[!] [>>] [file]
These are all various write and write-quit options. :wq saves the whole file and quits. :w filename writes everything;
the range specifies the range. >> appends.
The Substitute command is also very useful and powerful, but I find the syntax very hard to remember:
:[range]s[ubstitute]/pattern/string/[options]
Typical example: Let's say I want to substitute all occurances of the word dog in a file with cat in my text file
in the next 99 lines:
:s/dog/cat/g99
October 10, 1996 VI(1)
Man(1) output from the vi man page converted with man2html