Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ex(1)

edit(1)

expreserve(1)

terminfo(4)

environ(5)

lang(5)

regexp(5)

vi(1)

NAME

vi − screen-oriented (visual) display editor

SYNOPSIS

vi [-] [-v] [-r] [-R] [+ command] [-l] [-t tag] [-V] [-w size] [-x] [file ...]

view [-] [-v] [-r] [-R] [+ command] [-l] [-t tag] [-V] [-w size] [-x] [file ...]

vedit [-] [-v] [-r] [-R] [+ command] [-l] [-t tag] [-V] [-w size] [-x] [file ...]

DESCRIPTION

vi (visual) is a display-oriented text editor that is based on the underlying ex line editor (see ex(1)).  It is possible to switch back and forth between the two, and to execute ex commands from within vi. 

When using vi, the terminal screen acts as a window into the file being edited.  Changes made to the file are reflected in the screen display; the position of the cursor on the screen indicates the position within the file. 

The environment variable TERM must give the terminal type, and the terminal must be defined in the terminfo(4) database. As with ex, editor initialization scripts can be placed in the environment variable EXINIT, or in the file .exrc in the current or home directory. 

The view invocation is identical to vi except that the readonly editor option is set. 

vedit is intended for beginners.  It sets the report editor option to 1, and sets the showmode and novice editor options.  Otherwise it is the same as vi.  These settings help make it easier for some beginners to learn the editor. 

Options

vi recognizes the following command-line options:

- Suppress all interactive user feedback.  This is useful in processing editor scripts. 

-v Invoke vi (this option is intended for use with ex, and has no effect on vi). 

-r Recover file(s) after an editor or system crash. If no file is specified, a list of all saved files is printed.  You must be owner of the saved file in order to recover it (super-user cannot recover files owned by other users). 

-R Set "read-only" mode to prevent overwriting the file inadvertently. 

+command Begin editing by executing the specified ex search or positioning command.

-l lisp mode; indents appropriately for lisp code; the (), {}, [[, and ]] commands in vi are modified to have meaning for lisp.

-t tag Edit the file containing tag, and position the editor at its definition (see the tag command in ex(1) and ctags(1)).

-V Verbose mode; editor commands are displayed as they are executed when input from a .exrc file or a source file (see the source command in ex(1)).

-wsize Set the value of the window editor option to size.

-x Encryption mode; the user is prompted for a key to allow for the creation or editing of an encrypted file (see the crypt command in ex (1)). 

See ex(1) for the complete description of ex.  Only the visual mode of the editor is described here. 

When invoked, vi is in command mode; input mode is initiated by several commands used to insert or change text.  In input mode, ESC (escape) is used to leave input mode; however, two consecutive ESC characters are required to leave input mode if the doubleescape editor option is set (see ex(1)). In command mode, ESC is used to cancel a partial command; the terminal bell sounds if the editor is not in input mode and there is no partially entered command. 

The last (bottom) line of the screen is used to echo the input for search commands (/ and ?), ex commands (:), and system commands (!).  It is also used to report errors or print other messages. 

Receipt of SIGINT during text input or during the input of a command on the bottom line terminates the input (or cancels the command) and returns the editor to command mode.  During command mode, SIGINT causes the bell to be sounded; in general the bell indicates an error (such as an unrecognized key). 

Lines displayed on the screen containing only a ~ indicate that the last line above them is the last line of the file (the ~ lines are past the end of the file).  Terminals with limited local intelligence might display lines on the screen marked with an @: these indicate space on the screen not corresponding to lines in the file.  (These lines can be removed by entering a ^R, forcing the editor to retype the screen without these holes.) 

If the system crashes or vi aborts due to an internal error or unexpected signal, vi attempts to preserve the buffer if any unwritten changes were made.  Use the -r command line option to retrieve the saved changes. 

vi supports the SIGWINCH signal, and redraws the screen in response to window-size changes. 

Command Summary

Most commands accept a preceding number as an argument, either to give a size or position (for display or movement commands), or as a repeat count (for commands that change text).  For simplicity, this optional argument is referred to as count when its effect is described. 

The following operators can be followed by a movement command to specify an extent of text to be affected: c, d, y, <, >, !, and =.  The region specified begins at the current cursor position and ends just prior to the cursor position indicated by the move.  If the command operates on lines only, all the lines that fall partly or wholly within this region are affected.  Otherwise the exact marked region is affected. 

In the following listing, control characters are indicated in the form ^X, which represents Ctrl-X.  White space is defined to be the characters space, tab, and alternative space.  Alternative space is the first character of the langinfo(3C) ALT_PUNCT item for the language specified by the LANG environment variable (see environ(5)).

Unless otherwise specified, the commands are interpreted in command mode and have no special effect in input mode. 

^B Scroll backward to display the previous window of text.  A count specifies the number of windows to go back.  Two lines of overlap are kept if possible. 

^D Scroll forward a half-window of text.  A count gives the number of (logical) lines to scroll, and is remembered for future ^D and ^U commands. 

In input mode, ^D backs shiftwidth spaces over the indentation provided by autoindent or ^T.  White space inserted by ^T at other than the beginning of a line cannot be backed over using ^D.  A ^ followed by a ^D removes all indentation (for the current and subsequent input lines of the current input mode) until new indentation is established by inserting leading white space, either by direct input or by using ^T. 

^E Scroll forward one line, leaving the cursor where it is if possible. 

^F Scroll forward to display the window of text following the current one.  A count specifies the number of windows to advance.  Two lines of overlap are kept if possible. 

^G Print the current file name and other information, including the number of lines and the current position (equivalent to the ex command f). 

^H Move one space to the left (stops at the left margin).  A count specifies the number of spaces to back up (same as h). 

In input mode, ^H returns the cursor to the last input character without erasing it. 

^J Move the cursor down one line in the same column.  A count specifies the number of lines to move down (same as ^N and j). 

^L Clear and redraw the screen (use when the screen is scrambled for any reason). 

^M Move to the first non-whitespace character in the next line.  A count specifies the number of lines to advance. 

^N Same as ^J and j. 

^P Move the cursor up one line in the same column.  A count specifies the number of lines to move up (same as k). 

^R Redraw the current screen, eliminating the false lines marked with @ (which do not correspond to actual lines in the file). 

^T In input mode, ^T inserts shiftwidth white space.  If at the beginning of the line, this inserted space can only be backed over using ^D. 

^U Scroll up a half-window of text.  A count gives the number of (logical) lines to scroll, and is remembered for future ^D and ^U commands. 

^V In input mode, ^V quotes the next character to permit the insertion of special characters (including ESC) into the file. 

^W In input mode, ^W backs up one word; the deleted characters remain on the display. 

^Y Scroll backward one line, leaving the cursor where it is, if possible. 

^[ Cancel a partially formed command; ^[ sounds the bell if there is no partially formed command. 

In input mode, ^[ terminates input mode.  However, two consecutive ESC characters are required to terminate input mode if the doubleescape editor option is set (see ex(1)).

When entering a command on the bottom line of the screen (ex command line or search pattern with \ or ?), terminate input and execute command. 

On many terminals, ^[ can be entered by pressing the ESC or ESCAPE key. 

^\ Exit vi and enter ex command mode.  If in input mode, terminate the input first. 

^] Take the word after the cursor as a tag and execute the tag editor command (see ex(1)).

^^ Return to the previous file (equivalent to :ex #). 

space Move one space to the right (stops at the end of the line).  A count specifies the number of spaces to go forward (same as l). 

erase Erase, where erase is the user-designated erase character (see stty(1)). Same as ^H.

kill Kill, where kill is the user-designated kill character (see stty(1)). In input mode, kill backs up to the beginning of the current input line without erasing the line from the screen display. 

susp Suspend the editor session and return to the calling shell, where susp is the user-designated process-control suspend character (see stty(1)). See ex(1) for more information on the suspend editor command. 

!  An operator that passes specified lines from the buffer as standard input to the specified system command, and replaces those lines with the standard output from the command.  The !  is followed by a movement command specifying the lines to be passed (lines from the current position to the end of the movement) and then the command (terminated as usual by a return).  A count preceding the !  is passed on to the movement command after !. 

Doubling !  and preceding it by count causes that many lines, starting with the current line, to be passed. 

" Use to precede a named buffer specification.  There are named buffers 1 through 9 in which the editor places deleted text.  The named buffers a through z are available to the user for saving deleted or yanked text; see also y, below. 

$ Move to the end of the current line.  A count specifies the number of lines to advance (for example, 2$ causes the cursor to advance to the end of the next line). 

% Move to the parenthesis or brace that matches the parenthesis or brace at the current cursor position. 

& Same as the ex command & (that is, & repeats the previous substitute command). 

´ When followed by a ´, vi returns to the previous context, placing the cursor at the beginning of the line.  (The previous context is set whenever a non-relative move is made.)  When followed by a letter a-z, returns to the line marked with that letter (see the m command), at the first non-whitespace character in the line. 

When used with an operator such as d to specify an extent of text, the operation takes place over complete lines (see also `). 

` When followed by a `, vi returns to the previous context, placing the cursor at the character position marked (the previous context is set whenever a non-relative move is made).  When followed by a letter a z, returns to the line marked with that letter (see the m command), at the character position marked. 

When used with an operator such as d to specify an extent of text, the operation takes place from the exact marked place to the current position within the line (see also ´). 

[[ Back up to the previous section boundary.  A section is defined by the value of the sections option.  Lines that start with a formfeed (^L character) or { also stop [[. 

If the option lisp is set, the cursor stops at each ( at the beginning of a line. 

]] Move forward to a section boundary (see [[). 

^ Move to the first non-whitespace position on the current line. 

( Move backward to the beginning of a sentence.  A sentence ends at a ., !, or ?  followed by either the end of a line or by two spaces.  Any number of closing ), ], " and ’ characters can appear between the ., !, or ?  and the spaces or end of line.  If a count is specified, the cursor moves back the specified number of sentences. 

If the lisp option is set, the cursor moves to the beginning of a lisp s-expression. Sentences also begin at paragraph and section boundaries (see { and [[). 

) Move forward to the beginning of a sentence.  If a count is specified, the cursor advances the specified number of sentences (see ().

{ Move back to the beginning of the preceding paragraph.  A paragraph is defined by the value of the paragraphs option.  A completely empty line and a section boundary (see [[ above) are also interpreted as the beginning of a paragraph.  If a count is specified, the cursor moves backward the specified number of paragraphs. 

} Move forward to the beginning of the next paragraph.  If a count is specified, the cursor advances the specified number of paragraphs (see {). 

| Requires a preceding count; the cursor moves to the specified column of the current line (if possible).

+ Move to the first non-whitespace character in the next line.  If a count is specified, the cursor advances the specified number of lines (same as ^M). 

, The comma (,) performs the reverse action of the last f, F, t, or T command issued, by searching in the opposite direction on the current line.  If a count is specified, the cursor repeats the search the specified number of times. 

- The hyphen character (-) moves the cursor to the first non-whitespace character in the previous line. If a count is specified, the cursor moves back the specified number of times. 

_ The underscore character (_) moves the cursor to the first non-whitespace character in the current line. If a count is specified, the cursor advances the specified number of lines, with the current line being counted as the first line; no count or a count of 1 specifies the current line. 

.  Repeat the last command that changed the buffer.  If a count is specified, the command is repeated the specified number of times. 

/ Read a string from the last line on the screen, interpret it as a regular expression, and scan forward for the next occurrence of a matching string.  The search begins when the user types a carriage return to terminate the pattern; the search can be terminated by sending SIGINT (or the user-designated interrupt character). 

When used with an operator to specify an extent of text, the defined region begins with the current cursor position and ends at the beginning of the matched string.  Entire lines can be specified by giving an offset from the matched line (by using a closing / followed by a +n or -n ). 

0 Move to the first character on the current line (the 0 is not interpreted as a command when preceded by a non-zero digit). 

: The colon character (:) begins an ex command.  The : and the entered command are echoed on the bottom line; the ex command is executed when the user types a carriage return. 

; Repeat the last single character find using f, F, t, or T.  If a count is specified, the search is repeated the specified number of times. 

< An operator that shifts lines to the left by one shiftwidth.  The < can be followed by a move to specify lines.  A count is passed through to the move command. 

When repeated (<<), shifts the current line (or count lines starting at the current one). 

> An operator that shifts lines right one shiftwidth (see <). 

= If the lisp option is set, = reindents the specified lines, as if they were typed in with lisp and autoindent set.  Can be preceded by a count to indicate how many lines to process, or followed by a move command for the same purpose. 

?  Scan backwards, the reverse of / (see /). 

@buffer Execute the commands stored in the named buffer. Be careful not to include a <return> character at the end of the buffer contents unless the <return> is part of the command stream. Commands to be executed in ex mode should be preceded by a colon (:).

~ The tilde (~) switches the case of the character under the cursor (if it is a letter), then moves one character to the right, stopping at the end of the line).  A count specifies how many characters from the current line are switched. 

A Append at the end of line (same as $a). 

B Back up one word, where a word is any non-blank sequence, placing the cursor at the beginning of the word.  If a count is specified, the cursor moves back the specified number of words. 

C Change the rest of the text on the current line (same as c$). 

D Delete the rest of the text on the current line (same as d$). 

E Move forward to the end of a word, where a word is any non-blank sequence.  If a count is specified, the cursor advances the specified number of words. 

F Must be followed by a single character; scans backwards in the current line, searching for that character and moving the cursor to it, if found.  If a count is specified, the search is repeated the specified number of times. 

G Go to the line number given as preceding argument, or the end of the file if no preceding count is given. 

H Move the cursor to the top line on the screen.  If a count is given, the cursor moves to count number of lines from the top of the screen.  The cursor is placed on the first non-whitespace character on the line.  If used as the target of an operator, entire lines are affected. 

I Insert at the beginning of a line (same as ^ followed by i). 

J Join the current line with the next one, supplying appropriate white space: one space between words, two spaces after a period, and no spaces at all if the first character of the next line is a closing parenthesis ()). A count causes the specified number of lines to be joined, instead of two. 

L Move the cursor to the first non-whitespace character of the last line on the screen.  If a count is given, the cursor moves to count number of lines from the bottom of the screen.  When used with an operator, entire lines are affected. 

M Move the cursor to the middle line on the screen, at the first non-whitespace position on the line. 

N Scan for the next match of the last pattern given to / or ?, but in the opposite direction; this is the reverse of n. 

O Open a new line above the current line and enter input mode. 

P Put back (replace) the last deleted or yanked text before/above the cursor.  Entire lines of text are returned above the cursor if entire lines were deleted or yanked.  Otherwise, the text is inserted just before the cursor. 

The P can be preceded by a named buffer specification ("x), to retrieve the contents of the buffer. 

Q Exit vi and enter ex command mode. 

R Replace characters on the screen with characters entered, until the input is terminated with ESC. 

S Change entire lines (same as cc).  A count changes the specified number of lines. 

T Must be followed by a single character; scan backwards in the current line for that character, and, if found, place the cursor just after that character.  A count is equivalent to repeating the search the specified number of times. 

U Restore the current line to its state before the cursor was last moved to it. 

W Move forward to the beginning of a word in the current line, where a word is a sequence of non-blank characters.  A count specifies the number of words to advance. 

X Delete the character before the cursor.  A count repeats the effect, but only characters on the current line are deleted. 

Y Place (yank) a copy of the current line into the unnamed buffer (same as yy).  If a count is specified, count lines are copied to the buffer.  If the Y is preceded by a buffer name, the lines are copied to the named buffer. 

ZZ Exit the editor, writing out the buffer if it was changed since the last write (same as the ex command x).  Note that if the last write was to a different file and no changes have occurred since, the editor exits without writing out the buffer. 

a Enter input mode, appending the entered text after the current cursor position.  A count causes the inserted text to be replicated the specified number of times, but only if the inserted text is all on one line. 

b Back up to the previous beginning of a word in the current line.  A word is a sequence of alphanumerics or a sequence of special characters.  A count repeats the effect. 

c Must be followed by a movement command.  Delete the specified region of text, and enter input mode to replace deleted text with new text.  If more than part of a single line is affected, the deleted text is saved in the numeric buffers.  If only part of the current line is affected, the last character deleted is marked with a $.  A count is passed through to the move command.  If the command is cc, the entire current line is changed. 

d Must be followed by a movement command.  Delete the specified region of text.  If more than part of a line is affected, the text is saved in the numeric buffers.  A count is passed through to the move command.  If the command is dd, the entire current line is deleted. 

e Move forward to the end of the next word, defined as for b.  A count repeats the effect. 

f Must be followed by a single character; scan the rest of the current line for that character, and moves the cursor to it if found.  A count repeats the action that many times. 

h Move the cursor one character to the left (same as ^H).  A count repeats the effect. 

i Enter input mode, inserting the entered text before the cursor (see a). 

j Move the cursor one line down in the same column (same as ^J and ^N). 

k Move the cursor one line up (same as ^P). 

l Move the cursor one character to the right (same as <space>). 

m Must be followed by a single lowercase ASCII letter x; mark the current position of the cursor with that letter.  The exact position on the marked line is referred to by `x; the marked line is referred to by ´x. 

n Repeat the last / or ?  scanning commands. 

o Open a line below the current line and enter input mode; otherwise like O. 

p Put text after/below the cursor; otherwise like P. 

r Must be followed by a single character; the character under the cursor is replaced by the specified one.  (The new character can be a new-line.)  If r is preceded by a count, count characters are replaced by the specified character. 

s Delete the single character under the cursor and enter input mode; the entered text replaces the deleted character.  A preceding count specifies how many characters on the current line are changed.  The last character being changed is marked with a $, as for c. 

t Must be followed by a single character; scan the remainder of the line for that character.  The cursor moves to the column prior to the character if the character is found.  A count is equivalent to repeating the search count times. 

u Reverse the last change made to the current buffer.  If repeated, u alternates between these two states; thus is its own inverse.  When used after an insertion of text on more than one line, the lines are saved in the numerically named buffers. 

w Move forward to the beginning of the next word (where word is defined as in b).  A count specifies how many words the cursor advances. 

x Delete the single character under the cursor.  When x is preceded by a count, x deletes the specified number of characters forward from the cursor position, but only on the current line. 

y Must be followed by a movement command; the specified text is copied (yanked) into the unnamed temporary buffer.  If preceded by a named buffer specification, "x, the text is placed in that buffer also.  If the command is yy, the entire current line is yanked. 

z Redraw the screen with the current line placed as specified by the following options: z<return> specifies the top of the screen, z.  the center of the screen, and z- the bottom of the screen.  The commands z^ and z+ are similar to ^B and ^F, respectively.  However, z^ and z+ do not attempt to maintain two lines of overlap.  A count can be given after the z and before the following character to specify the number of lines displayed in the redrawn screen.  A count before the z gives the number of the line to use as the reference line instead of the default current line. 

Keyboard Editing Keys

At initialization, the editor automatically maps some terminal keyboard editing keys to equivalent visual mode commands.  These mappings are only established for keys that are listed in the following table and defined in the terminfo(4) database as valid for the current terminal (as specified by the TERM environment variable). 

Both command and input mode mappings are created (see the map command in ex(1)). With the exception of the insert char keys, which simply toggle input mode on and off, the input mode mappings exit input mode, perform the same action as the command mode mapping, and then reenter input mode. 

On certain terminals, the character sequence sent by a keyboard editing key, which is then mapped to a visual mode command, can be the same character sequence a user might enter to perform another command or set of commands.  This is most likely to happen with the input mode mappings; therefore, on these terminals, the input mode mappings are disabled by default.  Users can override the disabling and enabling of both the command and input mode keyboard editing key mappings by setting the keyboardedit and keyboardedit!  editor options as appropriate (see ex(1)). The timeout, timeoutlen, and doubleescape editor options are alternative methods of addressing this problem. 

terminfo command input map name description
entry mode map mode map

key_ic i ^[ inschar insert char
key_eic i ^[ inschar end insert char
key_up k ^[ka up arrow up
key_down j ^[ja down arrow down
key_left h ^[ha left arrow left
key_right l ^[la right arrow right
key_home H ^[Ha home arrow home
key_il o^[ ^[o^[a insline insert line
key_dl dd ^[dda delline delete line
key_clear ^L ^[^La clear clear screen
key_eol d$ ^[d$a clreol clear line
key_sf ^E ^[^Ea scrollf scroll down
key_dc x ^[xa delchar delete char
key_npage ^F ^[^Fa npage next page
key_ppage ^B ^[^Ba ppage previous page
key_sr ^Y ^[^Ya sr scroll up
key_eos dG ^[dGa clreos clear to end of screen

EXTERNAL INFLUENCES

Environment Variables

LC_COLLATE determines the collating sequence used in evaluating regular expressions and in processing the tags file. 

LC_CTYPE determines the interpretation of text as single and/or multi-byte characters, the classification of characters as uppercase or lowercase letters, the shifting of letters between uppercase and lowercase, and the characters matched by character class expressions in regular expressions. 

LANG determines the language in which messages are displayed. 

LANGOPTS specifies options determining how text for right-to-left languages is stored in input and output files.  See environ(5).

If LC_COLLATE or LC_CTYPE is not specified in the environment or is set to the empty string, the value of LANG is used as a default for each unspecified or empty variable.  If LANG is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of LANG.  If any internationalization variable contains an invalid setting, the editor behaves as if all internationalization variables are set to "C".  See environ(5).

International Code Set Support

Single- and multi-byte character code sets are supported. 

WARNINGS

See warnings in ex(1).

Program Limits

vi places the following limits on files being edited:

Maximum Line Length
LINE_MAX characters (defined in <limits.h>), including 2-3 bytes for overhead.  Thus, if LINE_MAX is 2048, containing up to 2044 characters should cause no problem. 

If loading files containing lines longer than the stated limit, lines are truncated to the stated maximum length.  If the file is stored back in the original file, truncated information is lost. 

Attempting to create lines longer than allowable maximum from the editor produces a line too long error message. 

Maximum File Size
Maximum file length of 250000 lines is silently enforced.

Other limits:

• 256 characters per global command list. 

• 128 characters in a filename in vi or ex open mode.  On short-filename HP-UX systems, maximum filename length is 14 characters. 

• 128 characters in previous insert/delete buffer. 

• 100 characters in a shell-escape command. 

• 63 characters in a string-valued option (:set command). 

• 30 characters in a program tag name. 

• 32 or fewer macros defined by map command. 

• 512 or fewer characters total in combined map macros. 

AUTHOR

vi was developed by the University of California, Berkeley.  The 16-bit extensions to vi are based in part on software of the Toshiba Corporation. 

SEE ALSO

ex(1), edit(1), expreserve(1), terminfo(4), environ(5), lang(5), regexp(5). 

The Ultimate Guide to the vi and ex Text Editors, Benjamin Cummings Publishing Co., Inc., ISBN 0-8053-4460-8, HP stock number 97005-90015. 

STANDARDS CONFORMANCE

vi: SVID2, XPG2, XPG3

Hewlett-Packard Company  —  HP-UX Release 9.0: August 1992

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026