VI(Visual) Editor Reference manual

The vi is a text editor. It is small, powerful, and standard on most UNIX systems. The vi often frustrates new users with a unique distinction between its two modes: Command Mode and Insert/Overtype Mode. This distinction, although difficult to become accustomed to for many users, provides great power and ability to the vi editor. Insert/Overtype Mode is designed for inserting text only. All text manipulations and cursor moving should be done from with in Command Mode. To know the vi editor well, is to love it. This page is not meant to replace the vi manual or vi man page. They are fine the way they are. This page is to provide a quick reference sheet for the vi editor and it's most often used functions. Editors such as vim or vile are supersets (at least for the most part) of the vi editor, so this page should still prove useful. I hope even the most ardent vi lover can find something useful in this vi editor reference page.

Entering the vi

prompt$ vi
prompt$ vi file1 file2 ...
prompt$ vi -r file #Recover file from crash
prompt$ vi +string file #Execute ex command "string"
prompt$ vi @rcfile #Read commands from rcfile

Insert/Overtype Mode

Insert/Overtype Mode is solely for entering text. To leave one of these two modes press the [ESC] key. if you wish to enter the ESC character or any other control character while in insert mode: type [CONTROL]-V and then the control sequence. The only difference between Insert Mode and Overtype Mode is that characters are placed in front of the text after the cursor in Insert Mode, where as existing characters are overwritten in Overtype Mode.

Command Mode

Entering Insert Mode

aappend text, after the cursor
iinsert text, before the cursor
Renter Overtype Mode
Aappend text, after end of line
Iinsert text, before first non-whitespace character
oopen new line below cursor in Insert Mode
Oopen new line above cursor in Insert Mode

vi Syntax

vi commands follow the general form:
n operator m object
which means:
execute operator n times on m objects. If n and/or m are omitted, they default to 1.
Operators which take objects are(if the operator is pressed twice then the object is the current line)
"cyYank, if "c is omitted, uses general buffer.
<shift lines left by shiftwidth variable
>shift lines right by shiftwidth variable
!cmdfilter trough cmd
#The operators <, >, and ! are line based so the set of objects is diminished greatly.
Operators which do not take objects:
xDelete character
rReplace character
~change case of character
Objects (if given without an operator are interpreted as a cursor motion command):
wforward until beginning of word
eforward until end of word
bbackward until beginning of word
$forward until end of line
^backward until first non-whitespace character
0backward until first column of line
nGline number n. (default: $, i.e. last line of file)
n|column n of current line
/patforward until beginning of pat, search
?patbackward until beginning of pat, backward search
nrepeat last search
Nrepeat last search/backward search, but in opposite direction
%until match of parenthesis, brace, or bracket
tcuntil next appearance of c on current line
Tcbackward until next appearance of c on current line
fcuntil and including next appearance of c on current line
Fcbackward until and including next appearance of c on current line
;repeat last f, F, t, or T
,repeat last f, F, t, or T in reverse
}forward until end of paragraph
{backward until end of paragraph
)forward until end of sentence
(backward until end of sentence
]]forward until end of section
[[backward until end of section
nHn lines before first line on screen; n defaults to 0
nLn lines before last line on screen; n defaults to 0
Mthe middle line of the screen
jdown one line
kup one line
hleft one character
lright one character
[BS]left one character, backspace usually equals ^H
[SPACE]right one character
_the entire current line
-until first non-whitespace character on previous line
+until first non-whitespace character on next line
[RETURN]until first non-whitespace character on next line


uundo last change
Uundo entire line
"cpput "c or general buffer after the cursor
"cPput "c or general buffer before the cursor
mcset mark with character c
`cgoto mark c
'cgoto beginning of line with mark c
``return to position before mark jump or search
''return to beginning of line before mark jump or search
Jjoin two lines
Ddelete rest of line
Cchange rest of line
Yyank current line into general buffer
&execute last ex-style substitution
.execute last modification
!obj cmdsend object as stdin to command and replace with stdout
[Ctrl]-Gprint information about file
: map x ywhen character x is pressed, execute y
: map! x ymap input mode character x to string y
: ab x yx is an abbreviation for y, changes are made on the fly
: suSuspend the current editor session
: shrun a shell

ex Commands

ex syntax

ex commands in the vi follow this general form:
: addr command
which means:
Execute command on specific lines obtained from the address part of the general form. If address is omitted, current line is used. Keep in mind that the ex is a line based editor, so all actions are line based.
%all lines in file
x,ylines x to y
.current line
nline number: n
$last line of file
x-nn lines before line x
x+nn lines after line x
/pat/forward to line containing pat
?pat?backward to line containing pat
Some commands are:
refer to ex manual page for more commands
s/pat/text/substitute 1st match of pat with text
s/pat/text/gsubstitute every match of pat with text
s/pat/text/nsubstitute the nth occurrence of pat with text
ya cyank into buffer c or the general buffer if c is omitted
g address cmdexecute cmd on all lines which satisfy address
>shift right
<shift left
ddelete line
! UNIX-cmdexecute UNIX-cmd on line
m addressmove lines to address

The vi environment variables

You can customize your environment with this command by typing set var=value, this will set the specified var to value for a scalar variable. For boolean variables, use set var to set and set novar to unset. You can see which variables are set by just typing the set by its self. You can see a list of all variables by typing set all. Some environment variables are specific to the ex editor and some are specific to the vi editor. I have included both.
(I am missing some variables, please contact me with additions.)

boolean variables:

autoindent(ai)begin editing next line at same level of indent-ion as this one.
autowrite(aw)write current buffer before leaving
exrc(ex)tells vi/ex if it should read the .exrc file in the current directory(this can be a security risk).
errorbellseditor sends a beep to the terminal when an incorrect
flashinverse the screen on an error instead of producing a bell
ignorecase(ic)ignore case of characters in searches.
lispenter lisp mode
listplace a $ at the end of each line and a ^I on each tab.
magicallow ., [, and * to be interpreted as special characters in RE's.
modelinesexecute the first and last 5 lines of the file if of the form: ex:command: or vi:command:
number(nu)number lines in left margin
showmatch(sm)when closing a paren., brace or bracket; move the visual cursor to opening item to check scope
showmode(smd)show type of insert mode
wrapscan(ws)when searching and at bottom of file, continue searching from the top

scalar variables:

directorythe location of the temporary directory used by vi
paragraphs(para)macros to signify the beginning of a paragraph
reportvi will notify you if you change more lines than the value of report
sections(sect)macros to signify the beginning of a section
shellThe shell to use when executing the command :sh or :!
shiftwidth(sw)number of spaces to to insert on a shift operation
showmatch(sm)show the match of ) and } when typed
tabstopthe length, in characters, of a tabstop
termholds the name of the terminal type being used
wrapmargin(wm)split lines at the column which is equal to the value of wrapmargin

File Saving and Loading

: wqwrite file and quit
: wwrite file
: w filewrite to specified file
: w!overwrite existing file
: e fileedit new file
: r fileput contents of file
: qquit the editor
: q!force quit the editor, do not save changes
: xquit the editor, save file if it was modified
ZZquit the editor, save file if it was modified
: nstart editing next file in list
: rewrewind file list, start editing 1st file on argument list again
Qquit vi and enter ex
: prePreserve file.
: rec filerecover file


jmove cursor down
kmove cursor up
h or [BS]move cursor left
l or [SPACE]move cursor right
+ or [RETURN]first non-whitespace character on next line
cwchange word
dd or d_delete line
yy or y_yank current line into the general buffer
"ayjyank current line and one below into buffer a
yfcyank until next occurrence of c on current line into the general buffer
3dl or d3ldelete next 3 characters
4c( or 2c2( or c4(change next 4 sentences
>%while on a brace, paren., or bracket; shift right until closing brace, etc.
:%!sort or :1,$!sortsort current file
:5,10s/foo/bar/2change the second occurrence of foo with bar on lines 5-10
:map g 1Gmap g to really run 1G
3JJoin next 2 lines to current one
3,9m$move lines 3 through 9 to the end of the file
ab w/o withoutwhen w/o is typed change to without
:?foo?,/bar/ddelete from the reverse match of foo until the next match of bar
:g/{/,/}/<shift all lines between, and including, a "{" and a "}" left
:$-4,$ddelete last five lines of buffer
:%s/^\(.*\) \(.*\)$/\2 \1/swap everything before and after the first space
d''delete from current position to line of last jump

¹ It is noteworthy to add that most control sequences are bound in the vi. I do not mention them here because they remind me of emacs and I hope to spare you such pain.

² For the record, no animals were physically harmed during the testing of these examples; although some elephants are now in psychological therapy as a direct result of my actions. I kind of feel rather guilty about this :(

I hope you liked my vi reference manual, if you have any questions or suggestions then feel free to send me mail.


Last modified: 21 December 02006
William Totten (

Copyleft: (C) 1996 1998 1999 2000 2006, William Totten