Learning the vi Editor/vi Reference

From Wikibooks, open books for an open world
< Learning the vi Editor
Jump to: navigation, search

Learning the vi Editor/TOC

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 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[edit]

vi 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 +5 myfile opens myfile at line 5.
  • vi + myfile opens myfile at the last 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 +/"search string"/ myfile opens myfile at the first line containing search string.
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[edit]

Movement[edit]

vi 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.
  • 10- moves up 10 lines.
<+> Moves to the first non-whitespace character of the line below. Can be preceded by a number.
  • 10+ moves down 10 lines.
<CR> Same as <+>.
<> Must be preceded by a number. Moves to the specified column on the current line.
  • 10| moves to column 10.

<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).

  • <3><f><x> moves forward on the third occurrence of x (if present).
Same as <f><x><;><;>
<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.
  • 5<Ctrl-F> moves forwards five pages.
<Ctrl-B> Move backwards one page.
  • 5<Ctrl-B> moves backwards five pages.
<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[edit]

All 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[edit]

r 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[edit]

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.

dd Deletes the current line. Can be preceded by a number.
  • 5dd deletes five lines. d5d is the same as 5dd.
de Deletes from the character underneath the cursor to the end of the word. Can be preceded by a number.
  • 5de deletes five words. d5e is the same as 5de.
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.
  • 5dw deletes five words. d5w is the same as 5dw.
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.
  • 5db deletes five words to the left of the cursor.
dB As db but takes into account punctuation.
dtc Deletes from the cursor position to before the first instance of the character.
  • dta deletes text up and to, but not including, the first letter 'a'.
dfc Deletes from the cursor position to the first instance of the character.
  • dfa deletes text up and to, and including, the first letter 'a'.
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.
  • 5x deletes the character underneath the cursor and the next 4 characters.
  • xp swaps the character underneath the cursor with the one to the right of it.
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.
  • 5X deletes 5 characters to the left of the cursor.

Changing[edit]

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 <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.
  • 5C changes 5 lines, the current line and the next 4 lines.

cM Generally the same as dMi, where M is any movement command.
cc Change the current line. Can be preceded by a number.
  • 5cc changes 5 lines, the current line and the next 4 lines.
ce Change the current word. Can be preceded by a number.
  • 5ce changes five words. c5e is the same as 5ce.
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.
  • cta changes text up and to, but not including, the first letter 'a'.
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.
  • 5s changes 5 characters, the one under the cursor and the next 4.
S Change the entire line. Same as ddO (uppercase letter "O").

Cut and Paste[edit]

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.

yy Yanks the current line into the buffer. Can be preceded by a number.
  • 5yy yanks five lines.
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.
  • 5yw yanks five words.

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[edit]

Searching 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.
  • /abc/ seaches for the first occurrence of abc.
/pattern/+ Goes to the line after the one containing the search string.
  • /abc/+3 goes to the third line after the one containing abc.
/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.
  • ?abc?-3 goes to the third line above the one containing abc.
<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.

Search and Replace[edit]

Search 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 character . can be used to indicate the current line and the character $ can be used to indicate the last line. For example: :.,$s/foo/bar/g will replace all occurrences of foo with bar on the current line through the end of the file.

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[edit]

Marked lines can be used when changing or deleting text.

<m>m Mark the current line with the letter.
  • <m><a> marks the current line with the letter a.
<'>m Move to the line marked by the letter.
  • <'><a> moves to the line marked by a.

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.
  • 35z refreshes the screen so that line 35 is at the top.
/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.
  • 35z. refreshes the screen so that line 35 is in the middle.
/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.
  • 35z- refreshes the screen so that line 35 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.
  • 3J joins three lines together, the current line and the next two lines.
<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.
  • {Vi/Ex|:write!}} myfile saves to the file called myfile.
: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 +5 myfile quits the current file and begins editing myfile at line 5.
: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.
  • :r myfile inserts the file named myfile after the cursor.
  • :5r myfile inserts the file after line 5.

vi Options[edit]

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
: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[edit]

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.

:! command Executes the named operating system command and then returns to vi.
  • :! ls runs the UNIX ls command.
: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[edit]

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.


co Copy, followed by the line position to copy to.
  • :co 5 copies the current line and places it after line 5.
    • :1,3 co 4 copies lines 1 to 3 and places after line 4.
d Delete.
  • :d deletes the current line.
    • :.,.+5d delete the current line and the next 5 lines.
    • :%d deletes all lines.
m Move, followed by the line position to move to.
  • :m 10 moves the current line and places it after line 10.
    • :1,3 m 4 moves lines 1 to 3 and places after line 4.

Mapping / Remapping vi Commands[edit]

:map Create new command or overwrite existing in vi command mode.
  • :map v i--<Ctrl-[> new command v will insert -- and return to command mode. <Ctrl-[> is the escape character typed as <CTRL-V><ESC>.
:map! Create new command in both command and insert mode.
  • :map! ;r <Ctrl-[> typing ;r in insert mode will return to command mode.

External link[edit]


Learning the vi Editor/TOC