Learning the vi Editor/vi Reference
The following conventions are used in this reference.
- <c>
- A single character, such as 'a' or '1'.
- <ESC>, <Ctrl-[>
- Indicates that the Escape (Esc) key on your keyboard should be pressed, which is identical to Control and '['.
- <CR>
- Indicates that the Carrier Return (Enter) key should be pressed.
- <TAB>
- Indicates that the Tabulator key should be pressed
- <Ctrl-x>, <C-x>
- Indicates that the Control key and the 'x' key should be pressed simultaneously. 'x' can be almost any other key on your keyboard.
- <Shift-x>, <S-x>, <X>
- Indicates that the Shift key and the 'x' key should be pressed simultaneously
- <Meta-x>, <M-x>
- Indicates that the Meta or Alt key and the 'x' key should be pressed simultaneously.
- :quit, :q
- An Ex command. started with <:>, followed by the command and ends with <CR>. For many Ex commands there is a long form (:quit) and a short form (:q).
- :set nocompatible
- represents a setting.
- strlen ()
- represents a function.
- /pattern/, ?pattern?
- A Search pattern. Search pattern in vi are regular expressions.
- :ranges/search/replace/options, :global /pattern/ delete
- A Search pattern combined with an Ex command.
All commands in vi are case sensitive.
c | A single character, such as 'a' or '1'. |
m | A single lowercase letter, used to mark text. |
string | Several characters, such as 'abc bed'. |
pattern | A string used in searching, which may contain regular expressions. For example 'abc' or '^ab[123]'. |
myfile | The name of a file to be edited. |
Invocation
editvi myfile | Open the file myfile for editing. If it does not exist, a new file is created. Multiple files can be opened at the same time. |
vi +line myfile | Open the file myfile with the cursor positioned at the given line.
|
vi +/string/ myfile | Open the file myfile with the cursor positioned at the first line containing the string. If the string has spaces it should be enclosed in quotes.
|
vi -r | Lists recovery copies of files. A recovery copy is taken if a vi session is killed or the system crashes. |
vi -r myfile | Opens a recovery copy of the file myfile. |
view myfile | view is a read only version of vi. All vi commands, include those to change the file are allowed and act as in vi. The difference is that normal attempts to save, ZZ or :wq do not work. Instead :x! or :w need to be used. |
vi Commands
editMovement
editvi can be set up on most systems to use the keyboard movement buttons, such as cursor left, page up, home, delete, etc.
<G> | Move to the last line of the file. Can be preceded by a number indicating the line to move to, <1><G> moves to the first line of the file. |
<h> | Move left one character, or cursor left. Can be preceded by a number, <5><h> moves left 5 places. |
<j> | Move one line down, or cursor down. Can be preceded by a number, <5><j> moves down 5 lines. |
<k> | Move one line up, or cursor up. Can be preceded by a number, 5k moves up 5 lines. |
<l> | Move forward one character, or cursor right. Can be preceded by a number, 5l moves right 5 places. |
<H> | Moves to the line at the top of the screen. |
<M> | Moves to the line in the middle of the screen. |
<L> | Moves to the line at the bottom of the screen. |
<-> | Moves to the first non-whitespace character of the line above. Can be preceded by a number.
|
<+> | Moves to the first non-whitespace character of the line below. Can be preceded by a number.
|
<CR> | Same as <+>. |
<|> | Must be preceded by a number. Moves to the specified column on the current line.
|
<w> | Moves to the start of the next word, which may be on the next line. |
<W> | As w but takes into account punctuation. |
<e> | Moves to the end of the current word or to the next word if between words or at the end of a word. |
<E> | As e but takes into account punctuation. |
<b> | Moves backwards to the start of the current word or to the previous word if between words or at the start of a word. |
<B> | As b but takes into account punctuation. |
<f>c | Find first occurrence of character c on the same line.
This command may be repeated using <;> or <,> (reverse direction).
|
<F>c | Same as f but backward. |
<t>c | Find the character before the first occurrence of character c on the same line. |
<T>c | Same as t but backward, placing the cursor after character c. |
<0> | Moves to the start of the current line. |
<^> | Moves to the first non-whitespace character on the current line. |
<$> | Moves to the end of the current line. |
<Ctrl-F> | Move forwards one page.
|
<Ctrl-B> | Move backwards one page.
|
<Ctrl-D> | Move forwards by half a page. |
<Ctrl-U> | Move backwards by half a page. |
<Ctrl-E> | Display one more line at the bottom of the screen. |
<Ctrl-Y> | Display one more line at the top of the screen. |
Inserting
editAll insert commands put vi into insert mode. Insert mode is terminated by the ESC key.
<i> | Enters insert mode at the cursor position. |
<I> | Enters insert mode at the start of the current line. |
<a> | Enters insert mode after the cursor, or appends. |
<A> | Enters insert mode at the end of the current line, or append to the end of the current line. |
<o> | Inserts a new line underneath the current line and then goes into insert mode. |
<O> | Inserts a new line above the current line and then goes into insert mode. |
Replacing
editr | Replaces the character underneath the cursor with the next character typed. Can be preceded by a number, 5ra replaces 5 characters with the letter a. |
R | Enters replace mode. Each time a letter is typed it replaces the one under the cursor and the cursor moves to the next character. Replace mode is terminated by the ESC key. Can be preceded by a number, 5Rab followed by ESC replaces the character under the cursor by a, the next character by b and then inserts another 4 abs. The original line is placed into the buffer, replacing any text already there. |
Deleting
editEach 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.
dd | Deletes the current line. Can be preceded by a number.
|
de | Deletes from the character underneath the cursor to the end of the word. Can be preceded by a number.
|
dE | As de but takes into account punctuation. |
dw | Deletes from the character underneath the cursor to the start of the next word. Can be preceded by a number.
|
dW | As dw but takes into account punctuation. |
db | Deletes from the left of the cursor to the start of the previous word. Can be preceded by a number.
|
dB | As db but takes into account punctuation. |
dtc | Deletes from the cursor position to before the first instance of the character.
|
dfc | Deletes from the cursor position to the first instance of the character.
|
dG | Deletes the current line and everything to the end of the file. |
d/string | Deletes from the cursor to the string, either forwards or backwards. |
D | Deletes from the cursor to the end of the line. |
d$ | Same as D. |
d^ | Deletes from the left of the cursor to the start of the line. |
x | Delete the character underneath the cursor. Can be preceded by a number.
|
X | Delete the character to the left of the cursor, but will not delete the end of line marker or any characters on the next line. Can be preceded by a number.
|
Changing
editThe 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 <ESC> 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.
C | Change from the cursor position to the end of the line. Can be preceded by a number.
|
cM | Generally the same as dMi, where M is any movement command. |
cc | Change the current line. Can be preceded by a number.
|
ce | Change the current word. Can be preceded by a number.
|
cw | Exactly the same as ce. This command is inconsistent with the usual vi moving: cw and ce is the same as dei but dwi removes also the spaces until the next word. |
ctc | Changes from the cursor position to the first instance of the character.
|
cfc | Changes from the cursor position to the first instance of the character (including the character c). |
cG | Changes from the start of the current line to the end of the file. |
s | Change the character underneath the cursor. Can be preceded by a number.
|
S | Change the entire line. Same as ddO (uppercase letter "O"). |
Cut and Paste
editThe 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.
yy | Yanks the current line into the buffer. Can be preceded by a number.
|
Y | Same as yy. |
yw | Yanks from the cursor to the start of the next word into the buffer. Can be preceded by a number.
|
p | If the buffer consists of whole lines, they are inserted after the current line. If it consists of characters only, they are inserted after the cursor. |
P | If the buffer consists of whole lines, they are inserted before the current line. If it consists of characters only, they are inserted before the cursor. |
Searching
editSearching uses regular expressions.
/pattern/ | Searches for the string, which could be a regular expression. Searching is from the cursor position downwards, stopping at the first match. If not found, it will continue from the start of the file to the cursor position. The trailing slash character is optional.
|
/pattern/+ | Goes to the line after the one containing the search string.
|
/pattern/e | Leaves the cursor on the last character of the string that pattern matched.* By adding +num or -num after e you can supply an offset in characters to where the cursor gets left. For example: /foo/e+3 will leave the cursor 3 characters past the next occurrence of foo.* By using b instead of e you can specify a character offset from the beginning of the matched string. |
/\cpattern/ | Does a case insensitive search. |
?pattern? | As /pattern/ but searches upwards. The trailing question mark character is optional. |
?pattern?- | Goes to the line above the one containing the search string.
|
<n> | Repeat last search. |
<N> | Repeat last search but in the opposite direction. |
<f>char | Search forward on the current line for the next occurrence of char. |
<F>char | Search backward on the current line for the next occurrence of char. |
<t>char | Search forward on the current line for the next occurrence of char and leave the cursor on the character before char. |
<T>char | Search backward on the current line for the next occurrence of char and leave the cursor on the character after char. |
<;> | Repeat the last f or F search. |
<,> | Similar to <;> but in the opposite direction. |
Search and Replace
editSearch and replace uses regular expressions and the Ex command :substitute (short :s) which has syntax similar to the sed utility - which is not surprising sed, Ex and w:Vi have common roots - the Ed editor.
:.s/pattern/replacement/ | Replaces the first occurrence of pattern on the current line with replacement.* If pattern contains \( and \) they are used to remember what matched between them instead of matching parenthesis characters. For example :.s/\(\d*\)-\(\d*\)/\2:\1/ could match the string 12345-6789 and substitute 6789:12345 for it. |
:.s/pattern/replacement/g | Replaces all occurrences of pattern on the current line with replacement. |
:%s/pattern/replacement/g | Replaces all occurrences of pattern in the whole file with replacement. |
:x,ys/pattern/replacement/g | Replaces all occurrences of pattern on lines x through y with replacement.* For example: :14,18s/foo/bar/g will replace all occurrences of foo with bar on lines 14 through 18.
|
The following meta-characters have special meaning in the replacement pattern:
& | Replaced by the text that matched the search pattern. |
\n | Replaced by the text matching the search pattern between \( and \), where n is in the range of 1 through 9 with \1 being replaced by the match from the first set. |
\u | Capitalize the next character (if the character is a letter). |
\l | Lowercase the next character (if the character is a letter). |
\U | Turn on capitalization mode, where all of the following characters are capitalized. |
\L | Turn on lowercase mode, where all of the following characters are lowercased. |
\E | Turn off capitalization or lowercase mode. |
\e | Turn off capitalization or lowercase mode. |
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
editMarked lines can be used when changing or deleting text.
<m>m | Mark the current line with the letter.
|
<'>m | Move to the line marked by the letter.
|
Screen Refresh
edit<Ctrl-L> | Refresh the screen. |
z<CR> | Refreshes the screen so that the current line is at the top. Can be preceded by a line number.
|
/pattern/z | Finds the line with the first occurrence of string and then refreshes the screen so that it is at the top. |
z. | Refreshes the screen so that the current line is in the middle of the screen. Can be preceded by a line number, in which case the line is at the middle. The sequence zz also has the same effect.
|
/string/z. | Finds the line with the first occurrence of string and then refreshes the screen so that it is in the middle. |
z- | Refreshes the screen so that the current line is at the bottom. Can be preceded by a line number, in which case the line is at the bottom.
|
/string/z- | Finds the line with the first occurrence of string and then refreshes the screen so that it is at the bottom. |
Others
edit<~> | Changes the case of the character underneath the cursor and moves to the next character. Can be preceded by a number, so that 5~ changes the case of 5 characters. |
<.> | Repeats the last insert or delete. Can be preceded by a number, dd followed by 5. deletes a line and then deletes another 5 lines. |
<%> | Moves the cursor to the matching bracket, any of (), [] or {}. |
<Ctrl-G> | Temporarily displays a status line at the bottom of the screen. |
:f | Same as <Ctrl-G>. |
<J> | Joins the next line to the end of the current line. Can be preceded by a number. Both 1J and 2J do the same as J.
|
<u> | Undoes the last change. |
<U> | Undoes all changes to the current line. |
<Ctrl-Z> | Puts vi into the background, that is control is returned to the operating system. In UNIX, the vi session can be returned to the foreground with fg. |
Saving and Quitting
edit<Z><Z> | Saves and quits. It is symbolic of sleep, indicating the end of work. |
:quit :q |
Quits, but only if no changes have been made. |
:quit! :q! |
Quits without saving, regardless of any changes. |
:write :w |
Saves the current file without quitting.
|
:write!filename :w!filename |
Saves to the file, overwriting any existing contents. |
:wq :write|quit |
Saves and quits. |
:exit :xit :x |
Saves and quits. |
:exit! :xit! :x! |
Used to save and quit in view. |
Files
edit:e filename | Quits the current file and starts editing the named file. |
:e + filename | Quits the current file and starts editing the named file with the cursor at the end of the file.
|
:e! | The current file is closed, all unsaved changes discarded, and the file is re-opened for editing. |
:e# | Quits the current file and starts editing the previous file. |
:n | When multiple files were quoted on the command line, start editing the next file. |
:n files | Resets the list of files for :n. The current file will be closed and the first file in the list will be opened for editing. |
:r filename | Read a file, that is insert a file.
|
vi Options
editAll 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
:set all | Displays all the current settings. |
! Set on | ! Set off | ! Meaning |
:set ignorecase :set ic |
:set noignorecase :set noic | Ignore case. Makes searching case insensitive. |
:set list | :set nolist | Shows control characters. <Ctrl-I> is tab, $ is linefeed. |
:set number :set nu |
:set nonumber :set nonu | Turns on line numbering. |
:set term | Displays the terminal type. | |
:set autoindent :set ai |
:set noautoindent :set noai | Automatically indents lines while in insert mode upon <CR>. |
:set hlsearch | :set nohlsearch | Highlights searches matched by /term/. It's a visual aide. |
ex Commands
editex commands start with :, which puts vi into last line mode, entered on the last line of the screen. Spaces within the command are ignored.
:! command | Executes the named operating system command and then returns to vi.
|
:sh | Starts up a shell. exit returns to the vi session. |
:vi | Exit last line mode and return to normal command mode. |
ex line commands
editThese commands edit lines and have the following syntax:
- No line number, meaning work on the current line.
- With %, meaning work on all lines.
- 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.
co | Copy, followed by the line position to copy to.
|
d | Delete.
|
m | Move, followed by the line position to move to.
|
Mapping / Remapping vi Commands
edit:map | Create new command or overwrite existing in vi command mode.
|
:map! | Create new command in both command and insert mode.
|
External link
edit