[Our Logo]

Vi Help Page

Sections
Vi Movement Commands
Cut+Paste in Vi
Handling Vi Bookmarks
Modes in Vi
Changing Text in Vi
Searching in Vi
Power Tools
Vi is the one editor that you can rely on to be on any UNIX system you go to. Unfortunately what it offers in power it sacrifices in user-friendliness.

First Aid for Vi Users


Movement Commands

In vi the arrow keys can be unreliable. Learning to use the proper movement commands provides more power for some of the other commands such as c, d or y that can take a wide variety of movement commands as their second parameter.
All movement commands can be used with these other commands except where specified.
Most movement commands can be preceeded by a number. Some examples are shown in the table (e.g. 5W), but experiment you'll find more.
Key Movement Key Movement Key Movement
h
one character to the left
H
top of the screen
5h
five characters to the left
l
one character to the right
L
bottom of the screen
5l
five characters to the right
5H
5th line from top of screen
5L
5th line from bottom of screen
G
to the bottom of the file
5G
to line 5 of the file
j
one line down
k
one line up
M
middle of the screen
w
to start of next word
W
to start of next word (skipping punctuation)
5W
to the start of the word that is 5 words to the right
e
to end of current word
E
to end of current word (skipping punctuation)
5E
to the end of the 5th word to the right
b
to start of current word
B
to start of current word (skipping punctuation)
5B
to the start of the 5th word to the left
'a
to bookmark a
'z
to bookmark z
0
(a zero) to the left most column on the line
_
to the start of the text on the line
$
to the end of the text on the line
The following movement commands CANNOT be used as part of other commands.
^U
up a screen (see set)
^D
down a screen (see set) ^U and ^D are opposites
^E
keep cursor on the same line but scroll the screen up one line
^Y
keep cursor on the same line but scroll the screen down one line ^E and ^Y are opposites
^B
down a screen
^F
up a screen ^F and ^B are not quite opposites


Cut + Paste

In vi terms, "yank" is copy, "put" is paste and "delete" is cut.
Key Meaning Key Meaning Key Meaning
Y
yank to the end of the current line
yy
yank the entire line
5yy
yank the current line plus the next 4
yH
yank all lines from the current to the top of the screen
yL
yank all lines from the current to the bottom of the screen
y5W
yank to the start of the 5th word to the right
D
delete to the end of the current line
dd
delete the current line
dG
delete to the end of the file
dH
delete all lines from the current to the top of the screen
dL
delete all lines from the current to the bottom of the screen
d5W
delete to the start of the 5th word to the right
x
delete the character at the current cursor position
X
delete the character to the left of the cursor
5x
delete the next 5 characters
y'a
yank (copy) to bookmark a
y'z
yank (copy) to bookmark z
d'a
delete to bookmark a
d'z
delete to bookmark z
vi stores yanked and deleted data in the same piece of memory (think of it as the clipboard), so a yank followed by a delete loses the text that was yanked.
P
put the text in the yank/delete buffer before the current cursor position
p
put the text in the yank/delete buffer after the current cursor position
yyp
copy the current line to the line below
ddp
swap the positions of the current line and the line below it
xp
swap the positions of the current character and the character to the right of it
Aswell as the clipboard, vi provides 26 "named clipboards" that you can yank and delete text into. Helpful if you know you need to copy/cut and paste multiple bits of text. The named clipboards are a to z. N.B These named clipboards are the only way to cut/copy and paste text between files in a vi session.
"ayy
yank the current line into name clipboard a
"ap
paste the tet from named clipboard a to the line below.
"ay$
yank to the end of the line into named clipboard a
"Ayy
yank the current line onto the end name clipboard a
"AdL
cut to the bottom of the screen onto the end of named clipboard a
Aswell as the clipboard, vi provides 9 "delete clipboards" deleted text is copied into. Helpful if want to retrieve the text from 4 deletes ago. The delete clipboards are 1 to 9.
"4p
past the delete from four deletes ago below the current line


Handling Bookmarks

Vi bookmarks allow you to mark and return to places in the document. There are 26 bookmarks named a to z.
Key Meaning Key Meaning
ma
marks bookmark a
mz
marks bookmark z
'a
to bookmark a
'z
to bookmark z
y'a
yank (copy) to bookmark a
y'z
yank (copy) to bookmark z
d'a
delete to bookmark a
d'z
delete to bookmark z


Vi Modes

Vi uses "modes" to decide what keys actually mean. For example the letter d can be a deletion command or it can actually mean the letter d is put into the text. The ESCAPE key return vi to command mode.
Vi has command, append, insert, open and replace modes. For all intents and purposes this can be and is shortened to command, insert and replace modes.
Key Meaning Key Meaning
i
puts vi in insert mode at the current cursor position. Insert mode means that characters are inserted into the text and the other characters are moved to the right
I
puts vi in insert mode at the start of the text on the current line
a
puts vi in insert mode to the right of the current cursor position
A
puts vi in insert mode at end of the text on the current line
o
creates a blank line beneath the one the cursor is on and puts vi in insert mode at the start of this new line
O
creates a blank line above the one the cursor is on and puts vi in insert mode at the start of this new line
r
puts vi in replace mode for one character, vi then automatically returns to command mode. Replace mode means that characters are put into the text replacing the existing characters
R
puts vi in replace mode until the ESCAPE key is pressed to return vi to command mode


Changing/Substituting Text

Text substitutions in vi are exceptionally powerful, a brief smattering is given here but see also the power users tips later on.
Key Meaning Key Meaning Key Meaning
cc
deletes the current line and puts vi into insert mode
C
deletes to the end of the line and puts vi into insert mode
cw
deletes to the end of the word and puts vi into insert mode
s
deletes the character the cursor is on and puts vi into insert mode
5s
deletes the character the cursor is and the 4 to the right of it and puts vi into insert mode


Searching

Searching in vi is reasonably flexible but in true vi fashion somewhat cumbersome. It needs to be used in conjunction with ":set ic/noic" to use case sensitive/insensitive searches and ":set ws/nows" to control whether or not the search wraps round when it reaches the end/beginning of the document.
Key Meaning Key Meaning
/
Searches forwards through the text. The cursor moves to the bottom of the screen to allow for the entry of the search criteria. Press RETURN to do the search.
?
Searches backwards through the text The cursor moves to the bottom of the screen to allow for the entry of the search criteria. Press RETURN to do the search.
n
Find the next search matching the most recently entered "/" or "?" search
?
Find the previous search matching the most recently entered "/" or "?" search
f
Searches forwards on the current line only, e.g. fl searches on the current line for the next lowercase l
F
Searches bacjwards on the current line only, e.g. Fl searches on the current line for the previous lowercase l
t
The same as "f" above except that it positions the cursor BEFORE the character
T
The same as "F" above except that it positions the cursor BEFORE the character
;
Find the next/previous "f", "F", "t" or "T" match


Vi Power Tools

Vi also contains some rather powerful functionality that is generally untapped by the vi users of this world.
Search and replace commands. A reasonable grounding in regular expressions is required to fully exploit this functionality. A more simple example: ":%s/THE/The/g" This command will go through the entire file finding all occurences of "THE" and replacing them with "The". For more details about "search and replace in vi" click here.

Copyright © 1998-2010 DWG Software