University of Minnesota Duluth People | Departments | Search UMD
ITSS
Search ITSS:

Useful vi Commands

The vi editor can be used to create and modify files on computers at UMD such as the Sun SparcCenter (ub). This document provides you with the basic commands. vi is a powerful, but fairly complicated editor, useful for working with computer program source files. We recommend the easier-to-use pico editor for working with less complicated files

vi differs significantly from the word processors which you may be familiar with. Instead of using menus or special function or control keys, you use the regular keyboard keys to perform tasks such as editing, deleting, saving, etc. For example, if you want to delete a character, you type an x. To insert something into the file, you have to type an "i" first. After doing so, everything that you then type will be inserted into the file until you press the <esc> key. While you are in insert mode you cannot move around in the file or use other editing commands. You must press the <esc> key first. When you are not in insert mode, you can move the cursor using your arrow keys, or by typing the h, j, k, and l keys.

Creating a New File
To create a new file, enter the following at any % prompt (replace filename with the name of the file you want to edit or create): % vi filename

After vi starts up, type an i to enter insert mode. The i will not appear on the screen. Then type in the text of your file. In insert mode you can use -t to indent (hold down the key and type a t), and -d to unindent. You cannot use the arrow keys to move around or make corrections while you are entering your program, however, you can use the key to back up and retype words on the current line. If you press the key to exit from insert mode you can then move around using the arrow keys or use any of the commands listed below. To get out of the vi editor and save the file, type a colon (:) to bring you down to the command line at the bottom of the screen, type an x (for exit), and then press .

Basic vi Commands
i
insert text before the cursor. Press to get out of insert mode.
a insert text after the cursor. Press to get out of insert mode.
x delete the character under the cursor. 5x deletes 5 characters.
(Repeat counts can be used in front of almost all commands.)
dd delete a line (3dd deletes 3 lines)
r replace the character under the cursor with next character you type
R overwrite text (<esc> to quit overwriting)
u undo the last change
O open and input new lines, above current line (<esc> to get out)
o open and input new lines, below current line (<esc> to get out)
:x save the file and exit from vi
:q! quit without saving the file (changes will be lost)

Scrolling Commands

The following commands will allow you to scroll forward or backward through your file, but they cannot be used with d or y to delete or copy text. Some of these commands are key combinations (hold down the <ctrl> key when you type the command letter).

f scroll forward a screen
b scroll backward a screen
d scroll forward a half screen
u scroll backward a half screen
z followed by a , a period (.), or a dash (&endash;) repositions the screen
with the current line at the top, middle, or bottom of screen, respectively.
Moving Commands
w
move to next word
b move back a word
$ move to end of line
0 move to beginning of line
_ (underscore) move to first character on the line (ignoring leading tabs and spaces)
G move to the last line of the file. 1G goes to line 1, 2G to line 2, etc
<ctrl>G display the current line number
fx move to move to the first x on the line (or whatever character)
; move to next occurrence on the line of the character named in the last f command
/string move to the next occurrence of string in the file
? string move backwards to the previous occurrence of the string in the file
n repeat last search in same direction
N repeat last search in opposite direction
` ` (two back quotes)move back to where you were before the last moving command

Moving commands are useful for deleting or copying blocks of text. If you type a d followed by one of the above moving commands you will delete text up to the place you move to. For example, dG will delete text up to the end of the file. To put the text you last deleted back into the file, type a p, or a P (see Other Useful Commands below). To copy a block of text without deleting it, type a y followed by a moving command, for example, yG.

Other Useful Commands
yy
yank line (copy the line ), 4yy would yank (copy) 4 lines
p paste the last deleted or yanked text into the file at the cursor position
P paste the last deleted or yanked text into the file (ahead of cursor)
dd delete line. A d followed by any moving command in the list above
will delete up to that point. For example, dw would delete the next word.
cw change word (type the replacement word or words and then press <esc>)
J join two lines together (place cursor on the first of the two lines)
U undo the changes you made to the current line
. (period) redo the last command
>> shift line to the right. ( << will shift to the left)

Global Replacement
To change all occurrences of a string of text to another string you can move down to the command line by typing a colon (:) and then use the substitute command (s). For example, to change all occurrences of the the string "old text" to the string "new text" on lines 1 through 17 you would type:

:1,17s/old text/new text/g

The /g at then end of the command tells vi to replace every occurence it finds on a line, rather than just the first one.You can also use a $ to indicate the last line of the file (:1,$s/old text/new text/g).

Exit and Other Command Line ("colon") Commands
:x
or :wq write (save) the file and quit (see note below)
ZZ write (save) only if changes have been made and quit (no colon necessary)
:w write (save) the file, but do not quit
:3,20w filename write lines 3 through 20 onto the file filename
:r filename read the contents of the file filename into the current file
:q! quit without saving

Note: be careful not to use the uppercase X colon command (:X). This will
prompt you for a key for encrypting your file. If you you enter a key then your
file will be encrypted when you save it and you will need to remember what the
key was in order to work with your file again.

 

Did you find what you were looking for? YES NO
©2003 Regents of the University of Minnesota. All rights reserved.
The University of Minnesota is an equal opportunity educator and employer.
Last modified on 04/02/00
Contact ITSS | Privacy