Custom Search

vi reference

Being a (fairly) complete reference guide to the king of editors. This is not a tutorial, but a compact and usable listing of command and insert mode keys, ex commands and options.

Preamble

Warning: some vi versions don't support the more esoteric features described in this document. You can edit/redistribute this document freely, as long as you don't make false claims on original authorship.

Author: Maarten Litmaath <maart@nat.vu.nl>
Version: 8
HTML version by: John Arundel <john@splange.freeserve.co.uk>

Contributors

Rich Salz <rsalz@bbn.com>
Eamonn McManus <emcmanus@cs.tcd.ie>
Diomidis Spinellis <diomidis%ecrcvax.uucp@pyramid.pyramid.com>
Blair P. Houghton <bph@buengc.bu.edu>
David J. MacKenzie <djm@wam.umd.edu>
Kevin Carothers <kevin@ttidca.tti.com>
Dan Mercer <mercer@ncrcce.StPaul.NCR.COM>
Paul Quare <pq@r2.cs.man.ac.uk>
Dave Beyerl <att!ihlpl!db21>
Lee Sailer <UH2@psuvm.psu.edu>
David Gast <gast@cs.ucla.edu>

Legenda

 default values 1 <*> *' must not be taken literally [*] *' is optional ^X X space carriage return linefeed horizontal tab escape your erase character your kill character your interrupt character an element in the range N number (*' = allowed, -' = not appropriate) CHAR char unequal to | WORD word followed by ||

Move commands

N Command Meaning
* h or ^H or <erase> <erase> <*> chars to the left.
* j or <lf> or ^N <lf> <*> lines downward.
* l or <sp> <sp> <*> chars to the right.
* k or ^P <*> lines upward.

Commands while in append/change mode

 ^@ If typed as the first character of the insertion, it is replaced with the previous text inserted (max. 128 chars), after which the insertion is terminated. ^V Deprive the next char of its special meaning (e.g. ). ^D One shiftwidth to the left, but only if nothing else has been typed on the line. 0^D Remove all indentation on the current line (there must be no other chars on the line). ^^D Idem, but it is restored on the next line. ^T One shiftwidth to the right, but only if nothing else has been typed on the line. ^H or One char back. ^W One word back. Back to the begin of the change on the current line. Like (but you get a beep as well).

Writing, editing other files, and quitting vi

In :' ex' commands - if not the first CHAR on the line - %' denotes the current file, #' is a synonym for the alternate file (which normally is the previous file). As first CHAR on the line %' is a shorthand for 1,$'. Marks can be used for line numbers too: '<a-z>. In the :w'|:f'|:cd'|:e'|:n' commands shell meta-characters can be used.  :q Quit vi, unless the buffer has been changed. :q! Quit vi without writing. ^Z Suspend vi. :w Write the file. :w Write to the file . :w >> Append the buffer to the file . :w! Overwrite the file . :x,y w Write lines x through y to the file . :wq Write the file and quit vi; some versions quit even if the write was unsuccessful! Use ZZ' instead. ZZ Write if the buffer has been changed, and quit vi. If you have invoked vi with the -r' option, you'd better write the file explicitly (w' or w!'), or quit the editor explicitly (q!') if you don't want to overwrite the file - some versions of vi don't handle the recover' option very well. :x [] Idem [but write to ]. :x! [] :w![]' and :q'. :pre Preserve the file - the buffer is saved as if the system had just crashed; for emergencies, when a :w' command has failed and you don't know how to save your work (see vi -r'). :f Set the current filename to . :cd [] Set the working directory to (default home directory). :cd! [] Idem, but don't save changes. :e [+] Edit another file without quitting vi - the buffers are not changed (except the undo buffer), so text can be copied from one file to another this way. [Execute the ex' command (default $') when the new file has been read into the buffer.] must contain no or . See vi startup'. :e! [+] Idem, without writing the current buffer. ^^ Edit the alternate (normally the previous) file. :rew Rewind the argument list, edit the first file. :rew! Idem, without writing the current buffer. :n [+] [] Edit next file or specify a new argument list. :n! [+] [] Idem, without writing the current buffer. :args Give the argument list, with the current file between [' and ]'.

Display commands

 ^G Give file name, status, current line number and relative position. ^L Refresh the screen (sometimes ^P' or ^R'). ^R Sometimes vi replaces a deleted line by a @', to be deleted by ^R' (see option redraw'). [*]^E Expose <*> more lines at bottom, cursor stays put (if possible). [*]^Y Expose <*> more lines at top, cursor stays put (if possible). [*]^D Scroll <*> lines downward (default the number of the previous scroll; initialization: half a page). [*]^U Scroll <*> lines upward (default the number of the previous scroll; initialization: half a page). [*]^F <*> pages forward. [*]^B <*> pages backward (in older versions ^B' only works without count).

If in the next commands the field <wi> is present, the windowsize will change to <wi>. The window will always be displayed at the bottom of the screen.

 [*]z[wi] Put line <*> at the top of the window (default the current line). [*]z[wi]+ Put line <*> at the top of the window (default the first line of the next page). [*]z[wi]- Put line <*> at the bottom of the window (default the current line). [*]z[wi]^ Put line <*> at the bottom of the window (default the last line of the previous page). [*]z[wi]. Put line <*> in the centre of the window (default the current line).

Mapping and abbreviation

When mapping take a look at the options to' and remap' (below).

 :map is interpreted as , e.g. :map ^C :!cc %^V' to invoke cc' (the C compiler) from within the editor (vi replaces %' with the current file name). :map Show all mappings. :unmap Deprive of its mapping. When vi complains about non-mapped macros (whereas no typos have been made), first do something like :map Z', followed by :unmap ' (Z' must not be a macro itself), or switch to ex' mode first with Q'. :map! Mapping in append mode, e.g. :map! \be begin^Vend;^VO'. When in append mode is preceded by ^V', no mapping is done. :map! Show all append mode mappings. :unmap! Deprive of its mapping (see :unmap'). :ab Whenever in append mode is preceded and followed by a breakpoint (e.g. or ,'), it is interpreted as , e.g. :ab ^P procedure'. A ^V' immediately following inhibits expansion. :ab Show all abbreviations. :unab Do not consider an abbreviation anymore (see :unmap'). @ Consider the contents of the named register a command, e.g.: o0^D:s/wrong/good/"zdd Explanation: o - open a new line 0^D - remove indentation :s/wrong/good/ - this input text is an ex' substitute command - finish the input "zdd - delete the line just created into register z' Now you can type @z' to replace wrong' with good' on the current line. @@ Repeat last register command.

Switch and shell commands

 Q or ^\ or Switch from vi to ex'. : An ex' command can be given. :vi Switch from ex' to vi. :sh Execute a subshell, back to vi by ^D'. :[x,y]! Execute a shell [on lines x through y; these lines will serve as input for and will be replaced by its standard output]. :[x,y]!! [] Repeat last shell command [and append ]. :[x,y]! ! [] Use the previous command (the second !') in a new command. [*]! The shell executes , with as standard input the lines described by <*>, next the standard output replaces those lines (think of cb', sort', nroff', etc.). [*]!! Append to the last and execute it, using the lines described by the current <*>. [*]!! Give <*> lines as standard input to the shell , next let the standard output replace those lines. [*]!!! [] Use the previous [and append to it]. :x,y w ! Let lines x to y be standard input for (notice the between the w' and the !'). :r! Put the output of onto a new line. :r Read the file into the buffer.

vi startup

 vi [] Edit the files, start with the first page of the first file.

The editor can be initialized by the shell variable EXINIT', which looks like:

    EXINIT='<cmd>|<cmd>|...'
<cmd>: set options
map ...
ab ...
export EXINIT (in the Bourne shell)


However, the list of initializations can also be put into a file. If this file is located in your home directory, and is named .exrc' AND the variable EXINIT' is NOT set, the list will be executed automatically at startup time. However, vi will always execute the contents of a .exrc' in the current directory, if you own the file. Else you have to give the execute (source') command yourself:

    :so file


In a .exrc' file a comment is introduced with a double quote character: the rest of the line is ignored. Exception: if the last command on the line is a map[!]' or ab' command or a shell escape, a trailing comment is not recognized, but considered part of the command.

On-line initializations can be given with vi +<cmd> file', e.g.:

vi +x file


vi +/<string> file


You can start at a particular tag with:

vi -t <tag>


Start in the right file in the right place.

Sometimes (e.g. if the system crashed while you were editing) it is possible to recover files lost in the editor by vi -r file'. A plain vi -r' command shows the files you can recover. If you just want to view a file by using vi, and you want to avoid any change, instead of vi you can use the view' or vi -R' command: the option readonly' will be set automatically (with :w!' you can override this option).

The most important options

 ai autoindent - In append mode after a the cursor will move directly below the first CHAR on the previous line. However, if the option lisp' is set, the cursor will align at the first argument to the last open list. aw autowrite - Write at every shell escape (useful when compiling from within vi). dir= directory - The directory for vi to make temporary files (default /tmp'). eb errorbells - Beeps when you goof (not on every terminal). ic ignorecase - No distinction between upper and lower cases when searching. lisp Redefine the following commands: (', )' - move backward (forward) over S-expressions {', }' - idem, but don't stop at atoms [[', ]]' - go to previous (next) line beginning with a (' See option ai'. list is shown as $', as ^I'. magic If this option is set (default), the chars .', [' and *' have special meanings within search and ex' substitute commands. To deprive such a char of its special function it must be preceded by a \'. If the option is turned off it's just the other way around. Meta-chars: ^ - must begin the line$ - must end the line . - matches any char [a-z] - matches any char in the range [^a-z] - any char not in the range [] - matches any char in [^] - any char not in * - 0 or more s \< - must begin a word \> - must end a word modeline When you read an existing file into the buffer, and this option is set, the first and last 5 lines are checked for editing commands in the following form: vi:set options|map ...|ab ...|!...: Instead of a can be used, instead of vi' there can be ex'. Warning: this option could have nasty results if you edit a file containing strange' modelines. nu number - Numbers before the lines. para= paragraphs - Every pair of chars in is considered a paragraph delimiter nroff macro (for {' and }'). A preceded by a \' indicates the previous char is a single letter macro. :set para=P\ bp' introduces .P' and .bp' as paragraph delimiters. Empty lines and section boundaries are paragraph boundaries too. redraw The screen remains up to date. remap If on (default), macros are repeatedly expanded until they are unchanged. Example: if o' is mapped to A', and A' is mapped to I', then o' will map to I' if remap' is set, else it will map to A'. report=<*> Vi reports whenever e.g. a delete or yank command affects <*> or more lines. ro readonly - The file is not to be changed. However, :w!' will override this option. sect= sections - Gives the section delimiters (for [[' and ]]'); see option para'. A {' beginning a line also starts a section (as in C functions). sh= shell - The program to be used for shell escapes (default \$SHELL' (default /bin/sh')). sw=<*> shiftwidth - Gives the shiftwidth (default 8 positions). sm showmatch - Whenever you append a )', vi shows its match if it's on the same page; also with {' and }'. If there's no match at all, vi will beep. taglength=<*> The number of significant characters in tags (0 = unlimited). tags= The space-separated list of tags files. terse Short error messages. to timeout - If this option is set, append mode mappings will be interpreted only if they're typed fast enough. ts=<*> tabstop - The length of a ; warning: this is only IN the editor, outside of it s have their normal length (default 8 positions). wa writeany - No checks when writing (dangerous). warn Warn you when you try to quit without writing. wi=<*> window - The default number of lines vi shows. wm=<*> wrapmargin - In append mode vi automatically puts a whenever there is a or within columns from the right margin (0 = don't put a in the file, yet put it on the screen). ws wrapscan - When searching, the end is considered `stuck' to the begin of the file.

Setting and showing options

 :set

To maintain integrity, this document was written using vi.