Quick Reference card for Vi
-------------------------------------------------------------------------------
TWO modes:
1. Command mode -- this is the startup mode, keys have special command
meanings
2. Insert mode -- inserting text
To get to command mode: ESC (several ESCs in a row won't hurt, just beeps)
To get to insert mode: o -- open a line below (O -- open a line above)
i -- insert within text
a -- append to current line
(to get out of insert mode, press ESC)
Another mode similar to insert is overtype mode: R (capital R)
Nothing is inserted, just type over what is already there.
Press ESC to exit this mode.
------------------------------------------------------------------------------
(All following commands are done from command mode)
Stopping and Saving
ZZ Quit and save
:q! Quit without saving
:w save to file without stopping vi
Cursor motions:
h left arrow
j down arrow
k up arrow
l right arrow
w advance to beginning of next word
b backward one word
$ go to end of current line
0 to to beginning of current line
G go to end of file
1G go to first line of file
:17 go to line 17
Deletions:
x Delete character under the cursor
D Delete to end of current line
dw delete current word 5dw -- delete 5 words
dd delete current line 10dd -- delete 10 lines
Other changes:
r replace one character, the one the cursor is on top of
R replace many characters, until ESC is pressed
xp exchange current character with the following character
Search:
/ forward search, type in your search string and press RETURN
n next search (don't type in string again)
? search backwards
Cut and paste:
yy yank current line (copy into buffer without deleting)
5yy yank 5 lines beginning with current line
p put after current line
dd delete line, can then later put
Informational and miscellaneous
:f show current file, whether modified, current line number
:!ls Do UNIX command (example: ls)
:!csh Fork off a Cshell, use "exit" to leave shell and get back to vi
:set nu Put line numbers at left edge (just for display,
they are not in the file)
:set nonu Remove line numbers
:r file Include file after current line
Undo
u undo last command. It only remembers 1 command.
Global search and replace
:10,25s/old/new/g Substitute "old" string with "new" in lines 10,25
:%s/^/ / For all lines (% is "all lines"), replace the begin-
ing of the line with 5 blanks
:%s/^ // For all lines, replace 5 blanks at beginning of line
with nothing
:50,.s/Unix/UNIX/ For lines 50 to the current line (.), replace "Unix"
with UNIX, only the first occurrence in the line
:50,.s/Unix/UNIX/g Same as above only do it for all occurrences of
"Unix" in the lines