ab [string text]
Define string when typed to be translated into text.
If string and text are not specified, list all current
Note: ^M appears when you type ^V
followed by Return.
:ab ora O'Reilly & Associates, Inc.
:ab id Name:^MRank:^MPhone:
Append text at specified address, or at present address if
none is specified. Add a ! to toggle the autoindent
setting that is used during input. That is, if autoindent
was enabled, ! disables it.
Print the members of the argument list (files named on the command line),
with the current argument printed in brackets ().
Replace the specified lines with text.
Add a ! to switch
the autoindent setting during input of text.
[address] co destination
Copy the lines included in address to the specified
The command t (short for “to”)
is a synonym for copy.
:1,10 co 50
[address] d [buffer]
Delete the lines included in address.
If buffer is specified, save or append the text to the named buffer.
Buffer names are the lowercase letters a–z.
Uppercase names append text to the buffer.
:/Part I/,/Part II/-1d Delete to line above "Part II"
:/main/+d Delete line below "main"
:.,$d Delete from this line to last line
e[!] [+n] [filename]
Begin editing on filename.
If no filename is given, start over with a copy
of the current file.
Add a ! to edit the new file even if the current file
has not been saved since the last change.
With the +n argument,
begin editing on line n.
Or n may be a pattern, of the
Change the name of the current file to filename,
which is considered
“not edited.” If no filename is specified,
print the current status of the file.
Execute commands on all lines which contain pattern or, if
address is specified, on all lines within that range.
If commands are not specified, print all such lines.
Add a ! to execute commands on all
containing pattern. See also v.
Insert text at line before the specified address, or at present
address if none is specified. Add a ! to switch the
autoindent setting during input of text.
[address] j[!] [count]
Place the text in the specified range on one line, with whitespace
adjusted to provide two space characters after a period (.), no
space characters after a ), and one space character otherwise.
Add a ! to prevent whitespace adjustment.
:1,5j! Join first five lines, preserving whitespace
[address] k char
Mark the given address with char,
a single lowercase letter. Return
later to the line with 'x.
k is equivalent to mark.
[address] l [count]
Print the specified lines so that tabs display as ^I, and the ends
of lines display as $. l is like a temporary version of
map[!] [char commands]
Define a keyboard macro named char as the specified
sequence of commands.
char is usually a single character, or the
representing a function key on the keyboard.
Use a ! to create a macro for input mode.
With no arguments, list the currently defined macros.
:map K dwwP Transpose two words
:map q :w^M:n^M Write current file; go to next
:map! + ^[bi(^[ea) Enclose previous word in parentheses
[address] ma char
Mark the specified line with char, a single lowercase letter.
Return later to the line with 'x. Same as k.
[address] m destination
Move the lines specified by address to the destination address.
:.,/Note/m /END/ Move text block after line containing "END"
n[!] [[+n] filelist]
Edit the next file from the command-line argument list. Use args
to list these files. If filelist is provided, replace the
current argument list with filelist and begin editing on the
first file. With the +n
argument, begin editing on line n.
Or n may be a pattern, of the form
:n chap* Start editing all "chapter" files
[address] nu [count]
Print each line specified by address,
preceded by its buffer line
number. Use # as an alternate abbreviation for number.
count specifies the number of lines to show, starting
[address] o [/pattern/]
Enter open mode (vi) at the lines specified by
address, or at the
lines matching pattern.
Exit open mode with Q.
Open mode lets you use the regular vi commands, but only one line at a time.
It can be
useful on slow dialup lines (or on very distant Internet
Save the current editor buffer as though the system were about to crash.
[address] p [count]
Print the lines specified by address.
count specifies the number of lines to print, starting with address.
P is another abbreviation.
:100;+5p Show line 100 and the next five lines
[address] pu [char]
Restore previously deleted or yanked lines from named buffer specified by
char, to the line specified by address.
If char is not specified,
the last deleted or yanked text is restored.
Terminate current editing session. Use ! to discard changes made
since the last save. If the editing session includes additional files
in the argument list that were never accessed, quit by typing
q! or by typing q twice.
[address] r filename
Copy the text of filename after the line specified by
If filename is not specified, the current filename
:0r $HOME/data Read file in at top of current file
[address] r !command
Read the output of Unix command into the text after the line
specified by address.
:$r !cal Place a calendar at end of file
Recover file from the system save area.
Rewind argument list and begin editing the first file in the list.
Add a ! to rewind even if the current file has not been
saved since the last change.
se parameter1 parameter2 ...
Set a value to an option with each
parameter, or, if no parameter is
supplied, print all options that have been changed from
their defaults. For toggle options, each
parameter can be phrased as option or nooption;
other options can be assigned with the syntax option=value.
Specify all to list current settings.
The form set option?
displays the value of option.
See the list of set options in
:set nows wm=10
Create a new shell. Resume editing when the shell terminates.
Read and execute ex commands from file.
[address] s [/pattern/replacement/] [options] [count]
Replace each instance of pattern on the specified lines with
If pattern and replacement are omitted,
repeat last substitution.
count specifies the number of lines on which to substitute, starting
See additional examples in Chapter 6.
(Spelling out the command name does not work in Solaris vi.)
Prompt for confirmation before each change.
Substitute all instances of pattern on each line (global).
Print the last line on which a substitution was made.
:1,10s/yes/no/g Substitute on first 10 lines
:%s/[Hh]ello/Hi/gc Confirm global substitutions
:s/Fortran/\U&/ 3 Uppercase "Fortran" on next three lines
[address] t destination
Copy the lines included in address to the
specified destination address.
t is equivalent to copy.
:%t$ Copy the file and add it to the end
[address] ta tag
Switch the focus of editing to tag.
Run ctags, then switch to the file containing myfunction:
Remove word from the list of abbreviations.
Reverse the changes made by the last editing command.
Remove char from the list of keyboard macros.
Use ! to remove a macro for input mode.
Execute commands on all lines not
If commands are not specified, print all such lines.
v is equivalent to g!.
:v/#include/d Delete all lines except "#include" lines
Print the editor's current version number and date of last change.
[address] vi [type] [count]
Enter visual mode (vi) at the line specified by
address. Exit with Q. type can
be one of -, ^, or . (See the
z command). count specifies an initial window size.
vi [+ n] file
Begin editing file in visual mode (vi),
optionally at line n.
[address] w[!] [[>>] file]
Write lines specified by address to file,
or write full contents of buffer if address is not specified.
If file is also omitted, save the contents of the buffer
to the current filename.
If >> file is used, write contents to the end of the specified
Add a ! to force the editor to write over any current
contents of file.
:1,10w name_list Copy first 10 lines to name_list
:50w >> name_list Now append line 50
[address] w !command
Write lines specified by address to command.
:1,66w !pr -h myfile | lp Print first page of file
Write and quit the file in one movement.
The file is always written.
The ! flag forces
the editor to write over any current contents of file.
Write the file if it was changed since the last write; then quit.
[address] ya [char] [count]
Place lines specified by address in named buffer char.
If no char is given, place lines in general buffer.
count specifies the number of lines to yank, starting with address.
:101,200 ya a
[address] z [type] [count]
Print a window of text with the line specified by address at the top.
count specifies the number of lines to be displayed.
- Place specified line at the top of the window (default).
- Place specified line at the bottom of the window.
- Place specified line in the center of the window.
- Print the previous window.
- Place specified line in the center of the window and leave
the current line at this line.
Execute Unix command in a shell. If address is specified, apply
the lines contained in address as standard input to command,
and replace the lines with the output and error output.
(This is called filtering the text
through the command.)
:!ls List files in the current directory
:11,20!sort -f Sort lines 11–20 of current file
Print the line number of the line indicated by address.
Default is line number of the last line.
|< >||[address] < [count]|
[address] > [count]
Shift lines specified by address
either left (<) or right (>).
Only leading spaces and tabs are added or removed when shifting lines.
count specifies the number of lines to shift,
starting with address.
The shiftwidth option controls the number of columns
that are shifted. Repeating the < or
> increases the shift amount.
For example, :>>> shifts three times as
much as :>.
Print the lines specified in address.
Print the next line in the file.
[address] & [options] [count]
Repeat the previous substitute (s) command.
count specifies the number of lines on which to substitute,
starting with address. options are the same as for the substitute command.
:s/Overdue/Paid/ Substitute once on current line
:g/Status/& Redo substitution on all “Status” lines
[address] ~ [count]
Replace the last used regular expression (even if from a search, and not
from an s command) with the replacement pattern from
the most recent s (substitute) command.
This is rather obscure;
see Chapter 6 of Learning the vi Editor for details.