vi Editor Information

To enter vi:

To enter vi from the command line, use any of the forms below:
vi file Invoke vi editor on file
vi file1 file2 Invoke vi editor on files sequentially
view file Invoke vi editor on file in read only mode
vi +n file Invoke vi editor; editing starts on line n
vi -r file Recover changes made to file after a system crash

ex Commands

To enter an ex command from vi:

To enter an ex command from vi, use this form:

:[address] command [options]

address is the line number or range of lines that are the object of command. If no address is given, the current line is the object of the command.

Address Symbols

In ex command syntax, address can be specified by any of the following:
1,$ All lines in the file.
x,y Lines x through y.
x;y Lines x through y, with current line reset to x.
0 Top of file.
. Current line.
n Absolute line number n.
$ Last line.
% All lines; same as 1,$.
x-n n lines before x.
x+n n lines after x.
-[n] One or n lines previous.
+[n] One or n lines ahead.
'x Line marked with x.
' ' Previous mark.
/pat / or ?pat ? Ahead or back to line matching pat.

Option Symbols

In ex command syntax, options may be any of the following:
! Indicates a variant form of the command, overriding the normal behavior.
count The number of times the command is to be repeated. count cannot precede the command, because a number preceding an ex command is treated as a line address. d3 deletes three lines beginning with the current line; 3d deletes line 3.
file The name of a file that is affected by the command. % stands for current file; # stands for previous file.

append [address] a[!]
Append text at specified address, or at present address if none is specified. Add a ! to switch the autoindent setting that will be used during input. That is, if autoindent was enabled, ! disables it.
copy [address] co destination
Copy the lines included in address to the specified destination address. The command t is a synonym for copy.
delete [address] d [buffer]
Delete the lines included in address. If buffer is specified, save or append the text to the named buffer.
edit e[!] [+n] [filename]
Begin editing on filename. If no filename is given, bring in another copy of the curent 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.
insert [address] i[!]
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.
map map char commands
Define a macro named char in visual mode with the specified sequence of commands. char is usually a single character, or the sequence #n, representing a function key on the keyboard.
mark [address] ma char
Mark the specified line with char, a single lowercase letter. Return later to the line with 'x.
move [address] m destination
Move the lines specified by address to the destination address.
next n[!] [[+command]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; if command is given (containing no spaces), execute command after editing the first such file.
put [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.
quit quit[!]
Terminate current editing session. Use ! to discard changes made since the lsat save. If the editing session includes additional files in the argument list that have not yet been accessed, quit by typing q! or by typing q twice.
read [address] r filename
Copy the text of filename at the specified address. If filename is not specified, the current filename is used.
read [address] r !command
Read in the output of command into the text after the line specified by address.
substitute [address] s [/pattern/repl/] [options]
Replace each instance of pattern on the specified lines with repl. If pattern and repl are omitted, repeat last substitution. An option of g substitutes all instances of pattern on the line. An option of c prompts for confirmation before change.
undo u
Reverse the changes made by the last editing command.
write [address] w[!] [[>>]filename]
Write lines specified by address to filename, or full contents of buffer if address is not specified. If filename is also omitted, save the contents of the buffer to the current filename. if >> filename is used, write contents to the end of the specified filename. Add a ! to force the editor to write over any current contents of filename.
yank [address] ya [char] [count]
Place lines specified by address in named buffer indicated by char, or if no char is specified place in general buffer.
z [address] z [type] [count]
Print a window of text with line specified by address at the top. type can be one of:
+ Place specified line at the top of the window (default).
- Place specified line at 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.
count specifies the number of lines to be displayed.
! [address] !command
Execute 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.

vi Commands

vi has two "modes", command and insert. Insert mode (text entry) is terminated by <escape>, which toggles back to command mode. <escape> in command mode is ignored.

In vi, commands have the following general form:

[n] operator [m] object

The basic editing operators are:
c Begin a change.
d Begin a deletion.
y Begin a yank (or copy).

If the current line is the object of the operation, then the operator is the same as the object: cc, dd, yy. Otherwise, the editing operators act on objects specified by cursor-movement commands or pattern-matching commands. n and m are the number of times the operation is performed, or the number of objects the operation is performed on. If both n and m are specified, the effect is n x m.

An object can represent any of the following text blocks:
word Includes characters up to a space or punctuation mark. A capitalized object is a variant form that recognizes only blank spaces.
sentence Is up to ., !, ? followed by two spaces.
paragraph Is up to next blank line or paragraph macro defined by para=option.
section Is up to next section heading defined by sect=option.


2cw Change the next two words.
d} Delete up to next paragraph.
d^ Delete back to beginning of line.
5yy Copy the next five lines.
y]] Copy up to the next section.

Movement Commands

h,j,k,l Left, down, up, right (direction arrows)

w, W, b, B Forward, backward by word
e, E End of word
), ( Beginning of next, previous sentence
},{ Beginning of next, previous paragraph
]],[[ Beginning of next, previous section

0, $ First, last position of current line
^ First character of current line (ignore spaces)
+, - First character of next, previous line
n| Column n of current line
H Top line of screen
M Middle line of screen
L Last line of screen
nH n (number) of lines after top line
nL n (number) of lines before last line

Ctrl-F Scroll forward one screen
Ctrl-B Scroll backward one screen
Ctrl-D, Ctrl-U Scroll down, up one-half screen
Ctrl-E, Ctrl-Y Show one more line at bottom, top of window
z<Return> Reposition line with cursor: to top of screen
z. Reposition line with cursor: to middle of screen
z- Reposition line with cursor: to bottom of screen
Ctrl-L, Ctrl-R Redraw screen (without scrolling)

/pattern Search forward for pattern
?pattern Search backward for pattern
n, N Repeat last search in same, opposite direction
/, ? Repeat previous search forward, backward
fx Search forward for character x in current line
Fx Search backward for character x in current line
tx Search forward for character before x in current line
Tx Search backward for character after x in current line
; Repeat previous current-line search
, Repeat previous current-line search in opposite direction

Line number:
Ctrl-G Display current line number and file name
nG Move to line number n
G Move to last line in file
:n Move to line number n

Marking position:
mx Mark current position as x
`x Move cursor to x (grave character)
`` Return to previous mark or context (two grave characters)
'x Move to beginning of line containing mark x (single quote)
'' Return to beginning of line containing previous mark (two single quotes)

Editing Commands

i, a Insert text before, after cursor
I, A Insert text at beginning, end of line
o, O Open new line for text below, above cursor

r Change character (no <escape> required)
cw Change word
cc Change currnt line
C Change to end of line
R Type of characters
s Delete character and substitute text
S Delete current line and substitute text

Delete, move:
x Delete character
X Delete character before cursor
dw Delete word
dd Delete current line
D Delete to end of line
p, P Put deleted text after, before cursor
"np Put text from delete buffer n after cursor (for last nine deletions)

yw Yank (copy) word
yy Yank current line
"ayy Yank current line into named buffer a
p, P Put yanked text after, before cursor
"aP Put text from buffer a before cursor

Other commands:
. Repeat last edit command
u, U Undo last edit; restore current line
J Join two lines

Exit Commands

ZZ Write (save) and quit file
:x Write (save) and quit file
:wq Write (save) and quit file
:w Write (save) file
:w! Write (save) file (overriding protection)
:30, 60w newfile Write from line 30 through 60 as newfile
:30, 60w >> file Write from line 30 through line 60 and append to file
:w Write current buffer named file as
:q Quit file
:q! Quit file (overriding protection)
Q Quit vi and invoke ex
:e file2 Edit file2 without leaving vi
:n Edit next file
:e! Return to version of current file at time of last write (save)
:e# Edit alternate file
% Current filename
# Alternate filename