Vi Commands
Vi Commands
What is ?
The deIault editor that comes with the UNIX operating system is called (sual editor).
|Alternate editors Ior UNIX environments include pico and emacs, a product oI GNU.|
The UNIX vi editor is a Iull screen editor and has two modes oI operation:
Command mode commands which cause action to be taken on the Iile, and 1.
Insert mode in which entered text is inserted into the Iile. 2.
In the command mode, every character typed is a command that does something to the text Iile
being edited; a character typed in the command mode may even cause the editor to enter the
insert mode. In the insert mode, every character typed is added to the text in the Iile; pressing the
() key turns oII the Insert mode.
While there are a number oI commands, just a handIul oI these is usually suIIicient Ior
beginning users. To assist such users, this Web page contains a sampling oI basic
commands. The most basic and useIul commands are marked with an asterisk ( or star) in the
tables below. With practice, these commands should become automatic.
Both UNIX and case-sensitive. Be sure not to use a capital letter in place oI a
lowercase letter; the results will not be what you expect.
To Get Into and Out Of
To use vi on a Iile, type in vi filename. II the Iile named filename exists, then the Iirst page
(or screen) oI the Iile will be displayed; iI the Iile does not exist, then an empty Iile and screen are
created into which you may enter text.
Usually the new or modiIied Iile is saved when you leave . However, it is also possible to quit
without saving the Iile.
The cursor moves to bottom oI screen whenever a colon (:) is typed. This type oI
command is completed by hitting the <Return> (or <Enter>) key.
Basic vi Commands http://www.cs.colostate.edu/helpdocs/vi.html
1 oI 5 3/22/2011 9:18 AM
:x<Return> quit , writing out modified file to file named in original invocation
:wq<Return> quit , writing out modified file to file named in original invocation
:q<Return>
quit (or exit)
:q!<Return> quit even though latest changes have not been saved for this call
Moving the Cursor
Unlike many oI the PC and MacIntosh editors, the mouse does not move the cursor within the
vi editor screen (or window). You must use the the key commands listed below. On some UNIX
platIorms, the arrow keys may be used as well; however, since vi was designed with the Qwerty
keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange eIIects
in vi and should be avoided.
II you go back and Iorth between a PC environment and a UNIX environment, you may Iind that
this dissimilarity in methods Ior cursor movement is the most Irustrating diIIerence between the
two.
In the table below, the symbol ^ beIore a letter means that the <Ctrl> key should be held down
while the letter key is pressed.
" down-arrow]
move cursor down one line
` " up-arrow] move cursor up one line
`
<Backspace>
" left-arrow]
move cursor left one character
`
" right-arrow]
move cursor right one character
` '() move cursor to start of current line (the one with the cursor)
:$<Return> G
move cursor to last line in file
Screen Manipulation
The Iollowing commands allow the editor screen (or window) to move up or down several
lines and to be reIreshed.
#
move up (back) one half screen
insert text before cursor, until hit
'
open and put text in a new line below current line, until hit
( open and put text in a new line above current line, until hit
Changing Text
The Iollowing commands allow you to modiIy text.
`
replace single character under cursor (no needed)
)
change words beginning with character under cursor, until hit,
e.g., changes 5 words
Basic vi Commands http://www.cs.colostate.edu/helpdocs/vi.html
3 oI 5 3/22/2011 9:18 AM
)
delete N characters, starting with character under cursor
$)
delete words beginning with character under cursor,
e.g., deletes 5 words
+ delete the remainder of the line, starting with current cursor position
$$
delete entire current line
)$$$)$
delete lines, beginning with the current line,
e.g., deletes 5 lines
Cutting and Pasting Text
The Iollowing commands allow you to copy and paste text.
,,
copv (vank, cut) the current line into the buffer
),,,), copv (vank, cut) the next N lines, including the current line, into the buffer
put (paste) the line(s) in the buffer into the text after the current line
Other Commands
Searching Text
A common occurrence in text editing is to replace one word or phase by another. To locate
instances oI particular sets oI characters (or strings), use the Iollowing commands.
)
move to next occurrence of search string in opposite direction
Determining Line Numbers
Basic vi Commands http://www.cs.colostate.edu/helpdocs/vi.html
4 oI 5 3/22/2011 9:18 AM
Being able to determine the line number oI the current line or the total number oI lines in the Iile
being edited is sometimes useIul.
01
returns line number of current line at bottom of screen
#.
provides the current line number, along with the total number of lines,
in the file at the bottom of the screen
Saving and Reading Files
These commands permit you to input and output Iiles other than the named Iile with which you are
currently working.
:r filename<Return>
read file named and insert after current line
(the line with cursor)
:12,35w smallfile<Return>
write the contents of the lines numbered 12 through 35 to a new file
named