Learning the vi Editor/vi Reference

The following conventions are used in this reference.

Movement
vi can be set up on most systems to use the keyboard movement buttons, such as cursor left, page up, home, delete, etc.

Inserting
All insert commands put vi into insert mode. Insert mode is terminated by the ESC key.

Deleting
Each time a delete command is used, the deleted text is placed into the buffer, replacing any text already in the buffer. Buffered text can be retrieved by p or P.

Changing
The change commands all select text to be removed, the end of which is indicated by a $. Insert mode is entered and new text overwrites or extends the text. When the  key is pressed to terminate the insert, any remaining original text is deleted.

Text deleted during a change is placed into the buffer, replacing any text already there. Buffered text can be retrieved by p or P.

Cut and Paste
The yank commands copy text into the vi buffer. Text is also copied into the buffer by delete and change commands. The put or place commands retrieve text from the buffer.

Searching
Searching uses regular expressions.

Search and Replace
Search and replace uses regular expressions and the Ex command (short ) which has syntax similar to the sed utility - which is not surprising sed, Ex and Vi have common roots - the Ed editor.

The following meta-characters have special meaning in the replacement pattern: For example .:s/\(foo\) \(bar\) \(baz\)/\u\1 \U\2\E \3/ could match the string foo bar baz and substitute Foo BAR baz for it.

Mark Text
Marked lines can be used when changing or deleting text.

vi Options
All options are ex options, and so require an initial colon.

Default options may be placed into a file in the user's home directory called .exrc. Options in this file do not have the initial colon, e.g.
 * set ic

ex Commands
ex commands start with :, which puts vi into last line mode, entered on the last line of the screen. Spaces within the command are ignored.

ex line commands
These commands edit lines and have the following syntax:
 * 1) No line number, meaning work on the current line.
 * 2) With %, meaning work on all lines.
 * 3) A pair of line numbers, such as '3,5' meaning work on lines 3 to 5 inclusive. Either number can be replaced with ., standing for the current line or $ standing for the last line. So .,$ means from the current line to the end of the file and 1,$ means the same as %. Additionally simple arithmetic may be used, so .+1 means the line after the current line, or $-5 means 5 lines before the last line.

External link

 * vim Official Reference Manual
 * An introduction to the vi editor
 * Learning Unix in 10min

Vi文件編輯器/vi指令表