vi - Use the display-oriented interactive text editor
Format
vi [-BelRrsv] [+command] [-c command] [-t tag] [-w size] [-W option[option] ... [file ...]
Symbol | Indicates |
---|---|
Ctrl-L followed by a single letter | Control character that is transmitted by holding down the Ctrl key and the letter key at the same time. |
BACKSPACE | The real backspace key. This key might differ from the Ctrl-H key. |
ENTER | The ENTER key, which is labeled RETURN on some keyboards. |
ESCAPE | The Escape key. |
INTERRUPT | The break key; often Ctrl-C. |
→ | The right arrow key. |
← | The left arrow key. |
↓ | The down arrow key. |
↑ | The up arrow key. |
Description
vi has two components: a screen editor (vi) and a line editor (ex). Each has a different set of commands. You can invoke the line editor from within the screen editor. Conversely, you can invoke the screen editor from within the line editor.
In the screen editor, you are in either command mode or insert mode. In command mode, every character you type is immediately interpreted as a command. In insert mode, every character you type is added to the text that you are editing.
- Invoke the command under the name ex.
- Invoke it under the name vi but specify the -e option.
- Invoke it under the name ex but specify the -v option.
- Invoke the command under the name vi (without specifying -e).
.0
or \0
), it is turned into
the value 0x7F
.
The newline character is interpreted as a line delimiter. Each line
is limited to a maximum length of {LINE_MAX} bytes, including the
newline. Any lines exceeding that length are truncated at that length.
If the last line in the file does not end in a newline, a newline
is added. In all those cases, vi marks the
file as modified and displays a message. vi is available if you log in to the shell with the rlogin command or via telnet. It is not available if you log in with the OMVS command.
The current position marker
The current position marker indicates a position in the text that is being edited (or was edited). In ex mode, the current position pointer is just the line number of the line being edited. In vi mode, the pointer gives this line number plus the position of the cursor within the line. The line indicated by the current position pointer is always on the screen.
vi display conventions
vi displays the input for search commands (/ and ?),
ex commands (:), and system commands (!) on the bottom line of the screen.
Error and informational messages also appear on this line. If the last line in the file is ahead of
the last line of the screen, then screen lines beyond the end of the file are displayed with a
single ~
character in column one. In certain infrequent circumstances (typically
involving lines longer than the width of the screen), vi is unable to fill
the display with complete lines. In this case, one or more screen lines are shown with a single
@
character in column one. These lines are not part of the file content and should
be ignored.
Options
- +command
- Begins the editing session by running the specified editor command. To specify multiple commands, separate them with a vertical bar (|).
- -B
- Disables the automatic conversion of tagged files. This option is ignored if the filecodeset or pgmcodeset options (-W option) are specified.
- -c command
- Runs command before displaying any text on the screen.
command is any ex command. You can specify
multiple ex commands by separating them with an or-bar
(
|
) and enclosing them in quotation marks. The quotation marks ensure that the shell does not interpret the|
as a pipe character. For example:-c 'set all | ver'
- -e
- Invokes ex.
- -i
- Sets LISP mode. The ( and ) commands use blocks of LISP code as their context rather than sentences.
- -R
- Sets the
readonly
variable, preventing the accidental overwriting of files. Any command that writes to a file requires the ! suffix. - -r
- Tries to recover all files that are specified on the command line after a system or editor
crash. If you do not specify any files, vi displays a list of all
recoverable files.
When you are using vi -r to recover a file that was being edited with automatic conversion, the file must also be recovered with automatic conversion enabled when writing the data back to the original tagged text file. Likewise, if explicit conversion was being used when editing the file (by using the -W filecodeset or -W pgmcodeset options), the same options must be specified when writing the recovered data back to the original file. Failure to do either of these might result in incorrectly coded character data being written to the file when you save the recovered version.
- -s
- Turns on quiet mode. The
editor does not print file information messages, thus allowing ex to
be used as a filter. Because the file is not displayed, the editor
does not read the value of the TERM environment variable. This
option also keeps ex from reading any startup
files (
.exrc
or the file specified by EXINIT). - -t tag
- Searches for a tag in the same way that you use with the ex tag command.
- Puts the editor into vi mode.
- -w size
- Sets the option variable
window
to size. See Setting the vi options for more information. - -W option[,option]...
- Specifies z/OS-specific options. The option keywords are case-sensitive. Possible
options are:
- filecodeset=codeset
- Performs text conversion from one code set to another when reading from the file. The coded
character set of the file is codeset. codeset
can be a code set name that is known to the system or a numeric coded character set identifier
(CCSID). The command
iconv -l
lists existing CCSIDs along with their corresponding code set names. The filecodeset and pgmcodeset options can be used on files with any file tag.If pgmcodeset is specified but filecodeset is omitted, then the default file code set is ISO8859-1 even if the file is tagged with a different code set. If neither filecodeset nor pgmcodeset is specified, text conversion will not occur unless automatic conversion is enabled or the _TEXT_CONV environment variable indicates text conversion. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.
If filecodeset or pgmcodeset is specified, then automatic conversion is disabled for this command invocation and the -B option is ignored if it is also specified. For more information about automatic conversion, see Converting files between code pages in z/OS UNIX System Services Planning.
When specifying values for filecodeset, use the values that Unicode Service supports.
- pgmcodeset=codeset
- Performs text conversion from one code set to another when reading from the file. The coded
character set of the program (command) is codeset.
codeset can be a code set name known to the system or a numeric coded
character set identifier (CCSID). The command
iconv -l
lists existing CCSIDs along with their corresponding code set names. The filecodeset and pgmcodeset options can be used on files with any file tag.If filecodeset is specified but pgmcodeset is omitted, then the default program code set is IBM-1047. If neither filecodeset nor pgmcodeset is specified, text conversion will not occur unless automatic conversion is enabled or the _TEXT_CONV environment variable indicates text conversion. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.
If filecodeset or pgmcodeset is specified, then automatic conversion is disabled for this command invocation and the -B option is ignored if it is also specified.
Restriction: The only supported values for pgmcodeset are IBM-1047 and 1047.
Examples
- To edit or browse a file that contains ASCII characters by using the
vi editor, assuming that:
- The file is untagged and you do not want to tag it or enable automatic conversion, and
- You cannot alter the tag (for example, you are browsing an untagged public file or a read-only file)
vi -W filecodeset=ISO8859-1,pgmcodeset=IBM-1047 myAsciiFile
- To edit or browse a file that contains EBCDIC characters by using the vi
editor, assuming that automatic conversion has been enabled but the file is incorrectly tagged as
UTF-8, issue:
vi -B myMisTaggedFile
vi command summary
The vi command is divided into categories: scrolling commands, movement commands, object manipulation commands, text insertion commands, and miscellaneous commands.
Scrolling commands
Scrolling commands adjust the position of text on the screen. The current position pointer only changes if the current line is scrolled off the screen. For example, Ctrl-E scrolls the text on the screen up one line. The cursor remains pointing to the same text that it was pointing to, unless that text is moved off the screen. See Scrolling commands.
[count] command
count means
different things with different commands. If you type count,
it is not displayed anywhere on the screen. - Ctrl-B
- Scrolls text back by a page, (that is, a screen), less two lines. The cursor is placed on the last line of the screen. count specifies a number of pages to scroll. The default value for count is 1.
- Ctrl-D
- Scrolls text onto the bottom of the screen. The current position pointer moves forward the same amount in the text, which means that the cursor stays in the same relative position on the screen. If count is given, the screen scrolls forward by the given number of lines; this number is used for all future Ctrl-D and Ctrl-U commands (until a new count is given). The default for the scrolling amount is half the screen.
- Ctrl-E
- Scrolls a new line onto the bottom of the screen. The current position pointer is not changed unless the current line scrolls off the top of the screen; then the pointer is set to the top line. If count is given, the screen scrolls forward the given number of lines. The default value for count is 1.
- Ctrl-F
- Scrolls text forward a page (that is, a screen), less two lines. The cursor is placed on the top line of the screen. count specifies the number of pages to scroll. The default value for count is 1.
- Ctrl-U
- Scrolls text onto the top of the screen. The current position pointer moves backward the same amount in the text, which means that the cursor stays in the same relative position on the screen. count operates as for Ctrl-D. The default scrolling amount is half the screen.
- Ctrl-Y
- Scrolls a new line onto the top of the screen. The current position pointer is not changed unless the current line scrolls off the bottom of the screen; then the pointer is set to the bottom line. If count is given, the screen scrolls backward the given number of lines. The default value for count is 1.
- [n] z [m] type
- Redraws the screen in a window of m lines. type determines
the position of the current line. If type is
the newline character, the current line is placed at the top of the
window. If type is a period (
.
), the current line is placed in the middle of the window. If type is a minus sign (-
), the current line is placed at the bottom of the window. If n is given, the current position pointer is first set to that absolute line number; then the screen is positioned according to type. If you omit n, it defaults to the current line. If you omit m, it defaults towindow
. (See Setting the vi options.)
Movement commands
Movement commands move the cursor in the file. For example, the character j moves the cursor down one line and the screen is scrolled only if necessary. There are two types of movement commands: absolute movement commands and context-dependent movement commands.
Absolute movement commands
Absolute movement commands move the cursor, regardless of the nature of the surrounding text. For example, j always moves the cursor down one line.
All the following movement commands except m, 0, ^, `, and ´ can be preceded by count to repeat the movement that many times.
For a list of absolute movement commands, see the vi command in z/OS UNIX System Services Command Reference.
Context-dependent movement commands
Context-dependent movement commands move the cursor based on the nature of the text. For example, w moves the cursor to the beginning of the next word, so it must look at the text to determine where the next word begins.
- A sequence of letters, digits, and underscores delimited at both ends by characters that are not letters, digits, or underscores; the beginning or end of a line; or the end of the editing buffer.
- A sequence of characters other than letters, digits, underscores, or white space delimited at both ends by a letter, digit, underscore, white space, the beginning or end of a line, or the end of the editing buffer.
- B
- Moves the cursor back to the first character of the current fullword. If the cursor is already at the beginning of a fullword, vi moves it to the first character of the preceding fullword.
- b
- Moves the cursor back to the first character of the current word. If the cursor is already at the beginning of a word, vi moves it to the first character of the preceding word.
- E
- Moves the cursor forward to the end of a fullword. If the cursor is already at the end of a word, vi moves it to the last character of the next fullword.
- e
- Moves the cursor forward to the end of a word. If the cursor is already at the end of a word, vi moves it to the last character of the next word.
- Fc
- Searches backward in the line for the single character c and positions the cursor on top of it. When count is given, the editor searches back for the count the such character.
- fc
- Searches forward in the line for the single character c and positions the cursor on top of it. When count is given, the editor searches for the count the such character.
- H
- Places the cursor on the first nonblank character of the top line of the screen. count specifies the number of lines from the top of the screen.
- L
- Places the cursor on the first nonblank character of the bottom line of the screen. count specifies the number of lines from the bottom of the screen.
- M
- Places the cursor on the first nonblank character of the middle line of the screen.
- N
- Repeats previous / or ?, but in the opposite direction.
- n
- Repeats previous / or ?.
- Tc
- Searches backward in the line for the character c and position the cursor after the character being sought. count searches backward for the count the matching character and then positions the cursor after the character being sought.
- tc
- Searches forward in the line for the character c and position the cursor on the preceding character. count searches forward for the count the matching character and then positions the cursor on the preceding character.
- W
- Moves forward to the start of the next fullword.
- w
- Moves forward to the start of the next word.
- (
- Moves back to the beginning of the previous sentence. A sentence
is bounded by a period (
.
), exclamation mark (!
), or question mark (?
); followed by any number of closing double quotation marks, ("
), closing single quotation marks ('
), closing parentheses ()
), or closing square brackets (]
); followed by two spaces or the end of the line. Paragraph and section boundaries are also sentence boundaries; see [[ and {.If you specified the lisp option, a lisp s-expression is considered a sentence for this command.
- )
- Moves forward to the beginning of the next sentence. See ( for
the definition of a sentence.
If you specified the lisp option, a lisp s-expression is considered a sentence for this command.
- {
- Moves back to the beginning of a paragraph. A paragraph begins
on a blank line, a section boundary, or a text formatter macro in
the
paragraphs
variable. - }
- Moves forward to the beginning of the next paragraph. See { for the definition of a paragraph.
- [[
- Moves back to the beginning of a section. A section begins on
lines starting with a form feed (Ctrl-L), starting with an open brace {,
a text formatter macro in the sections variable,
or begin or end of file.
If you specified the lisp option, a section boundary is also identified by a line with a leading (.
- ]]
- Moves forward to the beginning of the next section. See [[ for
the definition of a section.
If you specified the lisp option, a section boundary is also identified by a line with a leading (.
- %
- Finds the balancing character to that under the cursor. The character
should be one of the following characters: [{(< >)}].
- ;
- Repeats the previous F, f, T, or t command.
- ,
- Repeats the previous F, f, T, or t command in the opposite direction.
- /regexp ,
- Search forward in the file for a line that matches the regular expression regexp and position the cursor at the first character of the matching string. When used with an operator to define a text range, the range begins with the character at the current cursor position and ends with the first character of the matching string. You can specify whole lines by following regexp with /+n or /-n, where n is the offset from the matched line.
- ?regexp
- Is similar to /, but searches backwards in the file.
- Ctrl-]
- Uses the word after the cursor as a tag. (For information about tag, see ex.)
Object manipulation commands
Object manipulation commands change the text that is already in the file. An object manipulator command works on a block of text. The command character is followed immediately by any movement command. The object that is manipulated by the object manipulator command is the text from the current position pointer to wherever the movement command would leave the cursor.
For example, in dL, d is the object manipulator command to delete an object. It is followed by the movement command L which means move to the bottom line of the screen. The object that is manipulated by the command thus extends from the current line to the bottom line on the screen; these lines are deleted.
Typically, an object extends up to, but not including, the position of the cursor after the move command. However, some movements work in a line mode. For example, L puts the cursor on the first nonblank character of the last line on the screen. If it is used in an object manipulation command, it includes the entire starting line and the entire ending line. Some other objects include the cursor position. For example, d$ deletes up to and including the last character on a line; by itself the $ would have placed the cursor on the final character. Repeating the command letter implies working on a line basis; thus 5dd deletes five lines.
Use of buffers
Objects that are deleted or otherwise manipulated have their original values that are placed in a buffer, an area of computer memory that can hold text. There are several ways this can be done:
- You can use a named buffer. Buffers are named with single lowercase
letters. To place an object in a buffer, type a double quotation mark " followed
by the buffer name, followed by the object manipulator command, as
in:
This deletes text from the current line to the bottom line on the screen and puts the deleted text in buffer a. Typically, this sort of operation overwrites the current contents of the buffer. However, if you use the same form but specify the buffer name in uppercase, the object is appended to the current contents of the buffer. For example:"adL
deletes from the current line to the bottom line on the screen, and adds the deleted text to buffer"AdL
a
. - If you are deleting material and delete at least one full line, vi uses
buffers numbered
1
through9
. The first time a full line or more is deleted, the text is placed in buffer1
. The next time, the previous contents of1
are copied to2
, and the newly deleted text is put into1
. In the same way, deleted text continues to be rippled through the nine numbered buffers. When text is rippled out of buffer9
, it is gone for good. - In all other cases, the object that is manipulated goes to the unnamed buffer. For example, the unnamed buffer is used if you delete less than a line of text. The unnamed buffer is like the other buffers, but does not have a name.
Examples of buffers
Following are some examples of the use of buffers:
- To delete text from the current cursor position through to the
bottom of the screen and place it into buffer 1 (this will also ripple
numbered buffers), enter:
dL
- To delete from the current cursor position through to the next
position that contains (but not including) the string fred,
and place the deleted text into buffer
a
, enter:"ad/fred/+0
- To delete the current word and place it into an unnamed buffer,
enter:
dw
List of object manipulator commands
- c
- Deletes the object and enters insert mode for text insertion after
the current cursor position. If less than one line is changed, a dollar
sign (
$
) is placed on the final character of the object and typing goes directly over top of the current object until the dollar sign ($
) is reached. Additional text is inserted, with the existing text shifting to make room for the new text. - d
- Deletes the object.
- y
- Moves the object to the appropriate buffer; the source is not changed. This can be used to duplicate or copy objects.
- <
- Shifts the object left by the value of the variable
shiftwidth
. This operator always works on a line basis. This command replaces all leading blanks and tabs required for the new indent amount. count shifts count lines. - >
- Shifts the object right by the value of the variable
shiftwidth
. This operator always works on a line basis. This command replaces all leading blanks and tabs that are required for the new indent amount. count shifts count lines. - !
- Filters the object through an external command. After typing the object, the command line opens up for a system command which is parsed in the same manner as the ex system command (:!). This operator then invokes the given command and sends the entire object on a line basis to that command. The object is then deleted and the output from the command replaces it. For example, 1G!Gsort moves to the first line of the file; then takes all the text from the first line to the last line and runs it through the sort command. The output of sort then replaces the original text.
- C
- Changes to the end of the current line. This is equivalent to the c$ command.
- D
- Deletes to the end of the current line. This is equivalent to the d$ command.
- s
- Substitute the character. This is equivalent to the cl command.
- S
- Substitute the line. This is equivalent to the cc command.
- x
- Deletes the current character. This is equivalent to the dl command.
- X
- Deletes the previous character. This is equivalent to the dh command.
- Y
- Yanks the current line. This is equivalent to the yy command.
Text insertion commands
- A
- Enters insert mode at end of line. This is equivalent to the $a command.
- a
- Enters insert mode after the current cursor position.
- I
- Enters insert mode before first nonblank character on line. This is equivalent to the ^i command.
- i
- Enters insert mode before the current cursor position.
- O
- Opens up a new line before the current line and enters insert mode on it.
- o
- Opens up a new line after the current line and enters insert mode on it.
- R
- Replaces characters on the screen with characters typed up to the next ESC. Each character typed overlays a character on the screen. The newline character is an exception; it is simply inserted and no other character is replaced. While you are doing this, the screen may not correspond exactly to the contents of the file, because of such things as tabs. The screen is updated when you leave insert mode.
- r
- Replaces the character under the cursor with the next character typed. When count is given, count characters following the cursor to the new character are changed. If count is given and the newline character is the replacement character, count characters are deleted (as usual) and replaced with a single newline character, not count newlines.
Miscellaneous commands
- J
- Joins count lines together. If you do not specify count, or count is less than 2, vi uses a count of 2, joining the current line and the next line. This command supplies appropriate spacing: one space between words, two spaces after a period, and no spaces at all when the first character of the line is a ). When a line ends with white space, vi retains the white space, does not add any further spaces, and then appends the next line.
- p
- Same as p except that text is pasted before the cursor instead of after it.
- P
- Put buffer contents before the cursor. Also called a paste operation. If preceded
by quote buffername (for example, "b), the contents of
that buffer are used; otherwise the contents of the unnamed buffer are used. If the buffer was
created in ex mode, the contents of the buffer are inserted before the current
line. If the buffer was created in vi mode, the contents are inserted before the
cursor. As a special case, if a paste operation is repeated with the period (
.
) command and it used a numbered buffer, the number of the buffer is incremented. Thus, "1p ..., pastes in the contents of buffer 1 through buffer 6; in other words the last six things that were deleted are put back. - Q
- Switches to ex mode. You leave vi mode and the ex prompt is shown on the bottom line of the screen.
- U
- Undoes all changes to current line. As soon as you move off a line or invoke an ex command on the line, the original contents of the line are forgotten and U is not successful.
- u
- Undoes last change. If repeated, you undo the undo (that is, go back to what the text was before the undo). Some operations are treated as single changes; for example, everything done by a global G is undone with undo.
- ZZ
- Writes the file out, if changed, and then exits.
- .
- Repeats the last command. Any command that changes the contents of the file can be repeated by this command. If you do not specify count with the . command, vi uses the count that was specified for the command being repeated.
- ~
- Toggles the case of the character under the cursor and moves the cursor right by one. This command can be preceded by count to change the case of count characters.
- &
- Repeats the previous ex substitute command, using the current line as the target. Flags set by the previous command are ignored. Equivalent to the ex command &.
- :
- Invokes a single ex command. The editor
places the cursor on the bottom line of the screen and displays a
colon (
:
) to prompt for input. You can then type one or more ex commands; when you press ESC or a RETURN, the line you have entered is passed to ex and executed there. - @
- Invokes a macro. When the next character is a letter from a through z, vi treats it as the name of a buffer. The contents of that buffer are treated as input typed to vi. The text of a macro may contain an @ calling another macro. A macro may call itself, provided it is invoked at the end of the macro (tail recursion). Such a macro executes forever or until an error occurs or the INTERRUPT key is pressed. A macro that invokes itself at the beginning (head recursion) loops until it runs out of memory. A vi error terminates all currently executing macros. All changes made during a macro call are treated as a unit and can be undone with a single u command.
- =
- Reindents the specified line as though they were set via lisp and autoindent-set, if the lisp option was specified.
- Ctrl-G
- Displays the current path name, current line number, total number of lines in the file, and the percentage of the way through the file. This is equivalent to the ex command file.
- Ctrl-L
- Redraws the screen assuming another process has written on it. This should never happen unless a filter ! command writes to the screen rather than the standard output.
- Ctrl-R
- Redraws the screen, removing any deleted lines that are flagged
with the
@
convention. - Ctrl-Z
- Stops the editor and returns you to system level. You can return to the editor with the fg command; however, when you resume a vi session in this way, all of the session's buffers are empty. The jobs command lists all the stopped vi jobs. The amount of available memory limits the number of vi sessions that can be stopped at one time (see fg and jobs).
- Ctrl-^
- Switches to editing the alternate file (see ex for an explanation of write). If you attempt this and you have not written out the file since you made the most recent change, vi does not switch to the alternate file.
Insert mode commands
- Ctrl-D
- Decrements the autoindent for the current line by one level. This is only relevant if the variable autoindent is on.
- Ctrl-H
- Deletes the last typed character. The character is not removed from the screen; however it is no longer in your file. When you backspace over characters, new text overwrites the old ones. You are permitted to backspace to the start of the current line regardless of where you started to insert text. (This is not true of some other versions of vi.)
- BACKSPACE
- Deletes the last typed character. The character is not removed from the screen; however it is no longer in your file. When you backspace over characters, new text overwrites the old ones. You are permitted to backspace to the start of the current line regardless of where you started to insert text. (This is not true of some other versions of vi.)
- Ctrl-J
- Ends the current line and starts a new one.
- Ctrl-M
- Ends the current line and starts a new one.
- RETURN
- Ends the current line and starts a new one.
- Ctrl-Q
- Inserts the following character literally, instead of using its special meaning. You could use this to escape, say, the ESC character itself. It is impossible to insert a Ctrl-J or the null character in your line.
- Ctrl-V
- Inserts the following character literally, instead of using its special meaning. You could use this to escape, say, the ESC character itself. It is impossible to insert a Ctrl-J or the null character in your line.
- Ctrl-T
- Increments the autoindent for the current line by one level. This
is only relevant if the variable
autoindent
is on. - Ctrl-W
- Deletes the word preceding the cursor and blanks. Although the characters are not removed from the screen, they are no longer in your file.
- Ctrl-@
- If this is the first character typed after entering insert mode, the previously typed insert mode contents are repeated. After this, you exit insert mode. Only up to 256 characters from the previous insertion are inserted.
- ESC
- Leaves insert mode.
- INTERRUPT
- Leaves insert mode.
ex command mode
vi enters ex command mode if the program is invoked with the -e option or if the Q command is issued from vi. You can issue a single ex command from vi using the : command.
[address-list] [[command] [!] [parameters]]
Each
part is optional and might be invalid for some commands. You can specify
multiple commands on a line by separating them with an or-bar |
.
- address-list
- Commands can take zero, one, or two addresses. The address
%
is a short form to indicate the entire file. You can omit any or all of the addresses. In the command descriptions to follow, the addresses shown are the addresses that the commands use by default. Possible default addresses are:- [.,.]
- Indicates a two-address line range defaulting to the current line.
- [1,$]
- Indicates a two-address line range defaulting to the entire file.
- [.+1]
- Indicates a single address defaulting to the next line.
- address
- An address refers to a line in the text being edited. An address
can be an expression involving the following forms:
- .
- The value of dot; that is, the current line.
- n
- A line number that indicates an absolute line in the file; the first line has absolute line number 1.
- $
- The last line in the file.
- +[n]
- n lines forward in the file. If you omit n, it defaults to 1.
- -n]
- n lines backward in the file. If you omit n, it defaults to 1.
- 'x
- The value of the mark x.
- /pat/
- Search for regular expression pat forward from the current line.
- ?pat?
- Search for regular expression pat backwards from the current line.
are three addresses: the first searches for a pattern and then goes three lines further; the second indicates two lines after dot; and the third indicates the 100th line in the file./pattern/+3 ++ 100
- command
- The command is a word, which can be abbreviated.
Characters that are shown in square brackets are optional. For example:
indicates that the append command can be abbreviated to a.a[ppend]
- !
- Some commands have a variant; this is typically toggled with an
exclamation mark (
!
) immediately after the command. - parameters
- Many ex commands use parameters to allow
you to specify more information about commands. Common parameters
include:
- buffer
- Specifies one of the named areas for saving text.
- count
- Is a positive integer, specifying the number of lines to be affected by the command. If you do not specify count, it defaults to 1.
- file
- Is the path name for a file. If file includes
the
%
character, vi replaces that character with the path name of the current file. If file includes the#
character, vi replaces that character with the path name of the alternate file. If you do not specify a file, the default is the current file. - flags
- Indicate actions to be taken after the command is run. It can
consist of leading plus (
+
) and minus (-
) signs to adjust the value of the current line indicator, followed byp
,l
, or#
to print, list, or number a line. Thus:
deletes starting five lines down from dot; six lines are deleted; the current line indicator is set to the following line, then incremented by two; and that line is printed with its line number..+5 delete 6 ++#
ex commands
:
) character.
- ab[breviate] lhs rhs
- Indicates that the word lhs should be
interpreted as abbreviation for rhs. (See Context-dependent movement commands for the definition of word.)
If you enter lhs surrounded by white space
in vi INSERT mode, it is automatically changed
into rhs. If you do not specify any arguments
for the ab command, it displays the abbreviations
that are already defined. Abbreviated names cannot contain
#
or any other form of punctuation. - [.] a[ppend][!]
- Enters ex INSERT mode. Text is read
and placed after the specified line. An input line consisting of one
period (
.
) leaves INSERT mode. If you specify an address of zero, text is inserted before the first line of the file. The current line indicator points to the last line typed.If an exclamation mark (
!
) is specified, theautoindent
option is toggled during input. This command cannot be invoked from vi mode. - ar[gs]
- Displays the current list of files being edited. The current file is shown enclosed by square brackets.
- cd[!] path
- Changes the current directory to path. If you omit path, cd sets the current working directory to the directory identified by the HOME environment variable. If path is a relative path name, cd searches for it using the directories that are specified in the CDPATH environment variable. If path is -, then cd changes to the previous working directory. If you modified the buffer since the last write, vi displays a warning message. You can override this behavior by including the exclamation mark (!).
- [.,.] c[hange][!] [count]
- Deletes the line range that is given and then enters INSERT mode.
If an exclamation mark (
!
) is specified,autoindent
is toggled during input. You cannot invoke this command from vi mode. - chd[ir][!] [path]
- Same as cd.
- [.,.] co[py] addr [flags]
- Copies the line range given after addr. If addr is zero, the lines are inserted before the first line of the file. The current line indicator points to the last line of the inserted copied text.
- [.,.] d[elete] [buffer] [count] [flags]
- Deletes the specified line range. After the line range is deleted, the current line indicator points to the line after the deleted range. A buffer can be specified as a letter a-z. If so, deleted lines are saved in the buffer with that name. If an uppercase letter is specified for buffer, the lines are appended to the buffer of the corresponding lowercase name. If no buffer name is given, deleted lines go to the unnamed buffer.
- e[dit][!] [+line] [file]
- Begins a new editing session on a new file; the new file replaces
the old file on the screen. The text conversion that is specified
on the vi or ex command
(for example, the -B or -W option)
is used. This command is usually invalid if you have modified
the contents of the current file without writing it back to the file.
Specifying an exclamation mark (
!
) goes on to start a new session even you have not saved the changes of the current session.You can specify line as either a line number or as a string of the form /regexp or ?regexp where regexp is a regular expression. When line is a line number, the current line indicator is set to the specified position. When it has the form /regexp, vi searches forward through the file for the first occurrence of regexp and sets the current line indicator to that line. ?regexp is similar to /regexp except that vi searches through the file backwards. If you omit line and do not specify a file, the value of the current line indicator does not change. Otherwise, if a file is specified, the current line indicator is set to either the first or last line of the buffer, depending on whether the command was issued in vi or ex mode.
- ex[!] [+line] [file]
- Begins a new editing session on a new file; the new file replaces
the old file on the screen. The text conversion that is specified
on the vi or ex command
(for example, the -B or -W option)
is used. This command is typically invalid if you have modified the
contents of the current file without writing it back to the file.
Specifying an exclamation mark (
!
) goes on to start a new session even you have not saved the changes of the current session.You can specify line as either a line number or as a string of the form /regexp or ?regexp where regexp is a regular expression. When line is a line number, the current line indicator is set to the specified position. When it has the form /regexp, vi searches forward through the file for the first occurrence of regexp and sets the current line indicator to that line. ?regexp is similar to /regexp except that vi searches through the file backwards. If you omit line and do not specify a file, the value of the current line indicator does not change. Otherwise, if a file is specified, the current line indicator is set to either the first or last line of the buffer, depending on whether the command was issued in vi or ex mode.
- f[ile] [file]
- Changes the current file name to file and
marks it [Not edited]. If this file exists, it
cannot be overwritten without using the exclamation mark (
!
) variant of the write command. - [1,$] g[lobal][!] /pat/ [commands]
- Matches pat against every line in the
given range. On lines that match, the commands are
run. If the exclamation mark (
!
) variant is set, the commands are run on lines that do not match. This is the same as using the vi command.The global command and the undo command cannot occur in the list of commands. A subsequent undo command undoes the effect of the entire global command. In ex mode, multiple command lines can be entered by ending all but the last with a backslash (
\
). Commands that will take input are permitted; the input is included in the command list, and the trailing period (.
) can be omitted at the end of the list. For example,g/rhino/a\ hippo
appends the single linehippo
to each line containingrhino
. delim is an arbitrary, nonalphabetic character. The total length of a global command list is limited (see Limits). - [.] i[nsert][!]
- Enter ex INSERT mode, reads text and places it before the specified line. Otherwise, this is identical to the append command. This command cannot be entered from vi mode.
- [.,.+1] j[oin][!] [count] [flags]
- Joins together the lines of text within the range. Unless an exclamation mark (!) is specified, all white space between adjacent joined lines is deleted. Two spaces are provided if the previous line ended in a period, no spaces if the joined line begins with a opening parenthesis, and one space otherwise.
- [.] k x
- Synonymous with the mark command.
- [.,.] l[ist] [count] [flags]
- Displays the line range in a visually unambiguous manner. This command displays tabs as ^I, and the end of lines as $. The only useful flag is #, for line numbering. The current line indicator points to the last line displayed.
- map[!] lhs rhs
- This defines macros for use in vi. The lhs is
a string of characters; whenever that string is typed exactly, vi behaves
as if the string rhs had been typed. If lhs is
more than one character long, none of the characters are echoed or
acted on until either a character is typed that isn't in the lhs (in
which case all the characters up to that point in the lhs are
run) or the last character of lhs is typed.
If the variable
remap
is set, rhs itself can contain macros. If the flag!
is specified, the map applies within vi INSERT mode; otherwise it applies to command mode. A map command with no arguments lists all macros currently defined. - [.] ma[rk] x
- Records the specified line as being marked with the single lowercase letter x. The line can then be addressed at any point as 'x.
- [.,.] m[ove] [addr] [flags]
- Moves the specified line range after the addr given. If addr is zero, the text is moved to the start of the file. The current line indicator is set to the last line moved.
- n[ext][!] [+command] [file ...]
- Begins editing the next file in the file list (where the file
list was either specified on the command line or in a previous next command). The
text conversion that is specified on the vi or ex command
(for example, the -B or -W option)
is used. If the current file has been modified since the last
write, ex typically prevents you from leaving
the current file. You can get around this by specifying an exclamation
mark (
!
). If theautowrite
is set, the current file is written automatically and you go to the next file. If a list of files is specified, they become the new file list. If necessary, expressions in this list are expanded. Thus:
sets the file list to all the files in the current directory with names ending innext *.c
.c
(typically C source files). - [.,.] nu[mber] [count] [flags] [.,.] # [count] [flags]
- Displays the specified line range with leading line numbers. The current line indicator points to the last line displayed.
- [.] o[pen] [pat] [flags]
- Enters open mode, which is simply vi mode with a oneline window. If a match is found for the regular expression pat in the specified line, then the cursor is placed at the start of the matching pattern.
- pre[serve]
- Saves the current buffer in a form that can later be recovered using the -r option on the recover command. vi sends you mail telling you that you can recover this file and explains how to do so.
- [.,.] p[rint] [count] [flags]
- Displays the specified line range. The current line indicator points to the last line displayed.
- [.] pu[t] [buffer]
- Pastes deleted or yanked lines back into the file after the given
line. If no buffer name is given, the most recently changed buffer
is used.
Because the edit command does not destroy buffers, you can send that command in conjunction with put and yank to move text between files.
- q[uit][!]
- Exit from vi or ex.
If the current file has been modified, an exclamation mark (
!
) must be used or you cannot exit until you write the file. - [.] r[ead][!] [file]
- Reads the contents of file and inserts
them into the current file after the given line number. The text
conversion that is specified on the vi or ex command
(for example, the -B or -W option)
is used. If the line number is 0, the contents of the given file
are inserted at the beginning of the file being edited. If the current
file name is not set, a file must be given,
and it becomes the current file name; otherwise, if a file is
given, it becomes the alternate file name. If the file begins
with an exclamation mark (
!
), then it is taken as a system command. Pipes are used to read in the output from the command after the given line number. - rec[over] [file]
- Attempts to recover file if it was saved as the result of a preserve command or a system or editor crash. If you do not specify file, this command displays a list of all recoverable files.
- rew[ind][!]
- Rewinds the file argument list back to the beginning and starts
editing the first file in the list. The text conversion that is
specified on the vi or ex command
(for example, the -B or -W option)
is used. If the current file has been modified, an exclamation
mark (
!
) must be specified; otherwise, you cannot leave the current file until you have written it out. Ifautowrite
is set, the current file is written out automatically if it needs to be. - se[t] [parameter-list]
- Assigns or displays the values of option variables, If you do
not specify a parameter list, set displays
all the variables with values that have changed since the editing
session started. If the parameter all is
specified, ex displays all variables and
their values. You can use the parameter list to set or display each
of many variable values. Each argument in the list is a variable name;
if it is a Boolean variable, the value is set on or off depending
on whether the name is prefixed by
no
. Non-Boolean variables alone in an argument are a request to display their values. A Boolean variable's value can be displayed by following the name by a question mark (?
). You can set numeric or string variables with: name=value In a string variable, spaces must be preceded by a backslash. For example:
shows the value of the readonly flag, sets noautowrite, and sets the shell toset readonly? noautowrite shell=/bin/sh
/bin/sh
.
shows the value of theset report report=5
report
variable, and then set the value to 5. For more details. See Setting the vi options for more details. - sh[ell]
- Invokes a child shell. The environment variable SHELL is used to find the name of the shell to run.
- so[urce] file
- Runs editor commands from file. A file being executed with source can contain source commands of its own.
- st[op]
- Suspends the editor session and returns to system level. For more information, see the description of the vi command Ctrl-Z.
- [.,.] s[ubstitute] [/pat/repl/] [options] [count] [flags]
- Searches each line in the line range for the regular expression pat and
replaces matching strings with repl.
Normally, ex only replaces the first matching string in each line. If options contains g [global], all matching strings are changed.
If options contains c [confirm], ex first prints the line with caret (
^
) characters marking the pat matching location; you can then typey
if you want ex to go ahead with the substitution. pat cannot match over a line boundary; however in ex mode, repl can contain a newline, escaped by a preceding backslash (\
). See Regular expressions (regexp) for full information about both pat and repl. If there is no pat or repl, ex uses the most recently specified regular expression or replacement string. You can use any nonalphabetic character in place of the slash (/
) to delimit pat and repl. - su[spend]
- This is synonymous with the stop command.
- [.,.] t addr [flags]
- This is synonymous with the copy command.
- ta[g][!] tagname
- Looks up tagname in the files listed
in the variable
tags
. If the tag name is found in a tags file, that file also contains the name of the file that contains the tag and a regular expression required within that file to locate that tag. If the given file is different from the one you are currently editing, ex normally begins editing the new file. The text conversion that is specified on the vi or ex command (for example, the -B or -W option) is used. However, if you have modified the current file since the last time it was written out, ex does not start editing a new file unless the tag command contains an exclamation mark (!). Ifautowrite
is on, the current file is automatically written out and the new file read in. When the new file is read in, the regular expression from the tags file is invoked with the magic variable off.Tag names are typically used to locate C function definitions in C source files. The first step is to create a
tags
file using the ctags command. After you do this, you can use the ex tag command to look up a particular function definition and go directly to that definition in the file that contains it.All characters in tag names are significant unless the variable
taglength
is nonzero; in this case, only the given number of characters are used in the comparison. - una[bbreviate] lhs
- The abbreviation lhs previously created by abbreviate is deleted.
- u[ndo]
- Undoes the last change or set of changes that modified the buffer. Globals and vi macros are both considered as single changes that can be undone. A second undo undoes the undo restoring the previous state. The edit command cannot be undone, because it cleans up the temporary file which is used to maintain undo information. You cannot undo operating system commands and commands that write output to the file system.
- unm[ap][!] lhs
- Deletes the lhs map. If the flag
!
is used, this applies to the insert mode maps; otherwise it applies to the command mode maps. - [1,$] v /pat/ commands
- This is a synonym for the global command with
the
!
flag; that is, a global for all nonmatching lines. You can use any nonalphabetic character to delimit pat instead of the slash (/). - ve[rsion]
- Displays the current version information for vi or ex.
- [.] vi[sual] [type] [count] [flags]
- Enters vi mode. If no type is
specified, the current line is at the top of the screen. If type is
caret (
^
), the bottom line of the screen is one window before the current line. If type is a minus sign, (-
), the current line is at the bottom of the screen. If type is a period (.
), the current line is in the middle of the screen.You can use the undo command to undo all the changes that occurred during the vi command.
- [1,$] w[rite][!] [>>] [file]
- Writes the given range of lines to file. The
text conversion that is specified on the vi or ex command
(for example, the -B or -W option)
is used. If two right angle brackets (
>>
) are included, the lines are appended to the current contents of the file. If the current file name is not set, a file must be given. This becomes the current file name. Otherwise, file becomes the alternate file name if it is specified. If the file begins with an exclamation mark (!
), then it is taken as a system command. vi writes the given range to the command through a pipe.If a file is given, it must not already exist. The variable
readonly
must not be set. If a file is not given, the file must be edited; that is, it must be the same file as that read in. All these conditions can be overridden by using the flag!
. - [1,$] wn[!] [>>] [file]
- Similar to write, except that it begins editing the next file in the file list immediately afterward (if the write is successful).
- [1,$] wq[!] [>>] [file]
- Similar to write, except that it exits the editor immediately afterward (if the write is successful).
- x[it]
- If you have modified the current file since the last write, performs a write command using the specified range and file name and then terminates.
- [.,.] y[ank] [buffer] [count]
- Copies the given line range to the specified buffer (a
letter from a through z). If
a buffer is not specified, the unnamed buffer is used. Buffers are
not destroyed by an edit command, so yank and put can
be used to move text between files.
Because the edit command does not destroy buffers, you can use that command in conjunction with put and yank to move text between files.
- [.+1]z [type] [count] [flags]
- Displays count lines. If no count is
specified, ex uses the current value of
the
scroll
variable. The lines are displayed with the given line located according to the type. If type is a plus sign (+
), the editor displays the given line and a screen after that. If type is a period (.
), the editor displays a screen with the given line in the middle. If type is a minus sign (-
), the editor displays a screen with the given line at the end. If type is a caret (^
), the editor displays the screen before that. If type is an equal sign (=
), the current line is centered on the screen with a line of hyphens printed immediately before and after it. The current line indicator points to the last line displayed. - [.,.] <[<...] [count] [flags]
- Shifts the line range by the value of the
shiftwidth
variable. If there are multiple left angle brackets (<
), each one causes another shift. The current line indicator points to the last line displayed. If a count is specified, that many lines are shifted. - [.,.] >[>...] [count] [flags]
- Shifts the line range right by the value of the
shiftwidth
variable. If there are multiple right angle brackets (>
), each one causes another shift. The current line indicator points to the last line displayed. If a count is specified, that many lines are shifted. - [range] ! command
- Submits command to be run by the command
interpreter that is named by the SHELL environment variable. If range is
given, the command is invoked with the contents
of that line range as input. The output from the command then
replaces that line range. Thus: 1,$!sort sorts the entire contents
of the file.
Substitutions are made in command before it is run. Any occurrences of an exclamation mark (
!
) are replaced by the previous command line, while occurrences of percentage (%
) and hash mark (#
) characters are replaced with the path names of the current and alternate files, respectively. If any such substitutions actually take place, the new command line is displayed before it is executed. (See the read and write sections in ex command mode for more information about the current and alternative files.)If the file has been modified and the variable autowrite is on, the file is written before calling the command. If autowrite is off, a warning message is given.
- [$] =
- Displays the given line number. The default line number is the last line of the file. The current line indicator is not changed.
- "a line of text
- This is a comment.
- [.,.] & [options] [count] [flags]
- Repeats the last substitute command. If any options, count, or flags are specified, they replace the corresponding items in the previous substitute command.
- [.,.] ~ [options] [count] [flags]
- Repeats the last substitute command. However, the regular expression that is used is the last regular expression; that is, if there has been a search, the search's regular expression is used. The simple substitute with no arguments, or the & command, uses the regular expression from the previous substitute. substitute with an empty regular expression uses the last regular expression, like ~. If any options, count, or flags are specified, they replace the corresponding items in the previous substitute command.
- @ buffer
- Executes each line in buffer as an ex command. If you do not specify buffer or if you specify a buffer named @, the last buffer executed is used.
- Ctrl-D
- Displays the number of lines of text given by the
scroll
variable. The current line indicator points to the last line displayed.
Special characters in ex commands
%
), the character is replaced by the name of the
current file. For example, if you are about to try out a macro and
you are worried that the macro might damage the file, you could issue:
!cp % /tmp
to copy the current file to a safe
holding place. As another example, a macro could use the percentage
character (%
) to refer to the current file.#
),
the character is replaced by the name of the alternate file. The name
of the alternate file can be set with the read command
as described previously. Thus a command like: e #
tells ex to
edit the alternate file. Using an alternate file can be convenient
when you have two files that you want to edit simultaneously. The
command just given lets you flip back and forth between the two files.Setting the vi options
set autowrite
sets
the autowrite option. For options that are flags
(that is, are not numeric), the variables can be turned off by putting no in
front of the name in the set command, as
in: set noautowrite
In the following list,
variables that are off by default are preceded by no
.
The minimal abbreviation of each option is shown after the comma.
Default values are shown after the equal sign (=
).
autoflush
,af
- When this option is set, it holds the maximum number of seconds
of data a user would lose if a system crash occurs. vi flushes
memory out to its temporary files approximately this many seconds,
unless no changes were made to the current edit buffer, or the user
is sitting idle. It allows you to eventually recover a more current
representation of your edit buffer (after the exrecover daemon
and vi -r is run) because it intermittently
updates vi's temporary files that are used
by the exrecover daemon. Note the following comments:
- The default is set to 120 seconds (2 minutes).
- To turn off this option, set
autoflush
to 0. - This option does not affect on read-only files.
- This option is different from the previous
preserve
option because it works with vi's temporary files (whose location is specified by the environment variables: TMP_VI, TMPDIR, or TMP) as opposed to recovered files found in /etc/recover/$LOGNAME.
autoindent
,ai
- When
autoindent
is on and you are entering text, the indentation of the current line is used for the new line. In vi mode, you can change this default indentation by using the control keys Ctrl-D (to shift left) or Ctrl-T to shift right. In ex mode, a tab or spaces can be typed at the start of a line to increase the indent, or Ctrl-D can be typed at the start of the line to remove a level. ^Ctrl-D temporarily removes the indentation for the current line. 0Ctrl-D places the current line at a zero indent level, and the next line has this indent level as well.The size of indent levels is defined by the variable
shiftwidth.
. Based on this value and the value oftabstop
, the editor generates the number of tabs and spaces needed to produce the required indent level.The default is
noautoident
. autoprint
,ap
- When this option is set in ex mode,
the current line is printed after the following commands: copy, delete, join, move, substitute, undo, &, ~, <,
and >. Automatic displaying of lines does not take
place inside global commands.
The default is
autoprint
. autowrite
,aw
- When this option is on, the current file is automatically written
out if it has been changed since it was last written and you have
run any of the following commands: next, rewind, tag,
Ctrl-^ (vi), and Ctrl-] (vi).
Using an exclamation mark (
!
) with any of these commands stops the automatic write.The default is
noautowrite
. beautify
,bf
- When this option is on, the editor discards all nonprinting characters
from text that is read in from files.
The default is
nobeautify
. cdpath
- Used by cd to find relative path names
when changing the directory. You must delimit entries with a colon
(:). If the current directory is to be included in the search, it
must be indicated by a dot (
.
).cdpath
defaults to the contents of the CDPATH environment variable if it exists, or to dot (.
) if it does not. directory, dir
- The editor uses temporary files with unique names under the given
directory. Any error on the temporary files is fatal.
The default is
directory=tmp
. edcompatible
- When this option is on, the editor attempts to make substitution
commands behave in a way that is compatible with the ed editor.
The g and c options on the substitute
commands are remembered and toggled by their occurrence. The r option
uses the last regular expression rather than the last substitute regular
expression. Percentage mark (
%
) as the entire pattern is equivalent to the previous pattern.The default is
nocompatible
. errorbells
,eb
- When this option is on, vi precedes
error messages with the alert character. When it is off, the editor
warns you of an error by displaying a message using a standout mode
of your terminal (such as reverse video).
The default is
noerrorbells
. exrc
- When this option is on, ex and vi access any .exrc files in the current directory during initialization. If it is off, ex and vi ignore such files unless the current directory is the HOME directory.
home
- Used as the destination directory by cd.
If no path is specified,
home
defaults to the contents of the HOME environment variable if it exists, or to the vi startup directory if it does not. ignorecase
,ic
- When this option is on, the case of letters is ignored when matching
strings and regular expressions.
The default is
noignorecase
. linedelete
- vi sets the line delete character automatically
to the current terminal line delete character, as specified by the
user. Within vi, you can set the line delete
character with the
linedelete
variable. The value that you specify is the numeric value of the line delete character. The default is0x15
, the ASCII value for Ctrl-U. Another value is0x18
for Ctrl-X. list
- When this option is on, tabs are displayed as a caret mark (
^
) rather than expanded with blanks, and the ends of lines are indicated with a dollar sign ($
).The default is
nolist
. magic
- When this option is off (nomagic), regular expression
characters
^
\
and$
become the only ones with special meanings. All other regular expression metacharacters must be preceded by a backslash (\
) to have their special meaning.The default is magic.
maxbuffers
- The number of K units (1024 bytes) of memory to be used for the
editor buffers. These are allocated in units of 16 K.
The default is
maxbuffers=512
, but if that is not available upon entry, this is set to the number actually obtained. At least 32 K is needed. This is in addition to the code and data space that is required by vi; this may be as much as 128 K. Changing maxbuffers has no effect. mesg
- When this option is on, ex allows others
to use the write or talk commands
to write to your terminal while you are in visual mode. The command
overrides this variable (see mesg). This option does not affect systems that do not support mesg.mesg n
number
,nu
- When this option is on, line numbers are displayed to the left
of the text that is being edited.
The default is
nonumber
. paragraphs
- This list of character pairs controls the movement between paragraphs
in vi mode. Lines beginning with a period
(
.
) followed by any pair of characters in the list are paragraph boundaries (for example,.IP
). Such lines are typically commands to text formatters like nroff or troff.The default is
paragraphs="IPLPPPQPP LIpplpipbp""
prompt
- When this option is on, ex command mode
prompts with a colon (
:
). No prompts are given if input is not being read from a terminal.The default is
prompt
. pwd
- This is a read-only variable. The value always refers to the current working directory, and can only be changed by the cd command.
quiet
- When this option is on, vi does not
display file information messages.
The default is set by the -s option.
readonly
- When this option is on, vi does not
let you write to the current file.
The default is based on the permissions of the current file. If you do not have write permission on this file, the default is readonly. Otherwise, the default is set by the -R option.
remap
- If this option is on and a
map
macro is expanded, the expansion is reexamined to see if it also containsmap
macros.The default is
remap
. report
- The editor displays a message whenever you issue a command that
affects more than this number of lines.
The default is
report=5
. restrict
- All file names are restricted to the current directory. Subcommands
cannot be called. This variable is automatically set if you invoke
the editor with a command that starts with the letter r,
as in rvi. When the option is turned on,
it cannot be turned off.
The default is
norestrict
. scroll
- This sets the number of lines to scroll for the z ex and
Ctrl-D (ex) commands.
The default is the value of the variable
window
, divided by two. sections
- This list of character pairs controls the movement between sections
in vi mode. Lines beginning with a period
(
.
) followed by any pair of characters in the list are section boundaries (for example,.SH
). Such lines are typically commands to text formatters like nroff or troff.The default is
sections="SHNHH HU"
shell
,sh
- This is the name of the command interpreter to be used for ! commands and the shell command. The default value is taken from the SHELL environment variable.
shiftwidth
,sw
- This sets the width of indent that is used by shift commands and
autoindent
.The default is
shiftwidth=8
. showmatch
,sm
- If this option is on and you type a closing parenthesis or closing
brace in input mode, the cursor moves to the matching open parenthesis
or brace. It stays there for about one second and then moves back
to where you were. This lets you note the relationship between opening
and closing parentheses/braces.
The default is
noshowmatch
. showmode
- When this option is on, vi displays
an indicator in the lower right corner of the screen if you are in
Insert/Open/Change/Replace mode. If no indicator is displayed, you
are in Command mode.
The default is
noshowmode
. tabstop
- Tab stops for screen display in vi mode
are set to multiples of this number.
The default is
tabstop=8
. taglength
,tl
- If this variable is nonzero, tags are only compared for this number
of characters.
The default is
taglength=0
. tags
- The value of this variable should be a list of file names that are separated by a backslash (\)
followed by a space. If there is no backslash before the space, vi treats the
second and subsequent tags as part of an
option=value
combination. For example:set tags=file1\ file2\ file3\
These are used by the tag ex command and the Ctrl-] vi command. The files are typically created with the ctags program.
The default is
tags=tags
. term
- The value of this variable is the terminal type. The TERM environment variable specifies this variable's default value.
terse
- If this option is on, messages are displayed in an abbreviated
form. The default is
noterse
. warn
- When this option is on, commands with an exclamation
mark (
!
) print a warning message if the current file has been modified. No message is printed if this option is off.The default is
warn
. window
- This variable gives the number of text lines available in vi mode or the
default number of lines to display for the command.
The default is given by the -w option. If it is not specified with the -w^^ option, its value defaults to the environment variable LINES or the value found in the terminfo database for TERM.
wrapmargin
wm
- If this variable is nonzero in vi insert
mode, when a line reaches this number of characters from the right
of the screen, the current word moves down to the next line automatically;
you do not have to press ENTER.
The default is
wramargin=0
. wrapscan
,ws
- If this option is off, forward searches stop at the end of the
file and backward searches stop at the beginning.
The default is
wrapscan
writeany
,wa
- If this option is off, the editor does not let a file marked
[Not edited]
overwrite an existing file.The default is
nowriteany
.
Regular expressions
Many ex commands use regular expressions when searching and replacing text. A regular expression (indicated by pat in the command descriptions) is used to match a set of characters.
A
regular expression consists of a string of normal characters that
exactly match characters in a line. These can be intermixed with special
characters (known as metacharacters), which allow matching
in some special manner. Metacharacters can themselves be matched directly
by preceding them with the backslash (\
) character.
If the variable magic is turned off, all but two
of the metacharacters are disabled; in this case, the backslash character
must precede them to allow their use as metacharacters. See Regular expressions (regexp) for examples.
- (circumflex symbol)
- Matches the start of a line. This is only a metacharacter if it is the first character in the expression.
- $
- Matches the end of a line. This is only a metacharacter if it is the last character in the expression.
- .
- Matches any single character.
- *
- Matches zero or more occurrences of the previous expression.
- \<
- Matches the empty string preceding the start of a word. A word is a series of alphanumeric or underscore characters that are preceded by and followed by characters that are not alphanumeric or underscore.
- \>
- Matches the empty string following the end of a word. A word is a series of alphanumeric or underscore characters that are preceded by and followed by characters that are not alphanumeric or underscore.
- [string]
- Matches any of the characters in the class that is defined by string.
For example, [aeiouy] matches any of the vowels. You can put a range of
characters in a class by specifying the first and last characters of the range, with a hyphen
(
-
) between them. For example, in ASCII [A-Za-z] matches any upper or lowercase letter. If the first character of a class is the caret (^
), the class matches any character that is not specified inside the square brackets. Thus, in ASCII[a-z_][^0-9]
matches a single alphabetic character or the underscore, followed by any nonnumeric character. - \( ...\)
- A set of characters in the pattern can be surrounded by escaped parentheses. See the description of \n in Replacement patterns. This is not affected by the setting of magic.
- ~
- Matches the replacement part of the last substitute command.
Replacement patterns
- &
- Is replaced by the entire string of matched characters.
- ~
- Is replaced by the entire replacement pattern from the last substitute.
- \n
- Is replaced by the string that matched the nth occurrence of a
\(...\)
in the regular expression. For example, consider:
Thes/\([a-zA-Z]*\)our/\lor/
\l
represents the string that matched the regular expression\([a-zA-Z]*\
. Thus, the previous command might change the wordcolour
tocolor
. - \u
- Changes the next character in the replacement to uppercase.
- \l
- Changes the next character in the replacement to lowercase.
- \U
- Changes the following characters in the replacement to uppercase.
- \L
- Changes the following characters in the replacement to lowercase.
- \E, \e
- Turns off the effects of \U or \L.
Initializing the editor
Initialization code consists of one or more ex commands that run when the editor starts up. Initialization code can be obtained in several ways:
- If there is an environment variable named EXINIT with a nonnull value, it is assumed to hold initialization code. vi executes this code using an ex source command.
- If EXINIT does not exist or has a null value, the editor tries to find a file named .exrc. If you have an environment variable named HOME, the value of this variable is assumed to be the name of your home directory. vi runs the .exrc file using an ex source command.
- If tEXINIT or the $HOME/.exrc file sets the option variable
exrc
and if there is a file named .exrc under the current directory, it is assumed to hold initialization code. vi runs this code using an ex source command.
All .exrc files must be owned by the same user ID that invoked the vi command, and must only be writable by that user ID. Typical permissions for a .exrc file would be 744.
The .exrc file is read as if it were a sequence of keystrokes typed at the beginning of an ex session. As a result, the contents of .exrc must be the same as the characters you would type if you were in vi or ex. In particular, if the input contains an unusual character (for example, a carriage return) that you would typically precede with Ctrl-V, there must be a Ctrl-V in the .exrc file. If you are creating an .exrc file with vi, you must type Ctrl-V Ctrl-V to put a Ctrl-V character into your initialization file, then Ctrl-V followed by the special character to put the special character into your initialization file. The .exrc file must show both Ctrl-V and the special character. A command specified in the .exrc file can be ignored (treated as a comment) by beginning that line with a double quotation mark (").
Files
vi uses the following files:
- /tmp
- Directory used for temporary files if TMP_VI, TMPDIR, and TMP are not defined.
- /tmp/VInnnnn.mmm
- Temporary files.
- .exrc
- Startup file.
Localization
- LANG
- LC_COLLATE
- LC_ALL
- LC_CTYPE
- LC_MESSAGES
- LC_SYNTAX
- NLSPATH
Environment variables
vi uses the following environment variables:
- _TEXT_CONV
- Contains text conversion information for the command. The text conversion information is not used when either the -B option or the filecodeset or pgmcodeset option (-W option) is specified. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.
- CDPATH
- Contains a list of directories to be searched.
- COLUMNS
- Contains the number of columns between the left and right margins (see option variable wrapmargin). This is also used as the horizontal screen size.
- ENV
- Contains the path name of a file containing KornShell commands. When you invoke sh, it executes this file before doing anything else.
- EXINIT
- Contains a list of vi commands to be run when the editor is started up.
- HOME
- Contains the directory to be searched for the editor startup file.
- LINES
- Contains the number of lines in a screen (see option variable windows). This is also used as the vertical screen size.
- PATH
- Contains a list of directories to be searched for the shell command specified in the ex commands read, write, and shell.
- SHELL
- Contains the name of the command interpreter for use in !, shell, read, write, and other ex commands with an operand of the form !string. The default is the sh utility.
- TERM
- Contains the name of the terminal type.
- TERMINFO
- Contains the path name of the terminfo database.
- TMPDIR
- Contains the path name that the shell uses as the directory for temporary files.
- TMP_VI
- Contains a directory
path name that can be specified by an administrator as a location
for vi's temporary files. This is useful
if the current default directory for these files (typically /tmp)
is implemented as a TFS. In this case, all vi's
temporary files that the exrecover daemon
uses for recovery would be gone after a system crash.
This environment variable should be set by a system administrator as opposed to a user setting it for their environment. If the latter occurs, and the user sets the TMP_VI directory to something different from what exrecover recognizes as TMP_VI, the user will need to run the exrecover daemon manually to allow the temporary files to be converted to the recoverable files used by vi
(located in /etc/recover/$LOGNAME).When you set TMP_VI, follow these restrictions:
- Do not set TMP_VI to /etc/recover/$LOGNAME
- Do not set TMP_VI to any directory where a path name component is an environment variable with a user's value different from the initialization process's value (for example $HOME). The temporary files that are connected with vi are converted into a form recoverable by vi when exrecover is run during IPL. Because exrecover is issued during IPL, it is owned by the initialization process and will contain different values for certain environment variables, if those environment variables are set. Throughout the file system, there might exist some temporary files that can only be converted by exrecover. This conversion can be done manually by a system administrator (to recover files that are owned by all users) or by a single user (to recover only their own files).
Exit values
0
- Successful completion.
1
- Failure due to any of the following reasons:
- The code set is not valid.
- Could not turn off automatic conversion.
- Could not perform requested text conversion.
- Unknown option.
- No such command from open/visual.
- Missing lhs.
- Missing file name.
- System does not support job control.
- Write forms are
w
andw>>
. - Internal error: bad seek pointer.
- Internal error: Line out of range.
- Internal error: line too long.
- Nonzero address required on this command.
- No lines in the buffer.
- Nothing to undo.
- Cannot escape a newline in global from visual.
- Global command too long.
- Argument list too long.
- File is read-only.
- No previous command to substitute for
!
- Command too long.
- No previous regular expression.
- Buffers are 1-9, a-z.
- Line too long.
- System does not support job control.
- Digits required after
=
. - Nothing in buffer.
- Missing rhs.
- Too many macros.
- Recursive map expansion.
- Nothing to repeat.
- Last repeatable command overflowed the repeat buffer.
- Bad tag.
- No tags file.
- No such tag in tags file.
- Negative address. The first buffer line is 1.
- Not an editor command.
- Unimplemented ex command.
- Wrong number of addresses for command.
- Mark requires following letter.
- Undefined mark referenced.
- Global within global not allowed.
- First address exceeds second.
- Cannot use open/visual unless open option is set.
- Regular expression
\
must be followed by/
or?
. - No address allowed on this command.
- No more files to edit.
- No current file name.
- Extra characters at end of command.
- Not that many lines in buffer.
- Insufficient memory.
- Restricted environment.
- Command too long.
- Trailing address required.
- Destination cannot straddle source in
m
andt
. - No file name to substitute for
%
. - No alternate file name to substitute for
#
. - File name too long.
- Too many file names.
- Argument buffer overflow.
- Incomplete shell escape command.
- Regular expressions cannot be delimited by letters or digits.
- No previous scanning regular expression.
- No previous substitute to repeat.
- Cannot escape newlines into regular expressions.
- Missing
[
. - Badly constructed regular expression.
- No remembered regular expression.
- Line overflow in substitute.
- Replacement pattern contains \d; cannot use in regular expression.
- Replacement pattern too long.
- Regular expression too complicated.
- Cannot escape newline in visual.
- No such set option.
- String too long in option assignment.
2
-
- Unknown command-line option.
- Missing or incorrect num in an -n option.
Limits
- Maximum number of lines: 65 279 (64 K - 256 - 1).
- Length of longest line: {LINE_MAX}bytes, including the newline.
- Longest command line: 256 bytes.
- Length of file names: 128 bytes.
- Length of string options: 64 bytes.
- Length of remembered regular expressions: 256 bytes.
- Number of map, map!, and abbreviate entries: 64 each.
- Number of saved keystrokes for . in vi: 128.
- Length of the lhs of map, map!, or abbreviate: 10 bytes.
- Maximum number of characters in a tag name: 30.
- Number of characters in a
:
escape from vi: 128. - Requires 128K of memory plus the set option
maxbuffers
K of auxiliary memory. During startup,maxbuffers
is changed to reflect available memory; at least 32K is required. - Number of nested source files is 3.
Portability
POSIX.2 User Portability Extension, X/Open Portability Guide, UNIX systems.
The -B, -e, -s, -v, and -W options are extensions of the POSIX standard.
Related information
ed, ex, fg, jobs, msg, sed, talk, tee, write
See Regular expressions (regexp) for more information about regexp.