Starting and Exiting vi

vi filename             edit filename
vi -r filename          recover filename
:w                      write
:w newfile              write to newfile
:w! oldfile             overwrite oldfile
:w >> oldfile           append to oldfile
:q                      quit
:q!                     quit and discard unsaved changes
:wq                     write and quit
ZZ                      write and quit

Cursor Movement

j			cursor down one line
k			cursor up one line
h			cursor left one character
l			cursor right one character
G			go to last line in file
1G			go to first line in file
nG			go to nth line
^			go to beginning of line
$			go to end of line
H			Home position of current screen
M			Middle line of current screen
L			Last line of current screen
nw			forward n words
nW			forward n words(ignore punct.)
ne			go to end of nth word
nE			go to end of nth word(ignore punct.)
nb			go to start of nth word
nB			start of nth word(ignore punct.)
/string 		forward search for string
?string 		backward search for string
n			next search
N			previous search

Changing Text

cc			change current line
nC			change specified number of lines
Cw			change word
ncw			change specified number of words
C			change from cursor to end of line
c$			change from cursor to end of line
c^ or cO		change from cursor to start of line
cG			change from cursor thru last line
c1G			change from cursor thru lst line
r			replace one character
R			overwrite
s			substitute one character for 1 or more
ns			substitute specified number of
			characters for one or more characters

Deleting Text

dd			delete current line
ndd			delete specified number of lines
dw			delete word
ndw			delete specified number of words
x			delete single character
nx			delete specified number of characters
D			delete from cursor to end of line
d$			delete from cursor to end of line
d^ or dO(zero)		delete from cursor to start of line
dG			delete from cursor to end of file
d1G			delete from cursor to start of file
n1,n2d			delete from line n1 to line n2

Screen control

D			scroll down 1/2 screen
U			scroll up 1/2 screen
F			scroll forward 1 full screen
B			scroll backward 1 full screen
L			redraw screen

Delete and Paste(put)

dd			delete current line
ndd			delete specified number of lines
dw			delete words
ndw			delete number of words
P (upper case P)	put lines/words before cursor
p (lower case p)	put lines/words after cursor

Copying and Paste(Yank and Put)

yy			yank current line into paste buffer
nyy			yank n lines into paste buffer
yG			yank current to last lines
yw			yank current word
nyw			yank n words
yW			yank word (include adjoining
ynW			yank n words (include adjoining
P (upper case P)	put yanked line(s)/words above cursor
p (lower case p)	put yanked line(s)/words below cursor

Search, Substitute,Delete

:g/text/d		search for text and delete (current
			line only)
:s/textlnewtext/g	search for text and replace with
			newtext (one time only)
:1,$sltextinewtext/	search text, replace first occurrence
			on each line with newtext
:1,$s/text/newtext/g	search and replace all occurrences
			of text with newtext
:nl,n2s/text/newteXtlg	search for text and replace from line
			ni to line n2 with newtext

Copying Text from File

:r filename		read from filename, place into
			current cursor position
:nr filename		read from nth line of filename to
			end of filename, place after cursor

Input Modes

a text			append text to right of cursor
A text			append text to end of line
I text			insert text from beginning of line
I text			insert text to left of cursor
o text			open line below current line
O text			open line above current line

Misc. Commands

u			undo previous command
U			undo change to current line
J			join current with next line
nJ			join n lines with current line
a (CR) (ESC)		split line after current cursor position
i (CR) (ESC)		split line before current cursor position
r (CR)			replace current character with new line
:f			display current file name
. (dot)			repeat last command
~			invert caps
xp			(transpose) switch 2 characters

Editing other files

:e filename		edit filename
:e! filename		clear buffer and edit file
:n			edit next file in file list
:n 			edit new list of files
:rewind			edit first file in file list

Shell Commands

:! command		execute a command from vi
:! command%		execute a command and supply the
			current file name as a parameter
:!!			execute the last shell command issued
:r! command		redirect the output of the command given
			to the current line

Setting vi options

:set			displays the set values
:set all		displays all options that can be set
:set list		shows begin A and end of line $
:set nolist		shuts of display of$ and A when list is on
:set nu			turn on line numbering
:set nonu		shut off line numbering
:set showmode or smd	enables the display of the mode (insert, or
			command mode)
:set noshowmode or nsmd	shuts off mode display