You are on page 1of 4

Summary of Most vi Commands

vi has two modes: command mode and text(or insert) mode. Commands are issued when in command mode.
And text is entered in text mode Text mode can always be terminated by ESC, i.e. the Escape key. Arrow keys
only work in text mode when using the more modern clones of vi such as vim. When in doubt stay away
from the arrow keys while in text mode.
Italics in the following indicate text and highlights characters within commands; ENTER represents the Enter
key (or Return key); CTRL-L illustrates Control characters (L in this case).

1. Cursor Movement Commands


Command Meaning
Characters
← Move cursor to the left (can use h or backspace)
→ Move cursor to the right (can use l or space)
↑ Move cursor up (can use j)
↓ Move cursor down (can use k or linefeed)
Words
w Move forward by word
W Move forward by word skipping over punctuation
b Move backward by word
B Move backward by word skipping over punctuation
e Move forward to the end of the word
E Move forward to the end of the word skipping over punctuation
Lines
0 Move to first position of current line
$ Move to last position of current line
n| Move to column n
H Move to top line of screen
M Move to middle line of screen
L Move to last line of screen
ENTER Move down one line (can use +)
− Move back one line
Line number
CTRL-G Display current line number
nG Move to line number n
G Move to last line in file
Searching
/patternENTER Search forward for pattern (can terminate with ESC)
?patternENTER Search backward for pattern (can terminate with ESC)
n Repeat last search
N Repeat last search in opposite direction
/ENTER Search forward for current pattern
?ENTER Search backward for current pattern
fx Search forward for character x in current line
Fx Search backward for character x in current line
tx Search forward to character x in current line
Tx Search backward to character x in current line
; Repeat previous current-line search
, Repeat previous current-line search in opposite direction
% Find matching ( { ) or } when cursor on ) } ( or { respectively

CSE2391/3391 Unix Tools Page 1 © Trevor I. Dix, Monash University, March, 2005
Most vi Commands
Command Meaning
Sections
]] Move forward by section or C function
[[ Move backward by section or C function
) Move forward by sentence
( Move backward by sentence
} Move forward by paragraph
{ Move backward by paragraph
Screens
CTRL-F Scroll forward one screen
CTRL-B Scroll backward one screen
CTRL-D Scroll down ½ screen
CTRL-U Scroll up ½ screen
CTRL-E Scroll one line down
CTRL-Y Scroll one line up
CTRL-L Redraw screen, or CTRL-R
zENTER Redraw with current line at top
z− Redraw with current line at bottom
z. Redraw with current line at centre

2. Editing Commands
Command Meaning
Insert
itextESC Insert text before cursor
ItextESC Insert text after white space at beginning of line
atextESC Append text after cursor
AtextESC Append text at end of line
otextESC Open new line for text below cursor
OtextESC Open new line for text above cursor
Change
cwtextESC Change word
cctextESC Change current line
CtextESC Change to end of line
nstextESC Substitute text for next n characters
StextESC Substitute text to end of line
rx Replace current character by x
RtextESC Replace by typing over characters
Delete
x Delete character
X Delete character before cursor
dw Delete word
dd Delete current line
D Delete to end of line
Yank
yy Yank (ie copy) current line
p Put yanked or deleted text after cursor
P Put yanked or deleted text before cursor
Indentation
>> Indent right one shift-width
<< Indent left one shift-width

CSE2391/3391 Unix Tools Page 2 © Trevor I. Dix, Monash University, March, 2005
Most vi Commands
3. File and Exit Commands
Commands preceded by : are really ex commands and can be terminated by ENTER or ESC.
Command Meaning
ZZ Save current file and quit
:wENTER Save current file
:qENTER Quit
:q!ENTER Quit without saving file
:nENTER Edit next file
:e #ENTER Edit previous (alternate) file
:e newfileENTER Edit file newfile
:e!ENTER Edit current file ignoring changes
:w tempENTER Write (copy) file to temp
:w! %.bakENTER Overwrite to backup file, say newfile.bak
:r otherfileENTER Read otherfile into current file, default after current line
:fENTER Display current file information

4. Miscellaneous
Command Meaning
More editing
u Undo last command
U Undo last commands on a line
J Join current and next lines
Marking and moving (x can be any ASCII char.)
mx Mark current cursor position as x
`x Move cursor to position marked x
´x Move to beginning of line containing mark x
`` Move to cursor position of previous context
´´ Move to beginning of line of previous context
Customizing environment
:set allENTER Show all current settings
:set aiENTER Autoindent
:set noaiENTER Turn off autoindent
:set smENTER Showmatch
:set nosmENTER Turn off showmatch
:set ts=4ENTER Set tab stops every 4 columns, default 8 columns
:set sw=4ENTER Set shift width to 4 columns, default 8 columns
:set wm=10ENTER Set wrap margin to 10, default no line wrapping
:set wm=0ENTER Turn off line wrapping
:set listENTER Show all control characters, including newlines
:set showmodeENTER Show when in insert and append mode

5. Copying/Cutting and Pasting


Command Meaning (x can be any ASCII char.)
mx Mark beginning (or end) position as x
Move to end (or beginning) of text to be copied
"by´x Copy text into buffer b to marked line x
"bd´x Or delete text into buffer b to marked line x
Move to position to paste text
"bp Paste after this line

CSE2391/3391 Unix Tools Page 3 © Trevor I. Dix, Monash University, March, 2005
Most vi Commands
6. Special Control Characters in Text Mode
Control Character Meaning
CTRL-H Erases previous character, and your erase character
CTRL-W Erases previous word
CTRL-V Escapes next character, eg CTRL-VENTER to insert carriage return
CTRL-D Backtabs over autoindent, 0CTRL-D backtabs to margin

7. Examples of vi Commands
Editing operators can be preceded by counts, like in 12iqESC to insert 12 q’s.
Editing operators are followed by operands, like cw to change the next word. Some operators are repeated to
imply lines, like dd to delete a line. Marked positions are valid operands, like d’a to delete to line marked a.
Most operands can be preceded by counts, like d3b to delete back 3 words.
Named buffers can be specified to place text into or take text from. Unnamed buffers "0 through "9 contain
the last through 10th-last delete.
Command Meaning
3dw Delete three words, same as d3w
32| Move to column 32
d55| delete to column 55 (from column 32)
"a2dd Delete two lines and place them in named buffer a
"ap Put contents of named buffer a after cursor
/fredENTER Search forward for string fred
mb Mark cursor position as b
3L Move to 3rd last line on screen
y`b Yank text into unnamed buffer back to position marked b
/ENTER Search forward for string fred again
P Put yanked text before cursor, same as "0P
>?findENTER Shift right all lines back to line with string find
!!commandENTER Replace current line with output of shell command line
!2}fmtENTER Pipe 2 paragraphs to fmt and replace with output
!´aexpandENTER Pipe current to marked lines to expand and replace with output

8. Examples of ex Commands
All the following ex commands can be terminated by ENTER or ESC.
Command Meaning
:s/abc/def/ENTER Substitute first abc for def on current line
:s/abc/def/gENTER Same but substitute all strings abc
:1,$s/abc/def/gENTER Same but on all lines
:´a,.−2s/abc/def/gENTER Same but between line marked a and current line − 2
:g/ghi/dENTER Mark all lines containing ghi, then delete them
:v/ghi/dENTER Mark all lines not containing ghi, then delete them
:!commandENTER Run shell command line

CSE2391/3391 Unix Tools Page 4 © Trevor I. Dix, Monash University, March, 2005

You might also like