|

Introduction | Getting Started | Things to Remember | Unix chmod | Unix vi Reference
Unix
Unix Utilities: vi Quick Reference
This guide is presented as a comprehensive listing of the most commonly used commands
in the vi editor. It is not an exhaustive list showing every
possible combination -- that could literally fill a book.
The general syntax for a vi command is:
[n] operator [m] object
where n is the number of times the operator is performed on m
instances of the object. This is clearer when applied to a
command. This tipsheet was prepared utilizing Unix in a Nutshell
by Daniel Gilly and the staff of O'Reilly & Associates,
published by O'Reilly & Associates, ISBN 1-56592-001-5.
It is also available for perusal from the Senior Consultant's
Office, room 123 of the Korman Center. When working on the
IRT Sun 670 (dunx1.irt.drexel.edu), you may type "vitutor"
at the system prompt for a hands on tutorial in the vi editor.
Don't forget...Unix is case sensitive!
Movement Commands
h Left one character
j Down one character
k Up one character
l Right one character
Note: on some keyboards, the arrow keys may be used
w Forward one word
W Forward one word, ignores punctuation
b Back one word
B Back one word, ignores punctuation
e Go to end of word E Go to end of word, ignores punctuation
) Go to beginning of next sentence
( Go to beginning of current sentence
} Go to beginning of next paragraph
{ Go to beginning of current paragraph
]] Go to beginning of next section
[[ Go to beginning of current section
0 Go to beginning of line (this a zero)
$ Go to end of line
^ Go to the first non-blank character of line
+ Go to first character of next line
- Go to first character of 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 lines after top of screen
nL n lines before last line
CTRL-F Scroll forward one screen
CTRL-B Scroll backward one screen
CTRL-D Scroll down one-half screen
CTRL-U Scroll up one-half screen
CTRL-E Show one more line at bottom
CTRL-Y Show one more line at top
z Reposition line with cursor to top of screen
z. Reposition line with cursor to the middle of the screen
z- Reposition line with cursor to the bottom of the screen
Using Line Numbers
CTRL-G Display current line number
:n Go to line n
nG Go to line n
G Go to last line in file
:set number Display line numbers in the left hand margin
To Enter Insert Mode
a Append after cursor
A Append at end of line
c Begin change operation
C Change to end of line
i Insert before cursor
I Insert at beginning of line
o Insert on new line below current line
O Insert on new line above current line
rx Replace character with x
R Begin overwriting text
s Substitute single character with
S Substitute entire line any number of characters
To Leave Insert Mode
Press the escape key twice.
Changing and Deleting Text
C Change line from current position to end of line
cw Change word
cc Change line
dd Delete entire line
ndd Delete n lines
D Delete remainder of line
dw Delete word
d} Delete up to next paragraph
b Delete back to beginning of line
d/pat Delete up to the first occurrence of pat
dn Delete up to next occurrence of n
dfa Delete up to and including a on current line
dta Delete up to but not including a on current line
dL Delete up to last line on screen
dG Delete to end of file
p Insert last deleted text after cursor
P Insert last deleted text before cursor
rx Replace character with x
R Overwrite text
s Substitute character
S Substitute entire line
u Undo last change
U Restore current line
x Delete current cursor position
X Delete back one character
. Repeat last change
- Reverse case of cursor position
Saving and Exiting
ZZ Quit vi, writing the file only if changes have been made
:x Same as ZZ
:wq Write and quite file
:w Write file
:w file Save copy to file
:n,mw file Write lines n to m to new file
:n,mw >> file Append lines n to m to existing file
:w! Write file, overriding write protection
:w! file Overwrite file with the current buffer
:w %.new Write current buffer named file as file.new
:q Quit file
:q! Quit file while discarding edits since last write
Q Quit vi and invoke ex
:vi Return to vi after the Q command
:e file2 Edit file2 without leaving vi
n Edit next file
:e! Return to version of current file at time of last write
:e# Edit alternate file
% Current file name
# Alternate filename
Searching
/text Search forward for text
/ Repeat forward search
?text Search backward for text
? Repeat backward search
n Repeat previous search
N Repeat previous search in other direction
/text/+n Go to line n after text
?text?-n Go to line n before text
% Match current parenthesis, brace or bracket (useful for debugging code!)
fx Search forward for x in current t line
Fx Search backward for x in current line
tx Search forward for the character before x in current line
TX Search back for character after x in current line
, Reverse direction of search in previous f,F,t,T
; Repeat last character search (f,F,t,T)
Marking Your Position
mx Mark current position with character x
`x Move cursor to mark x
'x Move to beginning of line containing x
`` Return to previous mark or to location prior to a search
'' Return to start of line of previous mark or location prior to a search
Copying and Moving
Y Copy current line to new buffer
yy Copy current line
"xyy Yank current line to buffer x
"xd Delete into buffer x
"Xd Delete and append to buffer x
"xp Put buffer x
y]] Copy up to next section heading
ye Copy to end of word
Accessing Multiple Files
:e file Edit another file, current file becomes alternate
:e! Restore last saved version of current file
:e + file Begin editing at end of file
:e + n file Open file at line n
:n Edit next file
:n! Forces next file :n files Specify a new list of files
Interacting with the Shell and Unix
:! command Run shell command then return to vi
:r Read in contents of file after cursor
:r !command Read in output of command after current line
:nr !command As just above, but put output after line n (0 for top of file)
:!csh Invoke c-shell from vi
:sh Invoke Bourne Shell from vi
CTRL-Z Suspend editor, return to last environment...resumed with the fg command
Miscellaneous Commands
J Join two lines
:j! Join two lines, preserving blank spaces
<< Shift this line left one shift width (default is 8 spaces)
>> Shift this line right one shift width (default is 8 spaces)
Macros
:ab far near Use far as abbreviation for near
:unab far Remove abbreviation for far
:ab List abbreviations
:map c sequence Map character c as a sequence of commands
:unmap c Remove sequence and disable map for character c
:map List characters that are mapped
The following are not used in command mode and can be used safely in user defined
macros:
Letters: g K q V v
Control keys: ^A ^K ^O ^T ^W ^X
Changing vi Settings with ex Commands
The following are issued with the syntax
:set command
where command is one of the following:
ai (autoindent)
In insert mode, indent each line to the same column as the
line above or below.
Default: noai
ap (autoprint)
Display changes after each command.
Default: ap
aw (autowrite)
Automatically save file before opening another file with the
:n command or when issuing a Unix command with :!
Default: noaw
bf (beautify)
Ignore control characters while in insert mode
Default: nobf
eb (errorbells)
Beeps when an error occurs
Default: eb
ic (ignore
case)
Ignores case during a search
Default: noic
lisp
Insert indents in appropriate lisp format
Default: nolisp
magic
Wildcard characters have special meaning in patterns
Default: magic
mesg
Permit system messages to display in vi
Default: mesg
number
Display line numbers on left of screen
Default: nonumber
prompt
Display the ex prompt (:) when Q is issued in given
Default: prompt
report
Display a message whenever an edit affects at least the number
assigned to report. Here, 10dd would report '10 lines deleted'
on the prompt line.
Default: 5
sm (showmatch)
In insert mode, when a ) or a } is typed, the cursor briefly
moves to matching ( or {. If match is not on screen, vi will
beep.
Default: nosm
showmode
In insert mode, displays on the prompt line the type of insert
being made.
Default: showmode
ts (tabstop)
Define the number of spaces that a tab indents
Default: 8
terse
Displays shorter error messages
Default: noterse
warn
Displays the message "No write since last change"
Default: warn
Return to Workshops
|