vi is a display-oriented text editor.
Only users who have accessed the POSIX shell via rlogin can use vi (except in line mode).
If you call vi on a block-mode terminal, the ex editor will automatically be started instead.
Layout of this description
The description of vi is divided into eight main parts:
vi - screen oriented (visual) display editor (POSIX Commands 2022-06, #230)
Command mode
Input mode
Last line mode
ex command mode
ex input modevi - screen oriented (visual) display editor (POSIX Commands 2022-06, #230)
Saving the editor buffer and quitting vi
Text buffers
Movement commandsvi - screen oriented (visual) display editor (POSIX Commands 2022-06, #230)
Definitions
Control commands
Commands of the vi command modeCustomizing the vi environment
Environment variables
Command overview
The following overview summarizes the most common vi commands, grouping them together by function. In the section entitled Commands you will find a detailed description of the commands in alphabetical order.
Invoking vi
vi
<enter>
Call vi with empty editor buffer
vi file<enter>
Call vi and load file
Quitting vi
ZZ
Quit vi, writing editor buffer out to current file; as :wq
:w
<enter>
Write editor buffer to current file
:w datei<enter>
Write editor buffer to file
:q
<enter>
Quit vi if changes saved
:q!
<enter>
Quit vi, discarding unsaved changes
:wq
<enter>
Quit vi, writing editor buffer out to current file; as ZZ
Positioning the cursor
h
One character to left
j
One line down in same column
+
One line down and to start of line
<enter>
(enter key)
One line down and to start of line
k
One line up in same column
-
One line up and to start of line
l
One character to right
H
Top of screen
M
Middle of screen
L
Bottom of screen
0
Left edge of screen
|
Left edge of screen
$
Last character in line
^
First non-whitespace character in line
w
Start of next word
e
End of next word
b
Start of previous word
W
Start of next word (extended word)
E
End of next word (extended word)
B
Start of previous word (extended word)
Positioning the window
CTRL+D
Half a screen down
CTRL+U
Half a screen up
CTRL+F
One screen down
CTRL+B
One screen up
CTRL+E
One line down
CTRL+Y
One line up
z
<enter>
Current line to top of screen
z.
Current line ot middle of screen
z-
Current line to bottom of screen
1G
Start of file
nG
Line n to middle of screen
G
End of file
Deleting and restoring text
x
Delete character at cursor position
X
Delete character to left of cursor
dd
Delete current line
dG
Delete from current line to end of file
dH
Delete from start of file to current line inclusive
D
Delete rest of line from cursor position
u
Restore deleted text from standard buffer
U
Restore current line
p
Write buffer contents after current cursor position
P
Write buffer contents before current cursor position
Changing text
r
Replace current character with next character entered
R
Overwrite text starting at current character
o
Insert blank line below current line
O
Insert blank line above current line
a
Append text after current character
A
Append text to end of line
i
Insert text before current cursor position
I
Insert text at start of line
Copying
Y
Yank current line
yy
Yank current line
Yp
Duplicate current line
yyp
Duplicate current line
:10,13t 20
Copy lines 10 through 13 below line 21
:.t.
Duplicate current line
:t.
Duplicate current line
:r datei
Copy contents of file below current line
Cut and paste
3dd
Delete 3 lines including current line
p
Insert saved lines
"a3dd
Delete 3 lines and save in buffer a
"ap
Insert saved line below current line
Search and replace
/xyz
Search forward for xyz
/xyz /
Search for xyz and 2 blanks
:s/xyz/abc/
Replace xyz with abc in current line
:/alt/s/old/new
Find old, then replace old with new
:1,$s/old/new/g
Globally replace old with new
Introduction
vi displays a maximum of 23 lines of the file being edited. You can look at different parts of the file by moving this window. vi offers a wide range of text editing options. You can
create, modify, copy and delete text
position the cursor using simple commands (e.g. at the beginning or end of a word, line, sentence, paragraph, or the file)
search and replace text patterns using regular expressions
call a subshell
process a range of the current file with a POSIX command
edit multiple files during the same session and copy text from one file to another
recover a file after an interrupted session.
vi is an enhanced version of the ex line editor (see ex). You can switch between the two editors while editing and you can execute ex commands from within vi.
vi modes
The vi editor provides you with a number of modes in which you can operate:
vi command mode
vi input mode
vi last line mode
ex command mode
ex input mode.
When invoked, the editor always comes up in the vi command mode, from which you can switch to other modes if required. The mode of the editor determines how keystrokes are interpreted.
vi command mode
When vi is invoked, it comes up in the vi command mode. No text can be entered in this mode. Keystrokes are immediately interpreted as vi commands, without being displayed on the screen. If the entered command is legal, it is executed, and the result is immediately displayed on the screen.
vi input mode
The input mode allows you to enter or modify text in the editor buffer. The editor switches to this mode when you enter one of the vi commands A, a, C, c, I, i, O, o, S, s or R (see the section entitled “Commands of the vi command mode”). All following input characters, including various non-printing characters, are written to the buffer and the screen.
Most vi commands cannot be used in input mode. There are, however, a few vi commands that have a special significance in input mode. These include:
ESC
Exit input mode; return to vi command mode.
CTRL+C
Terminate input mode; return to vi command mode.
<enter>
(Enter key)
Newline.
CTRL+@
Repeat last text input. You enter this command immediately after switching to input mode. vi inserts the last text entered in input mode provided this text consists of no more than 128 characters. Control is immediately switched back to command mode.
CTRL+D
If you have defined automatic indentation, you can use CTRL+D to move the margin one tab stop to the left. You can set the shift width with the shiftwidth option. This command is only relevant at the beginning of a new input line and with the autoindent option set.
CTRL+H
Go back one character.
CTRL+T
One tab to the right in accordance with the value of shiftwidth. This command may only be entered at the start of a new input line.
CTRL+V
Insert a non-printing character.
CTRL+W
Go back one word.
\
Escape for rubout character.
vi last line mode
In the vi last line mode, you can enter vi, ex, and POSIX commands in the status line. The last line mode is activated by the input of a colon (:), slash (/), or question mark (?). Another method is to enter an exclamation mark (!), followed by a cursor movement command to a line. Commands entered in the last line mode must be terminated with the enter key or ESC. The input of a command can be aborted by pressing CTRL+C.
Caution!
Entering ESC in the vi last line mode does not delete a command just entered, but causes it to be executed instead. In vi last line mode you must use CTRL+C to delete an entered command.
:
Entering a colon in the vi command mode causes all following input up to the next ESC or enter key to be interpreted as an ex command. (All ex commands are described under ex, ex commands.) After the colon is entered, the cursor moves to the status line at the bottom of the screen, and the colon as well as the input command are displayed there. Almost all ex commands can now be used, except for those that would switch ex to the ex input mode.The percent character (%) represents the name of the file currently in the buffer; the hash character (#) stands for the name of the file last processed during the same session. (see section “Current and alternate file” (ex command and display editor)).
Example
:! diff # %<enter>
vi writes the differences between the previously edited file and the current file on standard output. Neither of the two files is changed.
/
or
?
When a slash or question mark is entered, the cursor moves to the status line, allowing you to specify a regular expression for a forward search (with /) or a backward search (with ?). The vi command n repeats the search in the same direction; N repeats the search in the reverse direction.
!
When an exclamation point is entered in the vi command mode and a movement command specifying a line is given to indicate the scope, the cursor moves to the status line. The exclamation mark is then displayed at the start of the status line. If you enter a second exclamation mark as the movement command, the position is the current line. The movement command is not displayed. You can now enter a POSIX command, which must be terminated with the enter key or ESC. The scope, i.e. the range from the current line to the specified position, becomes the input of the POSIX command (if it reads from standard input) and is replaced by its output (standard output and standard error). If no output is provided by the command (e.g. true), the specified range is deleted, i.e. replaced by a "null" string.
ex command mode
You switch to ex command mode by entering the command Q in vi command mode. You thus quit the display-oriented vi and switch to the line-oriented ex editor. Entering the command vi <enter>
in ex command mode returns you to vi.
ex input mode
You switch to the ex input mode by entering the command a, i or c in the ex command mode. All input in this mode is written to the editor buffer. To terminate this mode you enter a single period in the first column of a line.
Most ex commands can be entered in one of two ways:
from within vi command mode, by entering the vi command colon : followed by the ex command required.
from within ex command mode.
Program invocation
When invoked, the vi editor comes up in the vi command mode. If you have switched to vi input mode, pressing ESC (or CTRL+C) will take the editor back to the vi command mode.
Syntax
vi
[ option]... [ file]... |
The file containing the tag is loaded for editing. The editor positions the line containing the definition of the tag in the center of the screen. A tags file containing the search strings for the definition must be in the same directory. This option is typically used by for C programmers who want the editor to be positioned on the definition of a function or macro as soon as the file is opened. The tags file needed for this purpose must previously have been created with the ctags command.
The lisp mode is activated, and the text is correspondingly indented to comply with typical Lisp programming conventions.
Restores a vi session if the vi editor or the system crashed during your previous session.
Lists the names of all the files saved following a system or editor crash.
Opens the named file in read-only mode. This mode prevents the original file from being accidentally overwritten. Caution!
Sets the window size to n text lines. -w
This option enables you to invoke vi and directly execute an ex command or use a vi movement command to position the editor at a specific line of the file being edited. If you use a movement command, the line required will be displayed in the center of the screen. By contrast, if you execute an ex command, the editor will be positioned at the last line of the file after the command is executed, unless the ex command you specify (e.g. search) positions the editor itself. command not specified: command specified:
n is an integer. vi is positioned to the nth line of the file.
vi is positioned to the line containing pattern. pattern is a simple regular expression (see Tables and directories, Regular expressions). If the specified pattern includes metacharacters, you must escape such characters with a backslash \ or enclose pattern in single quotes ’pattern’, so that the shell does not interpret the metacharacters.
ex-command can be any arbitrary ex command. The ex command must be quoted (using single or double quotes) in order to prevent it from being interpreted by the shell. If the editor is not already positioned as a result of an ex command, vi will position to the last line of the file. Example When vi is called with:
the file appointments is opened, the cursor is position on the last line of the file, and the line numbers and the vi mode are displayed on the screen (see example in ex description).
Name of the file you want to edit. If you give a number of files, vi begins with the first file specified. The ex command n can be used to switch to the next file (see also ex commands rew and ar). file not specified: |
Acoustic signal (not for block-mode terminals)
An acoustic signal is sent to the terminal when
you press the ESC or CTRL+C keys in command mode
you use an illegal command
vi receives a SIGINT signal.
If the vi editor receives a SIGINT signal (CTRL+C) during text input, or during the input of a command on the bottom line, the input is terminated (or the command cancelled) and the editor returns to the command mode. The receipt of a SIGINT signal in the command mode produces an acoustic signal.
Screen layout
A character screen on a terminal is generally made up of 24 lines and 80 columns. By default, the first 23 screen lines are used to display the text of the file being edited. In xterm sessions more than 24 lines can be displayed. In this case the number of screen lines that vi uses is determined automatically.
The last line on the screen is a status and command line. The status line is used to enter commands and to display information and messages from the vi editor.
Text lines that do not fit on the screen are continued in the next line.
vi uses two special characters as markers in column 1 to identify the line status.
~
The tilde (~) identifies lines that are present on the screen but not in the editor buffer. These are lines past the end of the file. If the last text line of a file is located in the middle of the screen, for example, all lines thereafter will be marked with ̃. The tilde disappears as soon as you enter text into the first such marked line. When you invoke vi with a new or empty file, all screen lines except for the first and last line (status line) are marked with this character.
@
The commercial at (@) marker indicates lines which vi cannot display correctly on the screen. There are two possible causes:
A text line may extend over more than one line on the screen. This means that text lines may be present in the editor buffer, but the space on the screen after the last fully displayed text line does not suffice for another full line of text.
Deleting individual lines from a file can mean that what appears on the screen no longer matches the contents of the editor buffer. In this case, the vi commandL can be used to redraw the screen.
The last line on the screen is the status line. It is used:
to enter vi commands that begin with
/
,?
,!
or:
to display messages
for the output of ex commands
to display the mode (the command mode is not displayed).
Functionality
vi always works on a buffer, not on files. When you start a vi session, a copy of the file you are editing is read into the editor buffer. If you omit the file name or specify a name that does not exist, you will begin with an empty buffer. All your editing changes will initially be recorded in this buffer; the original file is not changed until you copy the buffer contents back to it. This is done by explicitly saving the buffer during the session (with the ex command w) or while quitting the vi editor (with the ex commands wq, x, or the vi command ZZ). Use the ex command q! if you prefer to quit vi without modifying the original file. If you are creating a new file, it will not be created until you explicitly save the buffer contents to this file.
Saving the editor buffer and quitting vi
vi must be in vi command mode in order to save the editor buffer or quit vi.
You save the contents of your editor buffer to the edited file or to a specified file by means of the following ex command (see Functionality of vi, Entering ex commands):
:w[ file]
(write, ex command)
The contents of the editor buffer are saved to the specified file.
file not specified:
The contents of the editor buffer are saved to the edited file.
You have the following ways of quitting vi:
:q
(quit, ex command)
Quit vi. This command functions only if no changes have yet been made to the editor buffer or if the modified editor buffer was saved to a file.
:q!
(quit, ex command)
Quit vi. Changes made to the editor buffer are lost.
:x
(exit, ex command)
Quit vi and write the modified editor buffer to the current file.
ZZ
(vi command)
Quit vi and write the modified editor buffer to the current file.
:wq[ file]
(write and quit, ex command)
Quit vi and write the editor buffer to the specified file.
file not specified:
The contents of the editor buffer are written to the current file.
Text buffers
The vi editor makes use of
one temporary buffer
9 numbered buffers
26 named buffers
The contents of these buffers can be accessed by using vi commands in the vi command mode.
Temporary buffer
The last change that you make in the editor buffer is stored by vi in the temporary buffer. If, for example, you have deleted a line, the buffer will contain this line. It is used by the vi commands U and u (undo). The u command therefore allows you to reverse the effect of the last command that changed the editor buffer. u also reverses the effect of u.
If the last vi command was a copy (y), delete (d), or replace (except R and r) command, the temporary buffer can also be accessed by using the vi commands P and p (put).
P and p copy the contents of the temporary buffer before (P) or after (p) the current line. Unlike U (u), P (p) does not modify the temporary buffer. This allows you to use P (p) several times in order to insert the same text at multiple locations in the file.
Caution!
The contents of the temporary buffer are overwritten by each command that changes the editor buffer. The temporary buffer cannot therefore be used to copy text from one file to another.
The only commands that may be used between a copy, delete, or replace command and the vi command P (p) are cursor movement commands (see “Movement commands"). This is because other commands would change the contents of the temporary buffer.
If vi is in input mode and you use one of the arrow keys, the vi command U (u) cannot be subsequently used to undo editing changes.
Numbered buffers
When you delete one or more lines (vi command d), the text you delete is copied to the first of nine numbered buffers. If you now delete some more lines, the contents of buffer 1 are copied to buffer 2, and the most recently deleted lines are again preserved in buffer 1. Thus, buffer 1 always contains the most recently deleted material, buffer 2, the text from the previous delete, and so on, until buffer 9, which contains the material deleted nine steps ago.
The contents of numbered buffers can be retrieved using the vi commands P and p. Numbered buffers are referenced by a double quote and the number of the buffer in question. For example: "4P
This vi command inserts the contents of buffer number 4 before the current line (or current position of the cursor).
The vi dot (.) command recovers the contents of the buffer from the next numbered buffer, provided your last vi command also retrieved text from a numbered buffer. In the above example, you would thus retrieve the contents of buffer number 5. The contents of numbered buffers are not lost when you switch files (with ex command n or e).
Named buffers
A copy (y) or delete (d) command can be used to write text into one of the twenty-six named buffers. These buffers are designated in lowercase from a-z or uppercase from A-Z. Lowercase and uppercase letters refer to the same buffer, however, with the following difference.
When you use a lowercase letter, the old buffer contents are overwritten by the new text, i.e. are deleted. By contrast, when uppercase letters are used, the old buffer contents are retained, and the new text is appended to the old. The vi editor does not change the contents of a named buffer during an editor session unless you explicitly overwrite its contents. vi writes text into a named buffer when you enter a double quote, followed by the buffer name and one of the vi commands d or y.
Examples
"A10dd
Delete (dd) the current line and 9 additional lines (10) and append them to the contents of buffer A.
"by4w
Copy (y) the current and the next three words (4w) into buffer b.
"cCstring
ESC
Overwrite (C) the current line from the cursor position to the end of the line with string, and save the overwritten text in buffer c.
"Ap
Recover the text from buffer A and insert (p) into the file after the current line or position.
Movement commands
vi provides you with a large number of commands, including search commands, which control the position of the cursor on the screen. These "movement" commands are entered in the vi command mode. The following overview lists the most important movement commands. The definitions of paragraph, section, sentence, word and extended word are given in the section "Definitions" .
Character-oriented positioning
[n]
CTRL+H
n characters to the left
[n]h
n characters to the left
[n]l
n characters to the right
0
to the start of a line
^
to the first non-whitespace character in the line
[n]|
to the first or nth column of the line
$
to the last character in the line
[n]fchar
forward to the nth instance of char
[n]Fchar
back to the nth instance of char
[n]tchar
forward to the position to the left of the nth instance of char
[n]Tchar
back to the position to the right of the nth instance of char
Word-oriented positioning
[n]b
back to the beginning of the nth previous word
[n]B
back to the beginning of the nth previous extended word
[n]e
forward to the end of the nth word
[n]E
forward to the end of the nth extended word
[n]w
forward to the beginning of the nth word
[n]W
forward to the beginning of the nth extended word
Line-oriented positioning
[n]k
up n lines in the same column
[n]j
down n lines in the same column
[n]
CTRL+J
down n lines in the same column
[n]
<enter>
down n lines to the first non-whitespace character
[n]+
down n lines to the first non-whitespace character
[n]-
up n lines to the first non-whitespace character
[line]G
to the specified line number or end of file (if no line is given)
[n]$
to the last character of the nth line
Positioning by sentences, paragraphs, and sections
[n](
back to beginning of nth previous sentence
[n])
forward to beginning of nth sentence
[n]{
back to beginning of current paragraph
[n]}
forward to beginning of next paragraph
[n][[
back to nth previous section boundary
[n]]]
forward to nth section boundary
%
to the matching parenthesis or brace in C source programs.
Screen-oriented positioning
[n]H
to the first character in the nth text line on the screen
M
to the middle of the screen
[n]L
to the first character in the last text line on the screen
[line]z+
current line or specified line to top of screen
[line]z.
current line or specified line to middle of screen
[line]z-
current line or specified line to bottom of screen
Searching for patterns and markers
/pattern
forward to pattern
?pattern
back to pattern
'marker
to marked line (beginning of line)
`marker
to marker
Commands
When invoked, the vi editor comes up in the vi command mode. If you have switched to vi input mode, pressing the ESC (or CTRL+C) key will take the editor back to the vi command mode.
As described above, you can enter ex commands either in vi command mode or in ex command mode. Only the vi commands are described below. ex commands are described under ex.
Hitting ESC or CTRL+C in the vi command mode cancels a partial command. ESC sounds an acoustic signal for character terminals if the editor is not in input mode or if there is no partially entered command.
Caution!
Entering ESC in the vi last line mode does not delete a command just entered, but causes it to be executed instead. In vi last line mode you must use CTRL+C to delete an entered command.
Unless otherwise specified, the commands are valid only in vi command mode and have no special effect in input mode.
Definitions
Extended word
The term "extended word" refers to any sequence of characters which does not include any blanks, tabs, or newline characters. See Word.
Paragraph
A paragraph is defined by the value of the ex option paragraphs. Blank lines and lines in which a section begins are also treated as paragraph boundaries.
Section
A section is defined by the value of the ex option sections. Lines that begin with the form-feed character (CTRL+L) or a left brace { are also treated as section boundaries.
If the ex option lisp has been set, any left parenthesis at the start of a line is also treated as a section boundary.
Sentence
A sentence end is defined by
a period (.),
an exclamation point (!), or
a question mark (?)
followed by at least two spaces (to avoid confusion with abbreviations) or a newline character. To allow for quotations, any right parentheses, right square brackets, or single or double quotes between the punctuation and the newline character or spaces are ignored.
Word
A word is either any sequence of alphanumeric characters or a string of special characters. A word is terminated by a blank, tab, newline character or the following word.
See "Extended word".
Examples
((!!++;-.
is one word
me&you2
are 3 words (me, &, you2), but one extended word
String
Any sequence of characters.
Control commands
To enter a control command, you hold down CTRL and press the key for the command required. Many control commands can be prefixed by a positive integer which specifies a count. The square brackets in the description signify that the specification of a count is optional. You must not press CTRL whilst entering count. The specified count is not displayed, but influences the effect of the subsequent command. If you specify an incorrect value, you can cancel your input by pressing ESC. The count is used to:
indicate the range of text to be affected in a movement command, e.g.
5
CTRL+U
scrolls back the screen by five lines.specify a repetition factor, e.g.
4
CTRL+B
scrolls four screen pages back.
Unless explicitly stated otherwise, the default value for count is one.
CTRL+@
In vi input mode:
If you enter this and nothing else, the last text input is repeated at the position at which you enter CTRL+@. Afterwards, control is automatically returned to command mode.
CTRL+@ only functions for a maximum of 127 characters.
[count]
CTRL+B
(backward) Scrolls backward by 21 screen lines to display the window above the current one. A count can be used to indicate the number of windows the editor should go back. If possible, two overlapping lines from the previous screen are displayed in the new window.
[count]
CTRL+D
In vi command mode:
count not specified:
count = 11, or last count used with CTRL+D or CTRL+U
(down) Causes the screen to scroll forward by half a window. The count specifies the number of text lines to be scrolled, and will be remembered during your vi session for future CTRL+D and CTRL+U commands.
In vi input mode:
count not specified:
In input mode, this command causes the cursor to back up by shiftwidth spaces over the indentation provided by the ex option autoindent or CTRL+T (see ex, ex options). CTRL+D only functions in a line in which no characters have been entered as yet.
count can also be: ^ or 0
^
CTRL+D
Cancels automatic indentation for this line.
0
CTRL+D
Terminates the automatic indentation feature.
[count]
CTRL+E
Scrolls forward by the number of lines given in count, leaving the cursor where it is if possible.
[count]
CTRL+F
(forward)
Scrolls forward by 21 screen lines. A count may be used to indicate how many screen pages to scroll forward.
CTRL+G
Displays the following information in the status line:
the current file name
possibly a status entry such as [Modified] if the file has been changed since the last time it was saved with the ex command w
the current line number
the total number of all text lines in the editor buffer
the percentage of the current line to the total length of the file in lines.
This command is equivalent to the ex command f.
[count]
CTRL+H
(same as rubout character)
In vi command mode:
The cursor is moved by count characters to the left but no further than the left margin (see also the vi command h).
In vi input mode:
A count cannot be specified.
The cursor is then moved by one character to the left but stops at the beginning of the text just entered.
[count]
CTRL+J
Moves the cursor count lines down in the same column.
This command is equivalent to CTRL+N and j.
CTRL+I
Clears and redraws the current screen. This command can be used when messages or reports are displayed on the screen (e.g. as a result of a write).
[count]
CTRL+M
(same as enter key)
Moves the cursor to the first character in the next line that is not a blank or tab. A count specifies the number of lines to go forward.
[count]
CTRL+N
This command is equivalent to CTRL+J and j.
[count]
CTRL+P
Moves the cursor count lines up in the same column.
This command is equivalent to the vi command k.
CTRL+R
(refresh)
The current screen is cleared and redrawn. Previously deleted lines marked with @ are removed. See CTRL+L.
CTRL+T
In vi input mode:
(tabulator)
Inserts white space, defined by the value specified in the ex option shiftwidth (see section “ex options” (ex command and display editor)). The white space will, however, not be "visible" until the vi input mode is terminated. The inserted space can only be backed over by using the CTRL+D command.
[count]
CTRL+U
count not specified:
count = 11, or last count used with CTRL+D or CTRL+U
(up)
Scrolls up a half-window of text. A count can be specified to scroll a specific number of lines. The count is remembered during the vi session for future CTRL+D and CTRL+U commands.
CTRL+V
In vi input mode:
"Quotes" or "escapes" a special character so that it can be placed in the text (e.g. escape or control characters). For instance, the sequence CTRL+V ESC places the ESC character into the file.
CTRL+W
In vi input mode:
(word)
Backs up to the start of the previous word, but cannot be used to move beyond the text just entered. The backed over words remain displayed on the screen but are deleted from the buffer and must be re-entered, if required.
[count]
CTRL+Y
Scrolls backward by count lines (towards the beginning of the file), leaving the cursor where it is, if possible.
CTRL+[
(equivalent to ESC)
In vi command mode:
Cancels a partially entered command; sounds an acoustic signal if there is none.
In vi last line mode:
Terminates input of a command just entered and executes it.
In vi input mode:
Terminates vi input mode.
ESC
See CTRL+[.
Commands of the vi command mode
Most vi commands accept a preceding number as an argument. Unless explicitly stated otherwise, the default value is one. This number, or count, is shown within square brackets in the description, which indicates that the entry is optional. The specified count is not displayed, but influences the effect of the following command. If you specify an incorrect count, you can cancel your input by pressing ESC.
The following cases must be differentiated:
[count]command
Here, count specifies how many times the command is to be executed. count must be a positive integer. count cannot be zero since 0 is also a vi command.
count not specified:
vi assumes a value of 1 for count.
[line]z
vi places the line with the specified line number at the top, bottom, or center of the screen (see detailed description of the vi command z).
line not specified:
vi places the current line at the top, center, or bottom of the screen.
[column]|
Specifies the column to which you position the cursor.
The following vi commands can be followed by a movement command movecmd to indicate the range to be affected by the command:
c, d, y, <, >, !, and =.
The <, >, !, and = commands, which only process whole lines, affect all lines that are fully or partially included in the specified range.
The range processed by the c, d, and y commands extends from (and including) the current cursor position to the position specified by the movement command. It is thus significant whether the cursor position is set by line or by word. For example, if the cursor position is set by line, all lines (including the current line) are processed as complete lines. If the cursor position is set by word, the processed range extends till the position before or after the word, depending on whether the cursor was set to the beginning or end of the word.
[count]a
(append) Switches to vi input mode, appending the entered text after the current cursor position. A count can be used to specify how many times the inserted text is to be replicated, but only if the inserted text is limited to one line.
[count]A
(append)
Switches to vi input mode, appending the entered text at the end of the current text line. If the inserted text is all on one line, a count can be used to specify how many times the inserted text is to be replicated, but only if the inserted text is limited to one line. This command is equivalent to $a.
[count]b
(back)
Backs up to the beginning of a previous word, where count specifies how many words to back up.
[count]B
(back)
Backs up to the beginning of a previous "extended" word, where count specifies how many words to move.
[count]cmovecmd
(change)
Overwrites, i.e. changes a range of text. This command must be followed by a movement command.
If the position specified by movecmd is in the current line, this position is marked by a $ character, and the editor enters the vi input mode to replace the range between the cursor and the $ by the entered replacement text.
If the position specified by movecmd is in a new text line, the behavior of c depends on whether the scope of the change is defined by line or not. If the scope is defined by line, all fully or partially affected lines are deleted. However, if movecmd defines the scope by word, only the affected range is deleted. In both cases, vi switches to input mode to replace the deleted text by the new text entered. vi saves the deleted text in the temporary buffer, which means that it can be recovered with P or "lp, for example.
count
A count can be passed through to the movement command.
[count]cc
The command countcc causes the whole of the current line (or count lines) to be changed, regardless of the cursor position in the current line.
[count]C
(change)
Changes the rest of the current line. This command is equivalent to countc$.
[count]dmovecmd
[count]dd
(delete)
Deletes a range of text.
This command must be followed by a movement command. d deletes the range from the current cursor position to the end of the indicated range.
If more than part of a single line is affected, the deleted text is saved in the numbered buffers 1 - 9, which means that it can be recovered with p.
count
A count can be passed through to the movement command which indicates the range.
d
The countdd deletes the whole of the current line (or count lines).
"buffer[count]dmovecmd
"buffer[count]dd
Deletes and stores in named buffers.
The text from the current cursor position to the end of the indicated range is deleted from the file and stored in a named buffer. buffer is a single uppercase or lowercase letter.
D
(delete)
Deletes the rest of the current line. This command is equivalent to d$.
[count]e
(end) Moves the cursor forward to the end of the countth word.
[count]E
(end) Moves the cursor forward to the end of the countth extended word.
[count]fchar
(find)
f must be followed by a single character. vi scans the rest of the current line (from left to right) for that character and moves the cursor to it if found. A count is equivalent to repeating the search that many times.
A semicolon repeats the search in the same direction, while a comma repeats it in the reverse direction.
[count]Fchar
(find)
F must be followed by a single character. vi scans backward in the current line (i.e. from right to left) for that character and moves the cursor to it if found. A count is equivalent to repeating the search that many times.
A semicolon repeats the search in the same direction, while a comma repeats it in the reverse direction.
[line]G
(go to)
vi moves the cursor to the beginning of the line specified by line number line.
line not specified:
The cursor position is set to the last line of the file.
[count]h
(home)
Moves the cursor by count characters to the left, but only in the current line.
[count]H
(home)
Moves the cursor to the top line of the screen. If a count is given, the cursor is moved to that line on the screen, counting from the top. In both cases, the cursor is placed on the first non-whitespace character on the line.
[count]i
(insert)
Switches to vi input mode, inserting the entered text before the current cursor position. If the new text is limited to one line, a count can be specified to indicate how many times the text is to be replicated.
[count]I
(insert)
Switches to vi input mode, inserting the entered text before the first character of the current line. If specified, a count repeats the effect (see above).
[count]j
Moves the cursor down by count text lines in the same column.
This command is equivalent to CTRL+J and CTRL+N.
[count]J
Joins the current line with the next one, supplying appropriate white space: one space between words, two spaces after a period, and no spaces at all if the first character of the next line is a right parenthesis ). A count can be used to specify the number of lines to be joined.
[count]k
Moves the cursor up by count text lines in the same column.
This command is equivalent to CTRL+P.
[count]l
Moves the cursor by count characters to the right, but only in the current line.
This command is equivalent to space.
[count]L
(last)
Moves the cursor to the first non-whitespace character of the last text line on the screen. If specified, a count moves to that line counting from the bottom.
Example
d3L
deletes all lines from (and including) the current line up to (and including) the third text line from the bottom.
mmarker
(mark)
Marks the current cursor position. m must be followed by a marker, which can be any single lowercase letter.
Addressing
`marker
A backquote moves the cursor to the exact position of marker.
'marker
A single quote moves the cursor to the first character in the line containing marker.
M
(middle)
Moves the cursor to the middle line on the screen, at the first non-whitespace position on the line.
n
Repeats the last / or ? scanning command.
N
Repeats the last / or ? scanning command, but in reverse direction.
o
(open)
Switches to vi input mode and opens (i.e. inserts) a line below the current line.
O
(open)
Switches to vi input mode and opens a new line above the current line.
p
Fetch text from temporary buffer
(put)
Writes text from the temporary buffer after the cursor. If the text was saved by vi commands as a whole line (e.g. dd, Y), it is inserted in a line below the current cursor position.
"bufferp
(put)
Fetch text from numbered or named buffer
You can specify the buffer as:
a digit from 1-9 to retrieve text from the buffer with the corresponding number
a letter, to retrieve text from the named buffer with the corresponding name
P
"bufferP
(put)
Similar to p, but putting the text before (or above) the current cursor position.
Q
(quit)
Quits vi mode and enters ex command mode. You must enter the vi <enter> command to return to the vi command mode.
[count]rchar
(replace)
Must be followed by a single character char; the character under the cursor is replaced by the specified one. If a count is specified, the next count characters are replaced with the single character given.
R
(replace)
Replaces several characters; vi enters the replace (or typeover) mode at the current cursor position. The characters on the screen are overwritten by the characters entered, until the input is terminated with ESC.
[count]s
(substitute)
Deletes the single character under the cursor and enters vi input mode; the entered text replaces the deleted character. A count specifies how many characters from the current line are changed. The last character to be changed is marked with a $, as in c.
Example
5sblah blah blah
ESC
replaces the character under the cursor and the next four characters to its right by the string blah blah blah. The same effect can be achieved with:
c5lblah blah blah
ESC
[count]S
(substitute)
Switches to vi command mode. Whole lines are substituted (same as cc). A count can be used to indicate how many lines to replace.
[count]tchar
(to)
t must be followed by a single character. vi scans forward through the remainder of the current line for the specified character. If it finds the character, it places the cursor on the character which precedes it. A count is equivalent to searching for the countth occurrence of the character. Using , or ; to repeat the search is not meaningful.
[count]Tchar
(to)
T must be followed by a single character. vi scans backwards through the current line for the specified character. If it finds the character, it places the cursor on the character which comes after it. A count is equivalent to searching for the countth occurrence of the character. Using , or ; to repeat the search is not meaningful.
u
(undo)
Reverses the last change made to the temporary buffer. If repeated, will alternate between these two states, thus is its own inverse.
Example
d3w
deletes three words from the current cursor position onward
u
puts back the three words
u
deletes the three words again
The contents of the temporary buffer can be accessed by using the p and P commands.
If u is used after an insert command that inserted at least one new text line, the text deleted with u is saved in numbered buffer 1.
U
(undo)
Restores the current line to its state before the cursor was last moved to it. U only functions as long as you have not left the current line.
[count]w
(word)
Moves forward to the beginning of the next word; a count specifies how many words to move.
[count]W
(word)
Moves forward to the beginning of the next extended word; a count specifies how many words to move.
[count]x
Deletes the character at the current cursor position. With a count, deletes count characters forward from the current cursor position, but only on the current line.
[count]X
Deletes the character before the cursor. A count, if specified, deletes that many characters before the cursor on the current line.
[count]ymovecmd
[count]yy
Saves text in the temporary buffer
(yank)
This command must be followed by a movement command to indicate the range. The specified text range is copied (yanked) into the temporary temporary buffer.
count
A count copies the specified range that many times.
y
The yy command causes the whole of the current line to be yanked into the buffer.
Examples
yw
yanks the text from the cursor position to the end of the next word into the temporary buffer.
4y3w
yanks 12 words into the temporary buffer.
4yy
yanks the current text line and the three lines below it into the temporary buffer.
"buffer[count]ymovecmd
"buffer[count]yy
Yank text into named buffers
(yank)
If the y command is preceded by the name of an named buffer ("buffer), the text will also be copied into this buffer. buffer must be specified as an uppercase or lowercase letter.
Examples
"a4yy
yank the current text line and the three lines below it into named buffer a.
"ap
put back the contents of a after the current cursor position.
[count]Y
Yank line(s) into temporary buffer
(yank)
Yanks a copy of the current line or count lines into the temporary buffer (same as yy).
"buffer[count]Y
Yank line(s) into named buffers
(yank)
Yanks a copy of the current line (or count lines) into the named buffer given in buffer, where buffer is an uppercase or lowercase letter. This format corresponds to "bufferyy.
[line]z[count]char
/pattern/z[count]char
Moves the text line specified by line or pattern to the screen position defined by char.
char must follow z and may assume the following values:
<enter> key or plus sign (+)
Specifies the top of the screen
dot (.)
Specifies the center of the screen
minus sign (-)
Specifies the bottom of the screen
line indicates the line number of the text line to be positioned.
line not specified:
The current line is used by default.
pattern identifies the line in which pattern first occurs.
count is an integer between 1 and 23 that may be given after z to specify the lines to be redrawn.
count not specified: count = 23.
ZZ
Exits the vi editor, writing out the buffer contents to the file with the current file name (see ex command, f) if the buffer was changed since the last save (with the ex command w). This command is equivalent to the ex command x.
[count]
space
The cursor is moved by count characters to the right (stops at the end of the line). This command is equivalent to l.
[count]!movecmd
An ! with a movement command allows you to switch to the vi last line mode to process text lines with a POSIX command. The ! must be followed by a movement command to indicate the scope, which must go beyond the current line. The specified line range (from the current position to the one given by the movement command) is then passed as the standard input of the POSIX command and is replaced by the output (standard output and standard error) of the system command. Lines are always passed as complete text lines.
The cursor does not move to the status line until the movement command has been entered. The POSIX command to be executed must be terminated with the enter key.
movecmd
Movement command to a particular line. Here are some examples:
3j
move down 3 lines
lineG
go to the line number given in line
L
move to the last screen line
A second ! can also be specified for movecmd; this passes the current line as standard input to the POSIX command. The second exclamation mark can be preceded by a count to indicate the number of lines to be passed to the POSIX command, starting with the current line.
count
Serves as a repetition factor.
Examples
!!wc
<enter>
Passes the current line to wc as input and replaces it by the output of wc.
!1Gsort
<enter>
Sorts the range from the current line to the start of the file.
3!$tr a b
<enter>
Replaces all occurrences of a by b in the entire current line and the two following lines.
[count]$
Moves the cursor to the end of the current line. A count specifies the number of lines to go forward (e.g. 2$ moves the cursor to the end of the next line).
"
Precedes the name of a named or numbered buffer. See the vi commands d, p, and y.
%
Moves the cursor to the parenthesis, brace, or bracket that matches the one at the current position. Left and right parentheses ( ), square brackets [ ], and braces { } represent matching pairs.
&
Repeats the last ex command s; is equivalent to the ex command &.
'marker
(single quote)
Move cursor to marked line
’marker moves the cursor to first non-whitespace character in the marked line. marker is a lowercase letter that was set as a marker with the vi command m.
If the ’ command is used as a movement command for some other command, all lines in the specified range are treated as whole lines, including the current and the marked line.
Example
d'a
Deletes all lines from the current line up to the marked line, including both extremes.
''
(two single quotes)
Move cursor to previous current line
The '' command moves the cursor to the beginning of the line in which it was located prior to the execution of one of the vi commands below.
H, L, M, n, N, %, ’, (, ), [[, ]], ` , {, }, /pattern<enter>, ?pattern<enter>.
If the ''
command is used as a movement command for some other command, all lines in the specified range are treated as whole lines, including the current and the marked line.
Example
d''
Deletes the range from the current line to the previous current line. You should first use ’ ’ to verify the location of the previous current line. If none exists, vi deletes the range from the current line to the beginning of the file. The deleted text can be retrieved with u.
[count](
Moves the cursor back to the beginning of a sentence; a count moves back that many sentences.
If the ex option lisp is set, moves to the beginning of a lisp s-expression (see section “ex options” (ex command and display editor)). Sentences also begin at paragraph and section boundaries (see { and [[).
[count])
Moves forward to the beginning of the countth sentence.
[count]+
Moves the cursor to the first non-whitespace character in the next line. Use a count to move more than a line (as in CTRL+M).
[count],
Reverses the last vi search command (for a single character): f, F, t, or T,
searching in the opposite direction in the current line. A count can be used as a repetition factor.
[count]-
(hyphen)
Moves the cursor backward to the first non-whitespace character in the preceding line. A count specifies how many lines to move back.
[count].
(dot)
Repeats count times the last vi command which changed the buffer.
If the last command given was used to output the contents of a numbered buffer, a following dot command retrieves the contents of the next numbered buffer in sequence.
/pattern
<enter>
The / immediately takes you to the command line (vi last line mode) where you can specify a regular expression for pattern. The search begins when you terminate pattern with the enter key. vi then scans forward for the next occurrence of a matching string and moves the cursor to it. If no string matching pattern is found, the cursor remains in the current position. The interrupt signal SIGINT can be given to terminate the search.
Since the ex option wrapscan (abbreviated to ws) is set by default, the search wraps around at the end of the file and continues downward from the start of the file to the current cursor position. The ex command set nows causes the search to terminate at the end of the file (see the ex command set and the section on "ex options" under ex).
The vi commands n and N repeat the search in the same and reverse directions, respectively.
If the / is used as a movement command with some other command to specify an extent of text, the defined region is from (and including) the current cursor position to just before the matched string.
Example
If a line contains the following text:
Larry Speakes, the Speaker of the House, spoke well.
And the cursor is positioned at the comma after Speakes, entering the command:
d/ spoke
<enter>
will leave:
Larry Speakes spoke well.
?
Scans backwards; the reverse of / (see /).
0
Moves the cursor to the first character on the current line. The zero is not interpreted as a vi command when preceded by a non-zero digit.
:
The vi command colon (:) switches to vi last line mode, allowing you to enter an ex command. The ex command is terminated by pressing the enter key.
If the ex command you wish to enter is longer than one input line, you will need to switch to the ex command mode with Q.
[count];
Repeats the previous vi search command (for a single character): f, F, t, or T.
A count can be used for a number of repetitions.
[count]<movecmd
Must be followed by a movement command to another line; shifts the inclusive range from the current to the specified line by shiftwidth columns to the left. See section “ex options” (ex command and display editor). A count is passed through to the movement command.
A second < character can also be specified as movecmd; << causes the current line to be shifted to the left (or count lines, including the current line).
[count]=movecmd
Must be followed by a movement command to another line. If the ex option lisp is set, the specified lines are re-indented as if they had been typed in with the autoindent option set. A count can be used to specify how many lines are to be processed. movecmd may also be a second equals sign, which causes only the current line (or count lines) to be indented.
[count]>movecmd
This vi command shifts specified lines by shiftwidth characters to the right (see <).
`marker
(backquote)
Move cursor to set marker
marker is a lowercase letter which identifies a marker that was set with the vi command m. ` marker places the cursor at the exact character position marked.
If the ` command is used as a movement command with some other command to specify its scope, the defined region extends from (and including) the current cursor position up to just before the previous current position.
Example
d`a
Deletes the range from the current cursor position up to the marker a.
``
(double backquotes)
Move marker to previous current position
The `` command moves the cursor to the position it occupied before one of the vi commands listed below was executed.
H, L, M, n, N, %, ’’, (, ), [[, ]], ``, {, }, /pattern<enter>, ?pattern<enter>
If the `` command is used as a movement command with some other command to specify its scope, the defined region extends from (and including) the current cursor position up to just before the previous current position.
[count][[
Backs up to the section boundary given by count.
[count]]]
Moves forward to the section boundary given by count.
^
Moves the cursor to the first non-whitespace character on the current line.
[count]{
Moves back to the beginning of the current paragraph. You can use a count to indicate how many paragraphs to move.
[count]}
Moves forward to the beginning of the next paragraph. A count can be used to indicate how many paragraphs to move.
[column]|
Moves the cursor to the specified column, if possible.
column not specified:
column = 1
~
Case shifting; converts the character at the current cursor position from uppercase to lowercase or vice versa as appropriate, and moves the cursor one character to the right.
Adapting vi to the terminal
The output of the vi program depends on the type of terminal used. The vi editor need not be adapted in the POSIX environment.
vi uses the value of the environment variable TERM to locate the required terminal information in a database (/usr/lib/terminfo/*/*). If TERM has not been defined or if you are working at some other type of terminal, you must reset the value of TERM. You can do this in a number of ways:
set and export the shell variable TERM.
define and export TERM in your .profile.
use the ex command set term (in ex command mode only).
enter the ex command set term in the .exrc file.
define and export the EXINIT environment variable in your .profile.
if all else fails, consult your system administrator.
Caution!
Make sure you only specify the actual name of your terminal for TERM. The use of other names may cause your terminal to malfunction.
Customizing the vi environment
Default values are set using ex options. The available options are described under “ex options” (ex command and display editor).
The ex command set all displays a list of all the current values. For example:
directory=/tmp
Directory for the editor buffer
nonumber
Line numbers are not displayed
report=5
A message is issued when more than 5 lines are changed
scroll=11
The screen is scrolled by 11 lines when CTRL+D is entered
noshowmode
The vi mode is not displayed
term=97801
Terminal type
window=23
Number of screen lines for the text window
wrapmargin=0
No automatic wraparound.
There are two types of option, those with Boolean values and those with non-Boolean values. showmode is an example of a Boolean type option. If it is set, set all displays the string showmode. If it is not set, set all displays noshowmode. With non-Boolean types, set all displays the option name and the value to which it is set. scroll is an example of a non-Boolean option.
If you have trouble with the arrow keys in vi (for example, if pressing an arrow key switches you from input to command mode), you should switch off the timeout option (set notimeout).
The vi environment can be customized to suit your requirements and working habits. For example, vi can be made to display line numbers of the current mode of operation in the status line.
There are three ways of changing the default values for vi:
during a vi session
by using the EXINIT environment variable
by using the .exrc file
Let us assume, for example, that you want to have the line numbers and the vi mode displayed.
During a vi session:
To display the line numbers, use the ex command set number during a vi session.
The ex command set showmode causes vi to indicate when it is in input mode. The customized environment will only be effective for the duration of the session.By using the EXINIT environment variable:
You must write the following lines into the .profile file in your HOME directory:EXINIT='set number showmode'
export EXINIT
Using the command . .profile you make known the new variable to your shell (see . (dot) command). If you now call vi, line numbers will be displayed, and vi will inform you when you are in the input mode. This default setting is always effective.
By using the .exrc file:
You must create this file yourself. The .exrc file can be used to define permanent vi settings. To ensure that vi displays line numbers and the mode, you must enter the following line into this file:
set number showmode
The .exrc file has to be in your HOME directory. For security reasons, vi does not evaluate .exrc files in subdirectories. The .exrc in the HOME directory is always read.
The default values defined for vi are also valid for ex.
It is possible to combine the various methods, but on certain conditions.
Values set within a vi session have the highest priority. You may therefore change the current values at any time during a session by means of the ex command set.
If EXINIT is set, the $HOME/.exrc file is not executed.
File
$HOME/.exrc File containing default values for ex and vi. These default values are overridden by values set using EXINIT or during a vi session. |
Variable
LINES Number of lines per screen. TERM The type of terminal used. EXINIT Environment variable with default values for ex and vi. COLUMNS Number of columns per screen. PATH Determine the search path for the shell command specified in the editor commands shell, read and write and the visual-mode command ! SHELL Determine the preferred command-line interpreter for use in !, shell, read and other commands with an operand of the form !string. For the shell command the program will be invoked with the single argument -i, for all others it will be invoked with the two arguments -c and string. If this variable is null or not set, sh will be used. |
Locale
The following environment variables affect the execution of vi: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_COLLATE Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments). LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
See also
edit, ex |