Edits files with a full-screen display.
{ vi | vedit } [ -l ] [ -R ] [ -tTag ] [ -v ] [ -wNumber] [ -yNumber ] [ -r [ File ] ] [{ + | -c } { Subcommand } ] [ File ... ]
The vi command starts a full-screen editor based on the underlying ex editor. Therefore, ex subcommands can be used within the vi editor. The vedit command starts a version of the vi editor intended for beginners. In the vedit editor, the report option is set to 1, the showmode option is set, and the novice option is set, making it a line editor.
You start the vi editor by specifying the name of the file or files to be edited. If you supply more than one File parameter on the command line, the vi editor edits each file in the specified order. The vi editor on an existing file displays the name of the file, the number of lines, and the number of characters at the bottom of the screen. In case of multibyte locales the number of characters need to be interpreted as the number of bytes.
Since the vi editor is a full-screen editor, you can edit text on a screen-by-screen basis. The vi editor makes a copy of the file you are editing in an edit buffer, and the contents of the file are not changed until you save the changes. The position of the cursor on the display screen indicates its position within the file, and the subcommands affect the file at the cursor position.
The following list provides the maximum limits of the vi editor. These counts assume single-byte characters.
0602-103 file too large to place in /tmp
The vi editor operates in the following modes:
command mode | When you start the vi editor, it is in command mode. You can enter any subcommand except those designated for use only in the text input mode. The vi editor returns to command mode when subcommands and other modes end. Press the Esc key to cancel a subcommand. |
text-input mode | You use the vi editor in this mode to add text. Enter text input mode with any of the following subcommands: the a subcommand, A subcommand, i subcommand, I subcommand, o subcommand, O subcommand, cx subcommands (where the x represents the scope of the subcommand), C subcommand, s subcommand, S subcommand, and R subcommand. After entering one of these subcommands, you can enter text into the editing buffer. To return to command mode, press the Esc key for normal exit or press Interrupt (the Ctrl-C key sequence) to end abnormally. |
last-line mode | Subcommands with the prefix : (colon), / (slash), ? (question mark), ! (exclamation point), or !! (two exclamation points) read input on a line displayed at the bottom of the screen. When you enter the initial character, the vi editor places the cursor at the bottom of the screen, where you enter the remaining characters of the command. Press the Enter key to run the subcommand, or press Interrupt (the Ctrl-C key sequence) to cancel it. When the !! prefix is used, the cursor moves only after both exclamation points are entered. When you use the : prefix to enter the last-line mode, the vi editor gives special meaning to the following characters when they are used before commands that specify counts: |
You can customize the vi editor by:
The following list describes the vi editor options you can change with the set command. The default setting for these options is off. If you turn on one of these toggle options, you can turn it off again by entering the word no before the option. If you want to discontinue the autowrite vi option, enter noaw, where no turns off the option and aw specifies the autowrite option.
vi Option (Abbreviation) | Description |
---|---|
autoindent (ai) | Indents automatically in text input mode to the indentation of the previous line by using the spacing between tab stops specified by the shiftwidth option. The default is noai. To back the cursor up to the previous tab stop, press the Ctrl-D key sequence. This option is not in effect for global commands. |
autoprin (ap) | Prints the current line after any command that changes the editing buffer. The default is ap. This option applies only to the last command in a sequence of commands on a single line and is not in effect for global commands. |
autowrite (aw) | Writes the editing buffer to the file automatically before the :n subcommand, the :ta subcommand, the Ctrl-A key sequence, and the ! subcommand if the editing buffer changed since the last write subcommand. The default is noaw. |
beautifying text (bf) | Prevents the user from entering control characters in the editing buffer during text entry (except for tab, new-line, and form-feed indicators). The default is nobf. This option applies to command input. |
closepunct (cp=) | Handles a list of closing punctuation, especially when wrapping text (wraptype option). Precedes multicharacter punctuation with the number of characters; for example, cp=3..;)}. The vi command does not split closing punctuation when wrapping. |
directory (dir=) | Displays the directory that contains the editing buffer. The default is dir = /var/tmp. |
edcompatible (ed) | Retains g (global) and c (confirm) subcommand suffixes during multiple substitutions and causes the r (read) suffix to work like the r subcommand. The default is noed. |
exrc (exrc) | If not set, ignores any .exrc file in the current directory during initialization, unless the current directory is that named by the HOME environment variable. The default is noexrc. |
hardtabs (ht=) | Tells the vi editor the distance between the hardware tab stops on your display screen. (This option must match the tab setting of the underlying terminal or terminal emulator.) The default is ht=8. |
ignorecase (ic) | Ignores distinction between uppercase and lowercase while searching for regular expressions. The default is noic. |
linelimit (ll=) | Sets the maximum number of lines, as per the -y command-line option. This option only is effective if used with the .exrc file or the EXINIT environment variable. |
lisp (lisp) | Removes the special meaning of ( ), { }, [ [, and ] ] and enables the = (formatted print) operator for s-expressions, so you can edit list processing (LISP) programs. The default is nolisp. |
list (list) | Displays text with tabs (^I) and the marked end of lines ($). The default is nolist. |
magic (magic) | Treats the . (period), [ (left bracket), and * (asterisk) characters as special characters when searching for a pattern. In off mode, only the ( ) (parentheses) and $ (dollar sign) retain special meanings. However, you can evoke special meaning in other characters by preceding them with a \ (backslash). The default is magic. |
mesg (mesg) | Turns on write permission to the terminal if set while in visual mode. This option only is effective if used with the .exrc file or the EXINIT environment variable. The default is on. |
modeline (modeline) | Runs a vi editor command line if found in the first five or the last five lines of the file. A vi editor command line can be anywhere in a line. For the vi editor to recognize a command line, the line must contain a space or a tab followed by the ex: or vi: string. The command line is ended by a second : (colon). The vi editor tries to interpret any data between the first and second colon as vi editor commands. The default is nomodeline. |
novice | Indicates whether you are in novice mode. You cannot change the value by using the set command. |
number (nu) | Displays lines prefixed with their line numbers. The default is nonu. |
optimize (opt) | Speeds the operation of terminals that lack cursor addressing. The default is noopt. |
paragraphs (para=) | Defines vi macro names that start paragraphs. The default is para=IPLPPPQPP\ LIpplpipnpbp. Single-letter nroff macros, such as the .P macro, must include the space as a quoted character if respecifying a paragraph. |
partialchar (pc=) | Appears in the last display column where a double-wide character would not be displayed completely. The default character is - (minus sign). |
prompt | Prompts for a new vi editor command when in command mode by printing a : (colon). The default is on. |
readonly (ro) | Sets permanent read-only mode. The default is noreadonly. |
redraw (redraw) | Simulates a smart workstation on a dumb workstation. The default is nore. |
remap | Allows defining macros in terms of other macros. The default is on. |
report (re=) | Sets the number of times you can repeat a command before a message is displayed. For subcommands that produce many messages, such as global subcommands, the messages are displayed when the command sequence completes. The default is report=5. |
scroll (scr=) | Sets the number of lines to be scrolled when the user scrolls up or down. The default is 1/2 of the window size, rounded down. |
sections (sect=) | Defines vi macro names that start sections. The default is sect=NHSHHH\ HUuhsh+c. Single-letter nroff macros, such as the .P macro, must include the space as a quoted character if respecifying a paragraph. |
shell (sh=) | Defines the shell for the ! subcommand or the :! subcommand. The default is the login shell. |
shiftwidth (sw=) | Sets the distance for the software tab stops used by the autoindent option, the shift commands ( > and < ), and the text input commands ( the Ctrl-D and Ctrl-T key sequences). This vi option only affects the indentation at the beginning of a line. The default is sw=8. |
showmatch (sm) | Shows the ( (matching left parenthesis) or { (left bracket) as you type the ) (right parenthesis) or } (right bracket). The default is nosm. |
showmode (smd) | Displays a message to indicate when the vi editor is in input mode. The default is nosmd. |
slowopen (slow) | Postpones updating the display screen during inserts. The default is noslow. |
tabstop (ts=) | Sets the distance between tab stops in a displayed file. The default is ts=8. |
tags (tags =) | Defines the search path for the database file of function names created using the ctags command. The default is tags=tags\ /usr/lib/tags. |
term (term=) | Sets the type of workstation you are using. The default is term=$TERM, where $TERM is the value of the TERM shell variable. |
terse (terse) | Allows the vi editor to display the short form of messages. The default is noterse. |
timeout (to) | Sets a time limit of two seconds on an entry of characters. This limit allows the characters in a macro to be entered and processed as separate characters when the timeout option is set. To resume use of the macro, set the notimeout option. The default is to. |
ttytype | Indicates the tty type for the terminal being used. You cannot change this value from the vi editor. |
warn (warn) | Displays a warning message before the ! subcommand executes a shell command if it is the first time you issued a shell command after changes were made in the editing buffer but not written to a file. The default is warn. |
window (wi=) | Sets the number of lines displayed in one window of text. The default depends on the baud rate at which you are operating: 600 baud or less, 8 lines; 1200 baud, 16 lines; higher speeds, full screen minus 1 line. |
wrapmargin (wm=) | Sets the margin for automatic word wrapping from one line to the next. The default is wm=0. A value of 0 turns off word wrapping. |
wrapscan (ws) | Allows string searches to wrap from the end of the editing buffer to the beginning. The default is ws. |
wraptype (wt=) | Indicates the method used to wrap words at the end of a line. The default value is general. You can specify one of the following four values: |
writeany (wa) | Turns off the checks usually made before a write subcommand. The default is nowa. |
To see a list of the vi editor settings that have changed from the default settings, enter set and press the spacebar. Press the Enter key to return to the command mode.
To see a complete list of the vi editor settings, enter set all. Press the Enter key to return to the command mode.
To turn on a vi editor option, enter set Option. This command automatically returns you to the command mode.
To turn on multiple vi editor options, enter set Option Option Option. This command turns on the three designated vi editor options and returns you to the command mode.
To turn off a vi editor option, enter set noOption. This command automatically returns you to the command mode.
To change the value of a vi editor option, enter set Option=Value. This command automatically returns you to the command mode.
You can use the :set subcommand of the vi editor to set options for this editing session only, or to set options for this editing session and all future editing sessions.
To set or change vi editor options for this editing session only, enter the :set subcommand from the command line.
To set vi options for all editing sessions, put the :set subcommand in the EXINIT environment variable in the .profile file (read by the shell on login) or put the set subcommand into a .exrc file. The vi editor first looks for the EXINIT environment variable and runs its commands. If the EXINIT environment variable does not exist, the vi editor then looks for the $HOME/.exrc file and runs its commands. Last, and regardless of any previous results, the vi editor looks for the local .exrc file and runs its commands.
For information about changing an option by setting the EXINIT environment variable, see the description of environment variables in the environment file.
The .exrc file can contain subcommands of the form set Option=Value; for example:
set cp=3 . . ;
To include a comment in the .exrc file, use a " (double quotation mark) as the first character in the line.
If you use a subcommand or sequence of subcommands frequently, you can use the vi editor to define a macro that issues that subcommand or sequence.
To define a macro, enter the sequence of subcommands into a buffer named with a letter of the alphabet. The lowercase letters a through z overlay the contents of the buffer, and the uppercase letters A through Z append text to the previous contents of the buffer, allowing you to build a macro piece by piece.
For example, to define a buffer macro named c that searches for the word corner and makes the third line after the word corner the current line, enter the following command:
o /corner/+3
Then press the Esc key and enter the following command:
"c
where c is the name of the buffer macro.
To add text to the previous contents of the defined buffer, enter the o viSubcommand, press the Esc key, and enter "CapitalLetter, where the CapitalLetter variable specifies an uppercase letter A through Z. For example, to build a buffer macro named T that searches for the word corner and allows you to add more commands, enter the following command:
o corner
Then press the Esc key and enter the following command:
"T
where T is the name of the buffer macro. You can repeat this process at any time to add more vi subcommands to the same buffer.
For example, to add commands that move the cursor to the previous line and delete that line, enter the following command:
o -dd
where - (minus sign) means to move the cursor up one line, and dd means to delete the current line. Press the Esc key and enter the following command:
"Tdd
To start the macro, enter @Letter, where the Letter variable specifies the letter name of the buffer macro you want to use. To use the same macro again, enter @@ (two at symbols). For example, enter @T to start the T buffer macro and run the search, move cursor, and delete line commands. Enter @@T to start the T buffer macro again.
The character set used by your system is defined by the collation table. This table affects the performance of vi macros.
You can use the :map, :map!, and :ab subcommands to map a keystroke to a command or a sequence of commands. The :map subcommand is used in the command mode. The :map! and :ab subcommands are used in the text input mode. You can map keys for this editing session and all future editing sessions or only for the current editing session from either mode.
To map keys for all future editing sessions, put the subcommand into a $HOME/.exrc file. Each time you start the vi editor, it reads this file. The mapping remains in effect for every editing session.
To map keys for the current editing session only from the command mode, start the subcommand during the vi editor session. To map keys for the current editing session only from the text input mode, enter the subcommand on the command line during the vi editor session. The mapping remains in effect only for the current editing session.
The :map, :map!, and :ab subcommands are defined and used as follows:
The set command has behavior similar to the map! command except that the set command substitutes the string for the abbreviation only when the abbreviation is a separate word. You can use the set command of the vi editor to:
-cSubcommand | Carries out the ex editor subcommand before viewing with vi begins. The cursor moves to the line affected by the last subcommand to be carried out. When a null operand is entered, as in -c' ', the vi editor places the cursor on the first line of the file. The -c flag is incompatible with the + flag. Do not specify both flags at the same time. |
-l | Enters the vi editor in LISP mode. In this mode, the vi editor creates indents appropriate for LISP code, and the (, ), {, }, [[, and ]] subcommands are modified to act appropriately for LISP. |
-r[File] | Recovers a file after a vi editor or system malfunction. If you do not specify the File variable, the vi editor displays a list of all saved files. |
-R | Sets the readonly option to protect the file against overwriting. |
-tTag | Edits the file containing the Tag variable and positions the vi editor at its definition. To use this flag, you must first create a database of function names and their locations using the ctags command. |
-v | Enters the vi editor in the verbose mode. |
-wNumber | Sets the default window size to the value specified by the Number variable. This flag is useful when you use the vi editor over a low-speed line. |
-yNumber | Overrides the maximum line setting of 1,048,560 with any value greater than 1024. You should request twice the number of lines that you require because the vi editor uses the extra lines for buffer manipulation. |
+[Subcommand] | Carries out the ex editor subcommand before editing begins. If you do not specify the Subcommand variable, the cursor is placed on the first line of the file. This + flag is incompatible with the -c flag. Do not specify both flags at the same time. |
Use the following general syntax to enter subcommands:
[Named_Buffer] [Operator] [Number] Object
You can put a number in front of many subcommands. The vi editor interprets this number in one of the following ways:
5G 10Z
25|
10Ctrl-U 10Ctrl-D
Use the subcommands to perform these kinds of actions:
Use subcommands to move the cursor within a file in these ways:
Enter the following subcommands in command mode. You can cancel an incomplete command by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Enter the following subcommands in command mode. You can cancel an incomplete command by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Enter the following subcommands in command mode. If you need information about the format of vi subcommands, "vi General Subcommand Syntax."
Enter the following subcommands in command mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
The subcommands for editing enable you to perform the following tasks:
Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Enter the following subcommands in command mode to change the vi editor into text input mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
aText |
Inserts text specified by the Text parameter after the cursor. End text input mode by pressing the Esc key. |
AText |
Adds text specified by the Text parameter to the end of the line. End text input mode by pressing the Esc key. |
iText |
Inserts text specified by the Text parameter before the cursor. End text input mode by pressing the Esc key. |
IText |
Inserts text specified by the Text parameter before the first nonblank character in the line. End text input mode by pressing the Esc key. |
o |
Adds an empty line below the current line. End text input mode by pressing the Esc key. |
O |
Adds an empty line above the current line. End text input mode by pressing the Esc key. |
Use the following subcommands only while in text input mode. These commands have different meanings in command mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Use the following subcommands in command mode. An incomplete subcommand can be canceled by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Use the following subcommands in command mode. An incomplete subcommand can be canceled by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Use the following subcommands in command mode. An incomplete subcommand can be canceled by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
u | Undoes the last change.
Note
After an undo, the cursor
moves to the first non-blank character on the updated current line. |
U | Restores the current line if the cursor has not left the line since the last change. |
. | Repeats the last change or increments the "np command.
Notes:
|
"n p | Retrieves the nth last delete of a complete line or block of lines. |
The subcommands for manipulating files allow you to do the tasks outlined in the following sections:
Use the following subcommands in command mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
:w | Writes the edit buffer contents to the original file. If you are using this subcommand within the ex editor, you do not need to type the : (colon). |
:w File | Writes the edit buffer contents to the file specified by the File parameter. If you are using this subcommand within the ex editor, you do not need to type the : (colon). |
:w! File | Overwrites the file specified by the File parameter with the edit buffer contents. If you are using this subcommand within the ex editor, you do not need to type the : (colon). |
Enter the following subcommands in command mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
:e File | Edits the specified file. If you are using this subcommand from the ex editor, you do not need to type the : (colon). |
:e! | Re-edits the current file and discards all changes. |
:e + File | Edits the specified file starting at the end. |
:e + Number File | Edits the specified file starting at the specified line number. |
:e # | Edits the alternate file. The alternate file is usually the previous file name before accessing another file with a :e command. However, if changes are pending on the current file when a new file is called, the new file becomes the alternate file. This subcommand is the same as the Ctrl-A subcommand. |
:r File | Reads the file into the editing buffer by adding new lines below the current line. If you are using this subcommand from the ex editor, you do not need to type the : (colon). |
:r !Command | Runs the specified command and places its output into the file by adding new lines below the current cursor position. |
:ta Tag | Edits a file containing the Tag tag starting at the location of the tag. To use this subcommand, you must first create a database of function names and their locations using the ctags command. If you are using this subcommand from the ex editor, you do not need to type the : (colon). |
Ctrl-A | Edits the alternate file. The alternate file is usually the previous current file name. However, if changes are pending on the current file when a new file is called, the new file becomes the alternate file. This subcommand is the same as the :e # subcommand. |
Enter the following subcommands in command mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
:n | Edits the next file in the list entered on the command line. If you are using this subcommand from the ex editor, a : (colon) is not needed. |
:n Files | Specifies a new list of files to edit. If you are using this subcommand from the ex editor, a : (colon) is not needed. |
Enter the following subcommand in command mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax".
Ctrl-G | Shows the current file name, current line number, number of lines in the file, and percentage of the way through the file where the cursor is located. |
The vi editor provides the subcommands described in the following sections:
Enter the following subcommands in command mode. An incomplete subcommand can be canceled by pressing the Esc key. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Ctrl-L | Clears and redraws the screen. |
Ctrl-R | Redraws the screen and eliminates blank lines marked with @ (at sign). |
zNumber | Makes the window the specified number of lines long. |
The following subcommands allow you to run a command within the vi editor. Enter these subcommands in command mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
:sh | Enters the shell to allow you to run more than one command. You can return to the vi editor by pressing the Ctrl-D key sequence. If you are using this subcommand within the ex editor, a : (colon) is not needed. |
:!Command | Runs the specified command and then returns to the vi editor. If
you are using this subcommand within the ex editor,
a : (colon) is not needed.
Note
The # (alternate file), % (current file), and ! (previous
command) special characters are expanded when following a :! subcommand. To prevent any of these characters from being expanded,
use the \ (backslash). |
:!! | Repeats the last :!Command subcommand. |
Number!!Command | Runs the specified command and replaces the lines specified by Number with the output of the command. If a number is not specified, the default value is 1. If the command expects standard input, the specified lines are used as input. |
!Object Command | Runs the specified command and replaces the object specified by the Object parameter with the output of the command. If the command expects standard input, the specified object is used as input. |
Enter the following subcommands in command mode. If you need information about the format of vi subcommands, see "vi General Subcommand Syntax."
Q | Enters the ex editor in command mode. |
ZZ | Exits the vi editor, saving changes. |
:q | Quits the vi editor. If you have changed the contents of the editing buffer, the vi editor displays a warning message and does not quit. If you are using this subcommand from the ex editor, a : (colon) is not needed. |
:q! | Quits the vi editor, discarding the editing buffer. If you are using this subcommand from the ex editor, a : (colon) is not needed. |
Esc | Ends text input or ends an incomplete subcommand. |
Ctrl-? | Interrupts a subcommand. |
The following exit values are returned:
0 | Indicates successful completion. |
>0 | Indicates an error occurred. |
Input files must be text files or files that are similar to text files except for an incomplete last line that is no longer than LINE_MAX -1 bytes in length and contains no null characters.
The .exrc files must be text files consisting of ex commands.
By default, the vi editor reads lines from the files to be edited without interpreting any of those lines as any form of vi editor command.
The ctags command, ed command, ex command, sed command, tvi command, view command.
The .profile file.