Aug 182012
 
General Startup
	To use vi: vi filename
	To exit vi and save changes: ZZ   or  :wq
	To exit vi without saving changes: :q!
	To enter vi command mode: [esc]

Counts
        A number preceding any vi command tells vi to repeat
	that command that many times.

Cursor Movement

	h       move left (backspace)

	j       move down

	k       move up

	l       move right (spacebar)

	[return]   move to the beginning of the next line

	$       last column on the current line

	0       move cursor to the first column on the
		current line

	^       move cursor to first nonblank column on the
		current line

	w       move to the beginning of the next word or
		punctuation mark

	W       move past the next space

	b       move to the beginning of the previous word
		or punctuation mark

	B       move to the beginning of the previous word,
		ignores punctuation

        e       end of next word or punctuation mark

        E       end of next word, ignoring punctuation

        H       move cursor to the top of the screen 

        M       move cursor to the middle of the screen

        L       move cursor to the bottom of the screen 

Screen Movement

       G        move to the last line in the file

       xG       move to line x

       z+       move current line to top of screen

       z        move current line to the middle of screen

       z-       move current line to the bottom of screen

       ^F       move forward one screen

       ^B       move backward one line

       ^D       move forward one half screen

       ^U       move backward one half screen

       ^R       redraw screen
		( does not work with VT100 type terminals )

       ^L       redraw screen
		( does not work with Televideo terminals )

Inserting

       r        replace character under cursor with next
		character typed

       R        keep replacing character until [esc] is hit

       i        insert before cursor

       a        append after cursor

       A        append at end of line

       O        open line above cursor and enter append mode

Deleting

	x       delete character under cursor

	dd      delete line under cursor

        dw      delete word under cursor

        db      delete word before cursor

Copying Code

        yy      (yank)'copies' line which may then be put by
		the p(put) command. Precede with a count for
		multiple lines.

        :t.     will duplicate the line.

        :t 7    will copy it after line 7.

        :,+t0   will copy current and next line at the
        beginning of the file.

        :1,t$   will copy lines from beginning of the file
        to the current cursor position, to the end of the file.

Put Command
        brings back previous deletion or yank of lines,
	words, or characters

        P       bring back before cursor

        p       bring back after cursor

Find Commands

	?       finds a word going backwards

	/       finds a word going forwards

        f       finds a character on the line under the
		cursor going forward

        F       finds a character on the line under the
		cursor going backwards

        t       find a character on the current line going
		forward and stop one character before it

	T       find a character on the current line going
		backward and stop one character before it

	;	repeat last f, F, t, T

Find and Replace Commands

        :%s/hello/goodbye/g   find hello and replace with goodbye

        :%s/^\(.*\)\n\1$/g    delete duplicate lines

        :%s/\n/ /g            join lines together

Miscellaneous Commands

	.	repeat last command

	u	undo last command issued

	U	undoes all commands on one line

	xp	deletes first character and inserts after
		second (swap)

	J	join current line with the next line

	^G	display current line number

	%	if at one parenthesis, will jump to its mate

	mx	mark current line with character x

	'x	find line marked with character x

	NOTE: Marks are internal and not written to the file.

Line Editor Mode
	Any commands from the line editor ex can be issued
	upon entering line mode.

	To enter: type ':'

	To exit: press[return] or [esc]

ex Commands
	For a complete list consult the
	UNIX Programmer's Manual

READING FILES
	copies (reads) filename after cursor in file
	currently editing

	:r filename

WRITE FILE
	:w 	saves the current file without quitting
	:20,40w filename write the contents of the lines numbered 20 through 40 to
	a new file named filename
MOVING

	:#	move to line #

	:$	move to last line of file

SHELL ESCAPE
	executes 'cmd' as a shell command.

	:!'cmd'

Source:
Aug 012011
 

The search command is /. To search for polite type /polite. Pressing the letter “n” repeats the search in the same direction, and “N” repeats the search in the opposite direction.
The search option accepts most of the standard Unix pattern matching language. (See the Wildcard section.)
Suppose I had a file that contained the following text:
 There was a young man of Milan Whose poems, they never would scan;
 When asked why it was, He said,
 `It’s because I always try to get as many words into the last line as I possibly can’. -anonymous
 
Here are a few examples (using this text) that you will probably never use but may find inspiring:
 /[a-z]as will search for any lowercase letter followed by “as”.
 
In this example, it would find “was” and “last” but not “as” or “asked”.
 /[^c]an will search for any “an” preceded by any character other than a “c”.
 
In our text it would find “Milan” but not “scan” or “can”.
 /^[A-Z].*\. *$ will search for any line that begins with a capital letter and ends with a period and any number of blanks.

Our only match in the example text would be with the last line.
All of these search patterns can be used in the search and replace command that takes on the following structure:
 :s/search_string/replacement_string/g This command replaces every search_string on the current line with replacement_string.
 
Omitting the g (global) flag at the end of the command will cause only the first occurrence of search_string to be altered.
Often you may wish to confirm each replacement. This can be done with the confirm flag c. The confirm flag should be placed after or in place of the g flag.

Suppose I had the following line:
 “Give a skeptic and inch… and he’ll take a mile.” and typed “:s/take a mile/measure it/”.
 I would be left with “Give a skeptic and inch… and he’ll measure it”.
 
Any command that begins with a “:” is called a line mode command and performs its duty on the line the cursor is currently on.
However, you can override vi’s default of operating only on the current line by preceding them with a range of line numbers.
For example, if I wanted to replace “guy” with “gal” on lines 32 through 56 I would type “:32,56s/guy/gal/g”.
Omitting the “g” would cause only the first occurrence of “guy” in each line to be replaced.
The “.” and “$” play a special role in this sort of designation. “.” indicates the current line, and “$” indicates the last line of the file.
Therefore, if I wanted to delete from the current line to the end of the file I would enter “:2 :.,$d”.
I could even do something like: “:.,/Edison/d” which would delete from the current line to the next line that contained Edison.
One other shortcut that might be worth mentioning is that “1,$” and “%” both indicate all the lines in the file.
Therefore, “:1,$s/search_string/replacement_string/g” and “:%s/search_string/replacement_string/g” do exactly the same thing.
This works because “:d” is a line mode command that deletes the current line. The same could be accomplished by typing “dG”.