The following tables contain all the basic vi commands.
Starting vi
Command
Description
vi file
start at line 1 of file
vi +n file
start at line n of file
vi +$ file
start at last line of file
vi +/patternfile
start at pattern in file
(do not insert a space between the ``+'' and ``/'' characters.
vi -r file
recover file after a system crash
Saving files and quitting vi
Command
Description
:e file
edit file (save current file with :w first)
:w
save (write out) the file being edited
:w file
save as file
:w! file
save as an existing file
:q
quit vi
:wq
save the file and quit vi
:x
save the file if it has changed and quit vi
:q!
quit vi without saving changes
Moving the cursor
Keys pressed
Effect
h
left one character
l or <Space>
right one character
k
up one line
j or <Enter>
down one line
b
left one word
w
right one word
(
start of sentence
)
end of sentence
{
start of paragraph
}
end of paragraph
1G
top of file
nG
line n
G
end of file
<Ctrl>W
first character of insertion
<Ctrl>U
up ½ screen
<Ctrl>D
down ½ screen
<Ctrl>B
up one screen
<Ctrl>F
down one screen
Inserting text
Keys pressed
Text inserted
a
after the cursor
A
after last character on the line
i
before the cursor
I
before first character on the line
o
open line below current line
O
open line above current line
Changing and replacing text
Keys pressed
Text changed or replaced
cw
word
3cw
three words
cc
current line
5cc
five lines
r
current character only
R
current character and those to its right
s
current character
S
current line
switch between lowercase and uppercase
Deleting text
Keys pressed
Text deleted
x
character under cursor
12x
12 characters
X
character to left of cursor
dw
word
3dw
three words
d0
to beginning of line
d$
to end of line
dd
current line
5dd
five lines
d{
to beginning of paragraph
d}
to end of paragraph
:1,. d
to beginning of file
:.,$ d
to end of file
:1,$ d
whole file
Using markers and buffers
Command
Description
mf
set marker named ``f''
`f
go to marker ``f''
´f
go to start of line containing marker ``f''
"s12yy
copy 12 lines into buffer ``s''
"ty}
copy text from cursor to end of paragraph into buffer ``t''
"ly1G
copy text from cursor to top of file into buffer ``l''
"kd`f
cut text from cursor up to marker ``f'' into buffer ``k''
"kp
paste buffer ``k'' into text
Searching for text
Search
Finds
/and
next occurrence of ``and'', for example,
``and'', ``stand'', ``grand''
?and
previous occurrence of ``and''
/^The
next line that starts with ``The'', for example,
``The'', ``Then'', ``There''
/^The\>
next line that starts with the word ``The''
/end$
next line that ends with ``end''
/[bB]ox
next occurrence of ``box'' or ``Box''
n
repeat the most recent search, in the same direction
N
repeat the most recent search, in the opposite direction
Searching for and replacing text
Command
Description
:s/pear/peach/g
replace all occurrences of ``pear'' with ``peach'' on
current line
:/orange/s//lemon/g
change all occurrences of ``orange'' into ``lemon'' on
next line containing ``orange''
:.,$/\<file/directory/g
replace all words starting with ``file'' by ``directory'' on
every line from current line onward, for example,
``filename'' becomes ``directoryname''
:g/one/s//1/g
replace every occurrence of ``one'' with 1, for example,
``oneself'' becomes ``1self'', ``someone''
becomes ``some1''
Matching patterns of text
Expression
Matches
.
any single character
zero or more of the previous expression
.
zero or more arbitrary characters
\<
beginning of a word
\>
end of a word
\
quote a special character
\
the character ``''
^
beginning of a line
$
end of a line
[set]
one character from a set of characters
[XYZ]
one of the characters ``X'', ``Y'', or ``Z''
[[:upper:]][[:lower:]]*
one uppercase character followed by any
number of lowercase characters
[^set]
one character not from a set of characters
[^XYZ[:digit:]]
any character except ``X'', ``Y'', ``Z'', or a
numeric digit
Options to the :set command
Option
Effect
all
list settings of all options
noautoindent
prevent the preparation of structured program text.
autoprint
display the current line after each ex copy, move, or substitute command.
noautowrite
do not automatically write the contents of the modified buffer to the current file when you give a next, rewind, tag, or ``!'' command, or a <Ctrl>^ (switch files) or <Ctrl>] (goto tag) command.
nobeautify
do not discard all control characters except <Tab>, newline and formfeed from the input.
directory=/var/preserve
set the default directory for the vi editing buffer.
noedcompatible the absence of g and c suffixes on substitute commands is not remembered.
noerrorbells
do not precede error messages with a bell.
hardtabs=8
set the boundaries on which terminal hardware tabs are set or on which tabs the system expands.
noignorecase
do not map all uppercase characters to thier lowercase equivalent.
nolisp
do not autoindent to indent appropriately for LISP code.
nolist
do not display the tabs and end of line characters
magic
with nomagic set, the number of regular expression metacharacters is
greatly reduced, with only caret ``^'' and dollar sign ``$''
having special effects. In addition, the metacharacters tilde ``~''
and ampersand ``&'' in replacement patterns are treated as normal
characters.
mesg
prevents other users writing to your screen with the UNIX write command and scrambling your screen as you edit.
nomodelines
disable the first five and last five lines of a file being read in to be interpreted as vi or ex commands.
nonumber
do not display output lines with their line numbers.
nooptimize
allow the terminal perform automatic carriage returns when displaying more than one line of output.
paragraphs=IPLPPPQPP LIpplpipnpb
set the paragraph delimiters for the { and } operations.
prompt
force ex input to be prompted for with a colon ``:''.
redraw
simulate (using great amounts of output) an intelligent terminal on a dumb terminal.
remap
mapped characters are repeatedly tried until they are unchanged. For example, if o is mapped to O and O is mapped to I, o will map to I if remap is set, and to O if noremap is set.
report=5
set the threshold for feedback from commands.
scroll=11
the number of logical lines scrolled when <Ctrl>-D is received from a terminal input.
sections=NHSHH HUuhsh+c
set the section macros for the [[ and ]] operations.
shell=/usr/bin/ksh)
set the pathname of the shell forked for the shell escape ``!'' command, and by the shell command.
shiftwidth=8
set the width of a software tab stop, used in reverse tabbing with <Ctrl>-D when using autoindent to append text, and by the shift commands.
noshowmatch
when a ``)'' or ``}'' is typed, moves the cursor to the matching ``('' or ``{'' for one second if this matching character is on the screen.
noshowmode
prevents the message ``INPUT MODE'' from being displayed on the lower right corner of the screen when insert mode is activated.
noslowopen
enables the display of updates during inserts.
tabstop=8
expand tabs in the input file to be on n boundaries for the purposes of display.
taglength=0
accept the first n characters in a tag name to be significant, but all others to be ignored.
tags=tags /usr/lib/tags)
set a path of files to be used as tag files for the tag command.
term=value of shell TERM variable)
set the terminal type of the output device.
timeout
set the milliseconds to wait for subsequent input characters.
warn
displays ``[No write since last change]'' before a shell escape command (!) if file has not been saved.
window=23
set the number of lines in a text window.
wrapscan
searches using the regular expressions in addressing wrap around past the end of the file.
wrapmargin=0
defines the margin for automatic insertion of newlines during text input.
nowriteany
perform all checks normally made before write commands.