VI Reference Card

* Starting vi
* Saving, Editing Other Files and Quiting vi
* Moving The Cursor Within The File
* Moving The Cursor Around The Screen
* Creating Text
* Modifying Text
* Searching and Replacing Through Text
* Yanking & Pasting
* Buffers and how to use them
* Some Useful ex commands for use in vi

Starting vi
vi -options filename
-l - lisp mode
-r - filename recovering after an editor crash or system crash
-R - read only mode

Saving, Editing Other Files and Quiting vi
:w - write current file
:w filename write the file filename
:w >> filename - Append the contents of the buffer to the filename
:w! filename - write the file filename overwriting the current file
:x,y w filename - write lines x to y under file filename
:x,y w! filename - write lines x to y under filename, overwriting current file
ZZ - write file and quit
:wq - write file and quit (same as ZZ)
:q - quit vi if no change was made to the text
:q! - quit without saving
:f filename - Set the current filename to filename
:n - Move to the next file in the file list
^^ - Edit the alternate (normally the previous) file
:e filename - Switches to the specified filename
:r filename - Read the contents of a specified filename, including it in the current edit buffer
^Z - Suspend vi
^\ - Quit "vi" mode and go into "ex" mode. to get back to "vi" use ":vi"

Moving The Cursor Within The File
Backspace - Move the cursor to the left one character position
nh - Move the cursor to the left n characters position (default one character)
nl - Move the cursor to the right n characters position (default one character)
nj - Move the cursor n lines down (default one line)
nk - Move the cursor n lines up (default one line)
nw - Move the cursor forward n words (default one word), will place the cursor at the first character of the next word
nW - Move the cursor forward to the beginning of a word, skipping over punctuation (default one word)
nb - Move the cursor back n words (default one word)
nB - Move the cursor back n words, skipping over punctuation (default one word)
ne - Move the cursor n words forward to the end of the n word (default one word)
nE - Move the cursor n words forward to the end of the n word, skipping over punctuation (default one word)
0 (zero) - Move to the beginning of the current line
n$ - Move to the end of the n line from the cursor
% - Move the cursor to the matching parenthesis or brace
Enter/Return - Move to the first character of the next line
^ - Move the cursor to the first non-whitespace character (to the first char of the line)
n_ - Move the cursor to the first non-whitespace character of the next n-line (current line is 1, so 1_ will be the first character of the current line)
n- - Move the cursor to the first non-whitespace character of the previous n-line (current line is 0, so 1- will be the previous line)
n+ or cr - Move the cursor to the first non-whitespace character of the next n-line (current line is 0, so 1+ will be the first character of the next line)
n| - Move the cursor to the n column
n( - Move n sentences backward
n) - Move n sentences forward
n{ - Move n paragraphs backward
n} - Move n paragraphs forward
n[[ - Move to the previous section (default begin of file)
n]] - Move to the next section (default End of file)
nH - Move the cursor to the n line from the top of the screen
nL - Move the cursor to the n line from the bottom of the screen
M - Move the cursor to the middle line on screen
nG - Go to line n
/ - Move to the first character in line marked with /
CTRL+h - Move the cursor one space to the left
CTRL+j - Move the cursor down one line in the same column
CTRL+m - Move to the first character on the next line
CTRL+n - Move the cursor down one line in the same column
CTRL+p - Move the cursor up one line in the same column

Moving The Cusror Around The Screen
CTRL+e - scroll up one line
CTRL+y - scroll down one line
CTRL+f - scroll forward one screen
CTRL+b - scroll backward one screen
CTRL+d - scroll forward half screen
CTRL+u- scroll backward half screen
z - redraw screen current line at the top
z- - redraw screen current line at the bottom
z. - redraw screen current line at center of screen

Creating Text
a - Append text after the current cursor position
A - Append text at the end of the current line
i - Insert text before the current cursor position
I - Insert text from the beginning of a line
o - Enter insert mode in a new line below the current cursor position
O - Enter insert mode in a new line above the current cursor position
n. - Repeat the last command n times

Modifying Text
nx - Delete n characters under and after the cursor
nX - Delete n characters before the cursor
d(position) - Delete from the cursor position to endpoint of (position)
ndw or ndW - Delete n words
ndd - Delete n lines
D or d$ - Delete text from the cursor to the end of the line
r? - Replace the current character under the cursor with the char ?
R - Overwrite the rest of the line
ns - Substitute n characters
nS - Substitute n lines
u - Undo your previous command
uu - Undo Undo
C - Change to the end of the line from the current cursor position
c - Change from the current cursor position till
ncc - Change n lines (default 2)

Searching and Replacing Through Text
?pattern - Search backwards for pattern
/pattern - Search forward for pattern
:n - Repeat Search in same direction for next occurance of last-searched-for text (Next Match)
:N - Search in other direction for next occurance of last-searched-for text
:s/old/new - Search & Replace the current line, Search for old to be replaced with new
:s/old/new/g - Search & Replace all accurrences on the current line
:[x,y]s/old/new - Search & Replace, Search line x to y for the old pattern to be replaced by the new (if no x,y specified by default search the whole buffer)
:[x,y]s/old/new/g - Search & Replace line x through y for all accurrences

Yanking & Pasting
The vi Yank command is used as the copy command , it's copying the text into a buffer which later on it may be pasted elsewhere in the file with put command (if no buffer is specified, then the general buffer is used.)
yw - Yank rest of the word (if the cursor is on first character will yank the whole word)
y$ - Yank rest of the line
nyy - Yank the entire n-lines
Y - Same as yy
y(to) - Yank from curser position till (to)
P - Put the Yanked/deleted text before the cursor
p - Put the Yanked/deleted text after the cursor

Buffers and how to use them
The undo buffer contain only the recent yanked or deleted text. If you yanked text into the buffer and intended to paste it but before you did so you have performed any other deletion/yanking you will be unhappy with the pasting results. In order to prevent this you would have to use named buffers. Name buffers allow you to keep up to 26 places which text can be deleted or yanked. The contents of the name buffer remains unchanged until vi finish session ( you can change it during the work if you want to). Buffers are named using a " followed by a lowercase letter. You can see that buffers are named from "a to "z. i.e. "c2yy will yank 2 lines into the name buffer c. To put from name buffer the sequence is "(Buffer Name)p/P command. Yanking/deleting name buffer will replace the previous content of that buffer. But can you append the name buffer? Sure you can, just use uppercase instead of lowercase i.e. "Ad4w will delete next four words and append them into buffer a.
Delete Buffers - vi saves the last 9 deleted buffers in cells numbering from 1-9. Inorder to recover the most recent delete use "1p/P command and so on.

Some Useful ex commands for use in vi
:set all - Show all options and their values
:set (option) - Turn option on
:set no(option) - Turn option off
:set (option)=(value) - Set option to value
:set - Show all non-default options and thier values
:set (option)? - Show option's values


Random quote Etorre's Observation: The other line moves faster. “