The vi (visual) program 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. The line-editor commands and the editor options are described in ex(1). Only the visual mode commands are described here.

The view program is identical to vi except that the readonly editor option is set (see ex(1)).

The vedit program is somewhat friendlier for beginners and casual users. The report editor option is set to 1, and the nomagic, novice, and showmode editor options are set.

In vi, the terminal screen acts as a window into a memory copy of the file being edited. Changes made to the file copy are reflected in the screen display. The position of the cursor on the screen indicates the position within the file copy.

The environment variable TERM must specify a terminal type that is defined in the terminfo database (see terminfo(4)). Otherwise, a message is displayed and the line-editor is invoked.

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.

OptionsandArgumentsvi recognizes the following command-line options and arguments:

-

Suppress all interactive-user feedback. This is useful when editor commands are taken from scripts.

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

-R

Set the readonly editor option to prevent overwriting a file inadvertently (see ex(1)).

-ttag

Execute the tagtag command to load and position a predefined file. See the tag command and the tags editor option in ex(1).

-v

Invoke visual mode (vi). Useful with ex, it has no effect on vi.

-V

Set 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. If size is omitted, it defaults to 3.

-x

Set encryption mode. You are prompted for a key to allow for the creation or editing of an encrypted file (see the crypt command in ex(1)).

-ccommand

(XPG4 only.)

+command (Obsolescent) Begin editing by executing the specified ex command-mode commands. As with the normal ex command-line entries, the command option-argument can consist of multiple ex commands separated by verticalline commands (|). The use of commands that enter input mode in this manner produces undefined results.

file

Specify the file or files to be edited. If more than one file is specified, they are processed in the order given. If the -r option is also specified, the files are read from the recovery area.

(XPG4 only.) If both the -t tag and -c command (ortheobsolescent+command) options are given, the -t tag will be processed first, that is, the file containing the tag is selected by -t and then the command is executed.

When invoked, vi is in commandmode. inputmode 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.

WARNING: ESC completes a "bottom line" command (see below).

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.

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

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

CommandSummary
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 description, control characters are indicated in the form ^X, which represents Ctrl-X. Whitespace is defined to be the characters space, tab, and alternative space. Alternative space is the first character of the ALT_PUNCT item described in langinfo(5) 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 preceding 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 preceding count gives the number of (logical) lines to scroll, and is remembered for future ^D and ^U commands.

^D

(input mode) Backs up over the indentation provided by autoindent or ^T to the next multiple of shiftwidth spaces. Whitespace inserted by ^T at other than the beginning of a line cannot be backed over using ^D. A preceding ^ removes all indentation for the current and subsequent input lines of the current input mode until new indentation is established by inserting leading whitespace, 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 preceding count specifies the number of windows to advance. Two lines of overlap are kept if possible.

(XPG4 only.) The current line is displayed and the cursor is moved to the first nonblank character of the current line or the first character if the line is a blank line.

^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 preceding count specifies the number of spaces to back up. (Same as h).

^H

(input mode) Move the cursor left to the previous input character without erasing it from the screen. The character is deleted from the saved text.

^J

Move the cursor down one line in the same column, if possible. A preceding 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 nonwhitespace character in the next line. A preceding 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 preceding 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).

Take the word at or after the cursor as a tag and execute the tagMbobC 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 preceding 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 processcontrol 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 preceding count 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 preceding 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.

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 nonrelative 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 nonwhitespace 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 nonrelative move is made). When followed by a letter az, 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 form feed (^L) 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 nonwhitespace 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 lisps-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 nonwhitespace 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 nonwhitespace 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 nonwhitespace 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 nonzero 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 preceding 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 preceding count specifies how many characters in 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 nonblank 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 nonblank 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 nonwhitespace 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 whitespace: 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 preceding count causes the specified number of lines to be joined, instead of just two.

L

Move the cursor to the first nonwhitespace 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 nonwhitespace 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.

(XPG4 only.) In this case, the cursor is moved to last column position of the inserted characters.

If P is preceded by a named buffer specification (x), the contents of that buffer are retrieved instead.

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

(XPG4 only.) The cursor position is set to the column position 1 or to the position indicated by the previous line if the autoindent is set.

W

Move forward to the beginning of a word in the current line, where a word is a sequence of nonblank characters. If the current position is at the beginning of a word, the current position is within a bigword or the character at that position cannot be a part of a bigword, the current position shall move to the first character of the next bigword. If no subsequent bigword exists on the current line, the current position shall move to the first character of the first bigword on the first following line that contains the bigword. For this command, an empty or blank line is considered to contain exactly one bigword. The current line is set to the line containing the bigword selected and the current position is set to the first character of the bigword selected. A preceding count specifies the number of words to advance.

X

Delete the character before the cursor. A preceding 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 preceding 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 preceding 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 preceding count passes that value 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 preceding count passes that value 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 preceding 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 preceding count repeats the action that many times.

h

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

Mark the current position of the cursor. x is a lowercase letter, a-z, that is used with the ` and ` commands to refer to the marked line or line position.

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 preceding 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 preceding 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 after the z and before the following character to specifies 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.

KeyboardEditingKeys
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 insertchar 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.

Support for international codes and environment variables are as follows:

EnvironmentVariablesUNIX95 specifies using the XPG4 behaviour for this command.

COLUMNS overrides the system-selected horizontal screen size.

LINES overrides the system-selected vertical screen size, used as the number of lines in a screenful and the vertical screen size in visual mode.

SHELL is a variable that shall be interpreted as the preferred command-line interpreter for use in !, shell, read, and other commands with an operand of the form !string. For the shell command the program shall be invoked with the two arguments -c and string. If this variable is null or not set, the sh utility shall be used.

TERM is a variable that shall be interpreted as the name of the terminal type. If this variable is unset or null, an unspecified default terminal type shall be used.

PATH determines the search path for the shell command specified in the editor commands, shell, read, and write. EXINIT determines a list of ex commands that will be executed on editor startup, before reading the first file. The list can contain multiple commands by separating them using a vertical line (|) character.

HOME determines a pathname of a directory that will be searched for an editor startup file named .exrc.

LC_ALL This variable shall determine the locale to be used to override any values for locale categories specified by the setting of LANG or any environment variables beginning with LC_.

LC_MESSAGES determines the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informative messages written to standard output.

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

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

MaximumLineLengthLINE_MAX characters (defined in <limits.h>), including 2-3 bytes for overhead. Thus, if the value specified for LINE_MAX is 2048, a line length up to 2044 characters should cause no problem.

If you load a file that contain lines longer than the specified limit, the lines are truncated to the stated maximum length. Saving the file will write the truncated version over the original file, thus overwriting the original lines completely.

Attempting to create lines longer than the allowable maximum for the editor produces a linetoolong error message.