The Regents of the University of California. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by the University of
California, Berkeley and its contributors.
4. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
.nr LL 6.5i .nr FL 6.5i Vi Command & Function Reference .AU CB 2675 Alan P.W. Hewett Revised for version 2.12 by Mark Horton .CB Author's Disclaimer
This document does not claim to be 100% complete. There are a few commands listed in the original document that I was unable to test either because I do not speak lisp, because they required programs we don't have, or because I wasn't able to make them work. In these cases I left the command out. The commands listed in this document have been tried and are known to work. It is expected that prospective users of this document will read it once to get the flavor of everything that vi can do and then use it as a reference document. Experimentation is recommended. If you don't understand a command, try it and see what happens.
[Note: In revising this document, I have attempted to make it completely reflect version 2.12 of vi . It does not attempt to document the VAX version (version 3), but with one or two exceptions (wrapmargin, arrow keys) everything said about 2.12 should apply to 3.1. "Mark Horton" ] Notation
[option] is used to denote optional parts of a command. Many vi commands have an optional count. [cnt] means that an optional number may precede the command to multiply or iterate the command. {variable item} is used to denote parts of the command which must appear, but can take a number of different values. <character [-character]> means that the character or one of the characters in the range described between the two angle brackets is to be typed. For example <esc> means the escape key is to be typed. <a-z> means that a lower case letter is to be typed. ^<character> means that the character is to be typed as a control character, that is, with the <cntl> key held down while simultaneously typing the specified character. In this document control characters will be denoted using the upper case character, but ^<uppercase chr> and ^<lowercase chr> are equivalent. That is, for example, <^D> is equal to <^d>. The most common character abbreviations used in this list are as follows: .VL 8
To run vi the shell variable TERM must be defined and exported to your environment. How you do this depends on which shell you are using. You can tell which shell you have by the character it prompts you for commands with. The Bourne shell prompts with `$', and the C shell prompts with `%'. For these examples, we will suppose that you are using an HP 2621 terminal, whose termcap name is ``2621''. Bourne Shell
To manually set your terminal type to 2621 you would type: TERM=2621 export TERM
There are various ways of having this automatically or semi-automatically done when you log in. Suppose you usually dial in on a 2621. You want to tell this to the machine, but still have it work when you use a hardwired terminal. The recommended way, if you have the tset program, is to use the sequence tset -s -d 2621 > tset$$ . tset$$ rm tset$$ in your .login (for csh) or the same thing using `.' instead of `source' in your .profile (for sh). The above line says that if you are dialing in you are on a 2621, but if you are on a hardwired terminal it figures out your terminal type from an on-line list. The C Shell
To manually set your terminal type to 2621 you would type: setenv TERM 2621
There are various ways of having this automatically or semi-automatically done when you log in. Suppose you usually dial in on a 2621. You want to tell this to the machine, but still have it work when you use a hardwired terminal. The recommended way, if you have the tset program, is to use the sequence tset -s -d 2621 > tset$$ source tset$$ rm tset$$ in your .login.* .FS * On a version 6 system without environments, the invocation of tset is simpler, just add the line ``tset -d 2621'' to your .login or .profile. .FE The above line says that if you are dialing in you are on a 2621, but if you are on a hardwired terminal it figures out your terminal type from an on-line list. Normal Commands
Vi is a visual editor with a window on the file. What you see on the screen is vi's current notion of what your file will contain, (at this point in the file), when it is written out. Most commands do not cause any change in the screen until the complete command is typed. Should you get confused while typing a command, you can abort the command by typing an <del> character. You will know you are back to command level when you hear a <bell>. Usually typing an <esc> will produce the same result. When vi gets an improperly formatted command it rings the <bell>. Following are the vi commands broken down by function. Entry and Exit
To enter vi on a particular file , type vi file The file will be read in and the cursor will be placed at the beginning of the first line. The first screenfull of the file will be displayed on the terminal.
To get out of the editor, type ZZ If you are in some special mode, such as input mode or the middle of a multi-keystroke command, it may be necessary to type <esc> first. Cursor and Page Motion
.VL 16 NOTE: The arrow keys (see the next four commands) on certain kinds of terminals will not work with the PDP-11 version of vi. The control versions or the hjkl versions will work on any terminal. Experienced users prefer the hjkl keys because they are always right under their fingers. Beginners often prefer the arrow keys, since they do not require memorization of which hjkl key is which. The mnemonic value of hjkl is clear from looking at the keyboard of an adm3a.
Move the cursor to the left one character. Cursor stops at the left margin of the page. If cnt is given, these commands move that many spaces.
Move down one line. Moving off the screen scrolls the window to force a new line onto the screen. Mnemonic: Next
Move up one line. Moving off the top of the screen forces new text onto the screen. Mnemonic: Previous
Move to the right one character. Cursor will not go beyond the end of the line.
Move the cursor to the line specified. Of particular use are the sequences "1G" and "G", which move the cursor to the beginning and the end of the file respectively. Mnemonic: Go-to
NOTE: The next four commands (^D, ^U, ^F, ^B) are not true motion commands, in that they cannot be used as the object of commands such as delete or change.
The following commands allow you to search for items in a file. .VL 16
Search forward for the next occurrence of "string". Wrap around at the end of the file does occur. The final </> is not required.
Search backwards for the next occurrence of "string". If a count is specified, the count becomes the new window size. Wrap around at the beginning of the file does occur. The final <?> is not required.
The following commands allow for the insertion of text. All multicharacter text insertions are terminated with an <esc> character. The last change can always be undone by typing a u. The text insert in insertion mode can contain newlines. .VL 16
The following commands allow the user to delete text in various ways. All changes can always be undone by typing the u command. .VL 16
Deletes one or more occurrences of the specified motion. Any motion from sections 4.1 and 4.2 can be used here. The d can be stuttered (e.g. [cnt]dd) to delete cnt lines. .LE Text Replacement
The following commands allow the user to simultaneously delete and insert new text. All such actions can be undone by typing u following the command. .VL 16
Change the specified "motion" by replacing it with the insertion text. A "$" will appear at the end of the last item that is being deleted unless the deletion involves whole lines. Motion's can be any motion from sections 4.1 or 4.2. Stuttering the c (e.g. [cnt]cc) changes cnt lines. .LE Moving Text
Vi provides a number of ways of moving chunks of text around. There are nine buffers into which each piece of text which is deleted or "yanked" is put in addition to the "undo" buffer. The most recent deletion or yank is in the "undo" buffer and also usually in buffer 1, the next most recent in buffer 2, and so forth. Each new deletion pushes down all the older deletions. Deletions older than 9 disappear. There is also a set of named registers, a-z, into which text can optionally be placed. If any delete or replacement type command is preceded by "<a-z>, that named buffer will contain the text deleted after the command is executed. For example, "a3dd will delete three lines starting at the current line and put them in buffer "a.* .FS * Referring to an upper case letter as a buffer name (A-Z) is the same as referring to the lower case letter, except that text placed in such a buffer is appended to it instead of replacing it. .FE There are two more basic commands and some variations useful in getting and putting text into a file. .VL 16
Vi has a number of miscellaneous commands that are very useful. They are: .VL 16
Any UNIX filter (e.g. command that reads the standard input and outputs something to the standard output) can be sent a section of the current file and have the output of the command replace the original text. Useful examples are programs like cb, sort, and nroff. For instance, using sort it would be possible to sort a section of the current file into a new list. Using !! means take a line or lines starting at the line the cursor is currently on and pass them to the UNIX command. NOTE: To just escape to the shell for one command, use :!{cmd}<nl>, see section 5.
There are some characters that have special meanings during insert modes. They are: .VL 16
Typing a ":" during command mode causes vi to put the cursor at the bottom on the screen in preparation for a command. In the ":" mode, vi can be given most ed commands. It is also from this mode that you exit from vi or switch to different files. All commands of this variety are terminated by a <nl>, <cr>, or <esc>. .VL 16
Vi takes the value of $TERM and looks up the characteristics of that terminal in the file /etc/termcap. If you don't know vi's name for the terminal you are working on, look in /etc/termcap.
When vi starts, it attempts to read the variable EXINIT from your environment.* If that exists, it takes the values in it as the default values for certain of its internal constants. See the section on "Set Values" for further details. If EXINIT doesn't exist you will get all the normal defaults. .FS * On version 6 systems Instead of EXINIT, put the startup commands in the file .exrc in your home directory. .FE
Should you inadvertently hang up the phone while inside vi , or should the computer crash, all may not be lost. Upon returning to the system, type: vi -r file This will normally recover the file. If there is more than one temporary file for a specific file name, vi recovers the newest one. You can get an older version by recovering the file more than once. The command "vi -r" without a file name gives you the list of files that were saved in the last system crash (but not the file just saved when the phone was hung up). Set Commands
Vi has a number of internal variables and switches which can be set to achieve special affects. These options come in three forms, those that are switches, which toggle from off to on and back, those that require a numeric value, and those that require an alphanumeric string value. The toggle options are set by a command of the form: :set option<nl> and turned off with the command: :set nooption<nl> Commands requiring a value are set with a command of the form: :set option=value<nl> To display the value of a specific option type: :set option?<nl> To display only those that you have changed type: :set<nl> and to display the long table of all the settable parameters and their current values type: :set all<nl>
Most of the options have a long form and an abbreviation. Both are listed in the following table as well as the normal default value.
To arrange to have values other than the default used every time you enter vi , place the appropriate set command in EXINIT in your environment, e.g. EXINIT='set ai aw terse sh=/bin/csh' export EXINIT or setenv EXINIT 'set ai aw terse sh=/bin/csh' for sh and csh , respectively. These are usually placed in your .profile or .login. If you are running a system without environments (such as version 6) you can place the set command in the file .exrc in your home directory. .VL 16
When in autoindent mode, vi helps you indent code by starting each line in the same column as the preceding line. Tabbing to the right with <tab> or <^T> will move this boundary to the right, and it can be moved to the left with <^D>.
Causes the current line to be printed after each ex text modifying command. This is not of much interest in the normal vi visual mode.
Autowrite causes an automatic write to be done if there are unsaved changes before certain commands which change files or otherwise interact with the outside world. These commands are :!, :tag, :next, :rewind, ^^, and ^].
Causes all control characters except <tab>, <nl>, and <ff> to be discarded.
This is the directory in which vi puts its temporary file.
Error messages are preceded by a <bell>.
This option contains the value of hardware tabs in your terminal, or of software tabs expanded by the Unix system.
All upper case characters are mapped to lower case in regular expression matching.
Autoindent for lisp code. The commands ( ) [[ and ]] are modified appropriately to affect s-expressions and functions.
All printed lines have the <tab> and <nl> characters displayed visually.
Enable the metacharacters for matching. These include . * < > [string] [^string] and [<chr>-<chr>].
Each line is displayed with its line number.
When set, prevents entering open or visual modes from ex or edit. Not of interest from vi.
Basically of use only when using the ex capabilities. This option prevents automatic <cr>s from taking place, and speeds up output of indented lines, at the expense of losing typeahead on some versions of UNIX.
Each pair of characters in the string indicate nroff macros which are to be treated as the beginning of a paragraph for the { and } commands. The default string is for the -ms and -mm macros. To indicate one letter nroff macros, such as .P or .H, quote a space in for the second character position. For example: :set paragraphs=P\e bp<nl> would cause vi to consider .P and .bp as paragraph delimiters.
In ex command mode the prompt character : will be printed when ex is waiting for a command. This is not of interest from vi.
On dumb terminals, force the screen to always be up to date, by sending great amounts of output. Useful only at high speeds.
This sets the threshold for the number of lines modified. When more than this number of lines are modified, removed, or yanked, vi will report the number of lines changed at the bottom of the screen.
This is the number of lines that the screen scrolls up or down when using the <^U> and <^D> commands.
Each two character pair of this string specify nroff macro names which are to be treated as the beginning of a section by the ]] and [[ commands. The default string is for the -ms and -mm macros. To enter one letter nroff macros, use a quoted space as the second character. See paragraphs for a fuller explanation.
This is the name of the sh to be used for "escaped" commands.
This is the number of spaces that a <^T> or <^D> will move over for indenting, and the amount < and > shift by.
When a ) or } is typed, show the matching ( or { by moving the cursor to it for one second if it is on the current screen.
On terminals that are slow and unintelligent, this option prevents the updating of the screen some of the time to improve speed.
<tab>s are expanded to boundaries that are multiples of this value.
If nonzero, tag names are only significant to this many characters.
This is the terminal and controls the visual displays. It cannot be changed when in "visual" mode, you have to Q to command mode, type a set term command, and do ``vi.'' to get back into visual. Or exit vi, fix $TERM, and reenter. The definitions that drive a particular terminal type are found in the file /etc/termcap.
When set, the error diagnostics are short.
The user is warned if she/he tries to escape to the shell without writing out the current changes.
This is the number of lines in the window whenever vi must redraw an entire screen. It is useful to make this size smaller if you are on a slow line.
These set window, but only within the corresponding speed ranges. They are useful in an EXINIT to fine tune window sizes. For example, set w300=4 w1200=12 causes a 4 lines window at speed up to 600 baud, a 12 line window at 1200 baud, and a full screen (the default) at over 1200 baud.
Searches will wrap around the end of the file when is option is set. When it is off, the search will terminate when it reaches the end or the beginning of the file.
Vi will automatically insert a <nl> when it finds a natural break point (usually a <sp> between words) that occurs within "wm" spaces of the right margin. Therefore with "wm=0" the option is off. Setting it to 10 would mean that any time you are within 10 spaces of the right margin vi would be looking for a <sp> or <tab> which it could replace with a <nl>. This is convenient for people who forget to look at the screen while they type. (In version 3, wrapmargin behaves more like nroff, in that the boundary specified by the distance from the right edge of the screen is taken as the rightmost edge of the area where a break is allowed, instead of the leftmost edge.)
Vi normally makes a number of checks before it writes out a file. This prevents the user from inadvertently destroying a file. When the "writeany" option is enabled, vi no longer makes these checks. .LE