A list of vi commands

vi has a comprehensive list of commands that can be used to control its activity. Please note, many commands can be prefixed by a number which will cause the command to be repeated that number of times.

e.g. 15dd will delete 15 lines if there are at least 15 lines from cursor to end of the file.

Screen shot of the vi editor, showing a simple C "hello world" example, with :w at the bottom ready to save the file.
The vi Editor in action!

Commands to enter or exit Edit mode

OperationKeystroke
insert before current positioni
append after current positiona
append at end of current lineA
insert on new line below currento
insert on new line above currentO
replace current character and the insert afters
replace current line from startS
overwrite character at cursorr<char>
overtype (replace)R
Enter edit mode

What you type will appear in the file, either inserted between or replacing the current text.

OperationKey stroke
end current edit modeEsc
Exit edit mode

You can also end the edit mode by leaving the text area or by moving the cursor outside the edit scope (this depends on which edit mode is being used).

vi Movement Command List

LeftRight
 Single Characterhl
Wordbw
End of Line^$
Movement within a line
UpDown
Linekj
Half a PageCtrl+uCtrl+d
Full PageCtrl+bCtrl+f
Max1GG
Movement within the document

Deletion Commands

OperationKey stroke
Delete a characterx
Delete a worddw
Delete an entire linedd
Available delete operations

List of Miscellaneous commands

OperationKey stroke
Display line no. and total no of linesCtrl+G
repeat last command.
undo last commandu
yank (copy) wordyw
yank lineY
paste (insert) yanked item afterp
paste (insert) yanked item beforeP
change case and move one character right~
join next line onto end of current lineJ
join previous line onto start of current lineK
move to home position (top right corner)H
General Commands

Search Command

OperationKey stroke
Find a string/<string to find>
Repeat a search forwardn
Searching in a document.

List of vi Replace Commands

:%s/<string to replace>/<replacing string>/

optionally, ending with g to do a global replace

vi File Operations

OperationKey strokes
read file into current file at current location:r <filename>
write file to new file name:w <filename>
save to current name:w
end current edit and start on file named
NB. if current file has been changed
either write the change, or discard changes
:e <filename>
discarding changes and restart edit on current file :e!
Working with files from within vi.

Commands to leave vi

OperationKey stroke
save any changes made and quit editor: x
:wq
ZZ
quit (no changes made):q
force quit discarding any changes:q!
Commands that exit vi