by Charles Roth. Last revised 11 June 1996.
VI is a very powerful text editor that dates from the early days of Unix. It is also quite obscure in its workings. This "cheat sheet" summarizes the most common things you'll want to do in VI, and points the way to some of its more powerful capabilities.
To edit a file called "xyz", type: vi xyz
VI is a "two-mode" editor. When you type a character, it is either:
(mode 2) immediately inserted into the text of the file you are editing.
VI always starts in the first mode, called "command mode". Section 4 tells you how to switch to the other mode, called "insert" mode.. When in doubt, press the escape key (usually labeled "Esc" or "ESC"). This will put VI in the first, or "command" mode. If you are already in command mode, it will just beep at you. Ignore it.
|Short Distances||Long Distances|
|h||move left one character|
|l||move right one character|
|j||move down one line||ctrl-F||move down ("forward") one screen|
|k||move up one line||ctrl-B||move up ("back") one screen|
|0||(zero) move to start of line||1G||move to the first line of the file|
|$||move to end of line||G||move to the last line of the file|
|Moving to Special Places||Text Searching|
|ma||mark this location as point "a"||/xyz||search for and move to next "xyz"|
|'a||move to previously marked "a"||n||move to next instance of search|
|%||move from "(" to matching ")"||N||move to previous instance of search|
All of these commands switch VI from "command" mode to "insert" mode. Everything you type after this command gets inserted into the file. Press ESC to go back to command mode.
a insert ("append") text after cursor position
A insert text at the end of this line
|Editing text||Editing the file|
|x||delete this character||:w||save your work|
|dd||delete this entire line||:q!||exit VI, don't save your work|
|D||delete from here to end of line||ZZ||save your work, and exit VI|
|u||undo last operation||:r abc||read in file abc after this line|
|.||do last operation again|
You may perform more complicated operations on sets of lines in a file by using "ex" commands. All "ex" commands begin with a colon (":") and have the general form:
:line1,line2,operationLine numbers may be of the form:
|5||(an actual number)|
|$||the last line of the file|
|.||the current line|
|'a||location "a" (from 'm' command)|
|.+1||line after this line|
|.-5||five lines back from this line|
|d|| delete lines
|m|| move lines
|t||transfer (copy) lines|
|s|| substitute strings on lines
| ||deletes lines between 'a and current line|
| ||copies lines 1 through 5 to the end of the file|
| ||substitutes first "abc" with "gef" in all lines|
| ||substitutes all "abc"s with "gef" in all lines|
| ||moves lines 8 through 10 to after line 12|
| ||duplicates current line|
Certain characters, when used in between the slashes in the substitute ("s") command, have a special meaning. These characters are called "metacharacters". They are:
|^||invisible character (or "anchor") before first character on line|
|$||invisible character (or "anchor") after last character on line|
|.||any single character|
|*||any number of the immediately preceeding character|
|[a-c]||any one character in the range a-c|
|\||escape next character to have normal meaning, e.g. "\$" is just a dollar sign.|
|&||replace with entire match string|
Puts "Hi! " at beginning of every line
On current line, replace "a", any character, "b", with "ABC"
Double contents of every line. That is, take each line, and append it to itself with a space in between. The ".*" matches any number of any character, and it's anchored on both ends so you get the entire line. Each "&" gets replaced with the entire match, so you end up "doubling" the entire line.