You are on page 1of 8

How to Use the vi Editor

*Copyright 1991 by Simon Fraser University. Reprinted with permission.


The vi editor is available on almost all Unix systems. vi can be sed !rom any type o!
terminal becase it does not depend on arrow "eys and !nction "eys##it ses the standard
alphabetic "eys !or commands.
vi $prononced %vee#eye%& is short !or %vi%sal editor. 't displays a window into the !ile
being edited that shows () lines o! text. vi is a text editor* not a %what yo see is what
yo get% word processor. vi lets yo add* change* and delete text* bt does not provide
sch !ormatting capabilities as centering lines or indenting paragraphs.
This help note explains the basics o! vi+
opening and closing a !ile
moving arond in a !ile
elementary editing
vi has many other commands and options not described here. The !ollowing resorces
can help yo get started sing the vi editor* and are available at the U, University -oo"
Store+
%vi Ttorial.% Speciali.ed Systems Consltants $SSC&.
%vi Re!erence.% Speciali.ed Systems Consltants $SSC&.
%/earning the vi 0ditor.% /inda /amb* 1991.
Starting vi
2o may se vi to open an already existing !ile by typing
vi filename
where %!ilename% is the name o! the existing !ile. '! the !ile is not in yor crrent
directory* yo mst se the !ll pathname.
3r yo may create a new !ile by typing
vi newname
where %newname% is the name yo wish to give the new !ile.
To open a new !ile called %testvi*% enter
vi testvi
3n#screen* yo will see blan" lines* each with a tilde $4& at the le!t* and a line at the
bottom giving the name and stats o! the new !ile+
~
"testvi" [New file]
vi Modes
vi has two modes+
command mode
insert mode
'n command mode* the letters o! the "eyboard per!orm editing !nctions $li"e moving the
crsor* deleting text* etc.&. To enter command mode* press the escape 50sc6 "ey.
'n insert mode* the letters yo type !orm words and sentences. Unli"e many word
processors* vi starts p in command mode.
Entering Text
'n order to begin entering text in this empty !ile* yo mst change !rom command mode
to insert mode. To do this* type
i
7othing appears to change* bt yo are now in insert mode and can begin typing text. 'n
general* vi8s commands do not display on the screen and do not re9ire the Retrn "ey to
be pressed.
Type a !ew short lines and press 5Retrn6 at the end o! each line. '! yo type a long line*
yo will notice the vi does not word wrap* it merely brea"s the line nceremoniosly at
the edge o! the screen.
'! yo ma"e a mista"e* pressing 5-ac"space6 or 5:elete6 may remove the error*
depending on yor terminal type.
Moving the Cursor
To move the crsor to another position* yo mst be in command mode. '! yo have ;st
!inished typing text* yo are still in insert mode. <o bac" to command mode by pressing
50sc6. '! yo are not sre which mode yo are in* press 50sc6 once or twice ntil yo
hear a beep. ,hen yo hear the beep* yo are in command mode.
The crsor is controlled with !or "eys+ h* ;* "* l.
Key Cursor Movement
--- ---------------
h left one space
j down one line
k up one line
l right one space
,hen yo have gone as !ar as possible in one direction* the crsor stops moving and yo
hear a beep. For example* yo cannot se l to move right and wrap arond to the next
line* yo mst se ; to move down a line. See the section entitled %=oving >rond in a
File% !or ways to move more 9ic"ly throgh a !ile.
Basic Editing
0diting commands re9ire that yo be command mode. =any o! the editing commands
have a di!!erent !nction depending on whether they are typed as pper# or lowercase.
3!ten* editing commands can be preceded by a nmber to indicate a repetition o! the
command.
Deleting Characters
To delete a character !rom a !ile* move the crsor ntil it is on the incorrect letter* then
type

The character nder the crsor disappears. To remove !or characters $the one nder the
crsor and the next three& type
!
To delete the character be!ore the crsor* type
" #uppercase$
Deleting Words
To delete a word* move the crsor to the !irst letter o! the word* and type
dw
This command deletes the word and the space !ollowing it.
To delete three words type
%dw
Deleting Lines
To delete a whole line* type
dd
The crsor does not have to be at the beginning o! the line. Typing dd deletes the entire
line containing the crsor and places the crsor at the start o! the next line. To delete two
lines* type
&dd
To delete !rom the crsor position to the end o! the line* type
' #uppercase$
Relacing Characters
To replace one character with another+
1. =ove the crsor to the character to be replaced.
(. Type r
?. Type the replacement character.
The new character will appear* and yo will still be in command mode.
Relacing Words
To replace one word with another* move to the start o! the incorrect word and type
cw
The last letter o! the word to be replaced will trn into a @. 2o are now in insert mode
and may type the replacement. The new text does not need to be the same length as the
original. Aress 50sc6 to get bac" to command mode. To replace three words* type
%cw
Relacing Lines
To change text !rom the crsor position to the end o! the line+
1. Type C $ppercase&.
(. Type the replacement text.
?. Aress 50sc6.
!nserting Text
To insert text in a line+
1. Aosition the crsor where the new text shold go.
(. Type i
?. 0nter the new text.
The text is inserted -0F3R0 the crsor.
). Aress 50sc6 to get bac" to command mode.
"ending Text
To add text to the end o! a line+
1. Aosition the crsor on the last letter o! the line.
(. Type a
?. 0nter the new text.
This adds text >FT0R the crsor.
). Aress 50sc6 to get bac" to command mode.
#ening a Blan$ Line
To insert a blan" line below the crrent line* type
$lowercase&
To insert a blan" line above the crrent line* type
( #uppercase$
%oining Lines
To ;oin two lines together+
1. At the crsor on the !irst line to be ;oined.
(. Type B
To ;oin three lines together+
1. At the crsor on the !irst line to be ;oined.
(. Type ?B
Undoing
To ndo yor most recent edit* type
u
To ndo all the edits on a single line* type
) #uppercase$
Undoing all edits on a single line only wor"s as long as the crsor stays on that line. 3nce
yo move the crsor o!! a line* yo cannot se U to restore the line.
Moving "round in a &ile
There are shortcts to move more 9ic"ly thogh a !ile. >ll these wor" in command
mode.
Key Movement
--- --------
w forward word *y word
* *ackward word *y word
+ to end of line
, #-ero$ to *eginning of line
. to top line of screen
M to middle line of screen
/ to last line of screen
0 to last line of file
10 to first line of file
2Control3f scroll forward one screen
2Control3* scroll *ackward one screen
2Control3d scroll down one-half screen
2Control3u scroll up one-half screen
Moving '( Searching
To move 9ic"ly by searching !or text* while in command mode+
1. Type C $slash&.
(. 0nter the text to search !or.
?. Aress 5Retrn6.
The crsor moves to the !irst occrrence o! that text.
To repeat the search in a !orward direction* type
n
To repeat the search in a bac"ward direction* type
N
Closing and Saving a &ile
,ith vi* yo edit a copy o! the !ile* rather than the original !ile. Changes are made to the
original only when yo save yor edits.
To save the !ile and 9it vi* type
44
The vi editor editor is bilt on an earler Unix text editor called ex. ex commands can be
sed within vi. ex commands begin with a + $colon& and end with a 5Retrn6. The
command is displayed on the stats line as yo type. Some ex commands are se!l when
saving and closing !iles.
To save the edits yo have made* bt leave vi rnning and yor !ile open+
1. Aress 50sc6.
(. Type +w
?. Aress 5Retrn6.
To 9it vi* and discard any changes yor have made since last saving+
1. Aress 50sc6.
(. Type +9D
?. Aress 5Retrn6.
Co))and Su))ar(
ST>RT'7< vi
vi filename edit a file named "filename"
vi newfile create a new file named "newfile"
07T0R'7< T0ET
i insert tet left of cursor
a append tet right of cursor
=3F'7< TG0 CURS3R
h left one space
j down one line
k up one line
l right one space
->S'C 0:'T'7<
delete character
n delete n characters
" delete character *efore cursor
dw delete word
ndw delete n words
dd delete line
ndd delete n lines
' delete characters from cursor to end of line
r replace character under cursor
cw replace a word
ncw replace n words
C change tet from cursor to end of line
o insert *lank line *elow cursor
#ready for insertion$
( insert *lank line a*ove cursor
#ready for insertion$
5 join succeeding line to current cursor line
n5 join n succeeding lines to current cursor line
u undo last change
) restore current line
=3F'7< >R3U7: '7 > F'/0
w forward word *y word
* *ackward word *y word
+ to end of line
, #-ero$ to *eginning of line
. to top line of screen
M to middle line of screen
/ to last line of screen
0 to last line of file
10 to first line of file
2Control3f scroll forward one screen
2Control3* scroll *ackward one screen
2Control3d scroll down one-half screen
2Control3u scroll up one-half screen
n repeat last search in same direction
N repeat last search in opposite direction
C/3S'7< >7: S>F'7< > F'/0
44 save file and then 6uit
7w save file
768 discard changes and 6uit file

You might also like