VI
Positioning
$ End of line.
^ Beginning of line.
G
Last line.
1G First line.
w Forward to beginning of next word.
e Forward to end of next word.
b Backwards to beginning of last word.
:f Shows which line you are on and number of total lines.
ctrl F Scroll forward a page
ctrl B Scroll back a page
Manipulating Text
u Undo.
I Insert text.
a Append text.
dG Delete all lines from where you are to end of file.
~ Change from lower case to capitals.
Copying & Moving Text
y$ yank from that position to end of line.
"adw Put deleted word in buffer a.
"cyy Put yanked line in buffer c
"ap Retrieve text from buffer a
:g/dog/ya Yank all lines containing dog and append to a buffer a
Search & Replace
/ Search forward
? Search backward
fc Intra-line forward search
; Repeat last intra-line search.
:g/text_pattern/p Global search
:1,$s/original/final/ Replace all occurrences of original with final.
:g/dog/d Delete all lines containing dog
To delete all lines with a certain character in use grep. Eg grep -v "char" file > output
Working with Files
vi one two three Working with multiple files in succession.
Macros
Buffer macro's let you place a series of commands into one of vi's buffers.
To create a buffer macro type the desired command into your file and the delete that text into a named buffer (e.g. b).
To use the macro type @b
Miscellaneous
u Undo
: undo
Vi automatically keeps the last nine deletes in the buffers 1 to 9, so to undo one of these deletes paste from the buffer e.g. "1p pastes from buffer 1.
:! command Host command
:set number Sets numbering on.
:set nonumber
General Commands
:a,bs/original/new/g Changes all occurences of original to new in lines a to b (b=$ for last line).
:g/patt/comm Scans each line containing patt(ern) and performs comm(ands) specified.
:g!/patt/comm Scans each line that does not contain the patt and performs comm specified.
:g/pattern/p Print all lines with the pattern in.
:start_line, end_line g/pattern/p Print lines within the range specified with the pattern in.