You are on page 1of 9

BEGINNER'S GUIDE TO THE VI EDITOR

Academic Technology Support


California State University, Los Angeles

JC - UVIBEG (v1.3) Fall 2000

T ABLE OF C ONTENTS
General Information 2
Terminal Type
Mode
Case
Repeating Commands
Creating a New File 3
Calling vi
Typing the File
Leaving vi
Bailing Out
Editing a File 4
Positioning the Cursor
Making Changes in the File
Inserting Text
Deleting Text
Replacing Text
Undo
Moving Text within a File
Moving Text between Files
Command Summary 8

For additional written instructional guides, visit us @ http://www.calstatela.edu/handouts


VI
vi is a full screen editor available on computers running the Unix operating system. This handout is an
introduction to file editing with vi. It describes a minimum set of vi commands that should be
sufficient for most editing purposes. vi has many other functions and commands than those described
in this handout. For a complete reference to vi, see the Unix operating system manuals available in the
instructional computing labs.

General Information

Terminal Type
Before starting vi, you must make sure that your terminal type is set correctly, otherwise, the screen
displayed by vi will be unreadable. Fortunately, the system will normally automatically set the
terminal type for you when you first log on to the system. (When it cannot determine the terminal type
you are using, however, it will prompt you to enter one.) The following information is mainly useful to
people using legacy hardware terminals and old telecomm software; most readers can safely skip to the
section on vi’s Mode instead.

When using Procomm or Kermit communication software, vt100 is the correct terminal type for
most of the equipment on campus, such as a Teleray 100 terminal, a microcomputer, or a Tab
terminal. Otherwise, check the manual for the hardware terminal or the communication software
to determine the terminal type.

If the display is scrambled when you access vi, it is likely because the system's information
regarding your terminal type is incorrect. You’ll need to check and alter the terminal type is this is
the case.

You can display the terminal type for your current session by typing

set

at the Unix prompt. A list of settings will be displayed. One setting will show

$TERM=xxx or term xxx

where xxx is the terminal type. To change your terminal type, type

set term=xxx or (Sun)

TERM=xxx; export TERM (AT&T computers)

where xxx is a terminal type, such as vt100.

Beginner's Guide to the VI Editor 2


Mode
When you are using vi, it is important to know which mode you are in: the edit mode or the input
mode. When you are in the edit mode, the keys you type are editing commands that perform
operations on your file, such as moving the cursor, deleting characters or lines, etc. When you are in
input mode, all the characters you type will be inserted into your file.

When you first access vi, you will be in edit mode. There are several ways to enter input mode
(described below). Once you enter input mode, all the characters you type will be inserted into your
file until you end input mode and return to edit mode by pressing the Esc key.

Case
vi is case sensitive, just as is Unix. This means that vi does not see upper and lower case letters as
the same letter. To vi, an "A" is not the same thing as an "a". They are two different commands that
perform two different operations. When using this handout, be sure to type the commands in the case
in which they are shown.

Repeating Commands
For most of the vi commands discussed below, a number can be included at the beginning of the
command. The command will then execute several times as indicated by the number. For example,
typing dd will delete a line and 6dd will delete 6 lines.

Creating a New File


To create a new file, you first call vi, giving your file a name. Next, type in the file. Finally, you must
exit vi and save your file.

Calling vi
To create the new file, you must first call vi and name your new file. To do this, type

vi filename (Remember, the command vi should be typed in lowercase.)

where filename is the name you wish to give your new file. File names on Unix must be 14 or fewer
characters. The file name may not include blank spaces or a forward slash (/).

After you type the above command, vi will display an empty screen with a tilde (~) at the beginning of
each line and the following line at the bottom of the screen:

"filename" [New file]

where filename is the name you gave the file when you called vi.

You will see on your screen a blinking white square, called a cursor. Notice that the cursor is now at
the top of the file.

Beginner's Guide to the VI Editor 3


Typing the File
To enter text into your file, press the a key. This will take you into input mode. Then, type the
information. It will be entered into your file as it appears on the screen. Anything you type will
appear where the cursor is located. Be sure to press the Return or Enter key at the end of each
line.

When you have completed typing the information, press the Esc key to leave input mode.

Leaving vi
When you have finished entering the new file, type

:wq (Again, be sure this is in lowercase.)

This will save the file, giving it the name you provided when you first called vi, and will exit from vi,
back to the Unix operating system.

Bailing Out
Occasionally you may wish to leave vi without saving the file. For instance, you may have made so
many mistakes in the file that you feel it would be easier to start over again. In such a case, type

:q! (The cursor will move to the bottom of the screen.)

This will exit from vi and will NOT save the file.

Editing a File

You may wish to make some changes in the new file you have just typed. Or, after you have created
the file, saved it, and exited vi, you may find that you wish to call the file back into vi to make
changes. (Call the file back into vi as described in the previous section.) Making a change is a two
step process. For each change, first move the cursor to the place in the file you wish to change and
second, make the change. When all the changes have been made, be sure you exit vi by typing
:wq to save the edited version of the file.

Positioning the Cursor


The first step in making any changes in your file is to move the cursor to the location of the text you
wish to change. The keys for moving the cursor around the screen are:

direction key

down j
up k
right l
left h

Beginner's Guide to the VI Editor 4


These keys should work on with any terminal and any Unix computer. In some cases, the arrow keys
will also work to move the cursor around the screen, depending on the configuration of the Unix host
computer and the type of terminal or communication software used to access it.

The above keys allow you to position the cursor anywhere on the screen. However, in many cases,
your file will be longer than one screen. Therefore, you often need to move forward and backward
through the file. To move forward one screen, press Ctrl-D. (Press the key labeled Ctrl and, while
continuing to hold down the Ctrl key, press the letter key D.) To move backward one screen, press
the Ctrl-U. Using these two commands, you can move through the file until the line you wish to
change is displayed on the screen; then use the above four characters (h, j, k, l) to move the cursor to
the place to be changed.

There are several other commands for moving the cursor around the page and through the file, such as
moving forward one word, moving to next line, etc. Some of these commands are listed in the
Command Summary at the end of this handout.

Another way to move the cursor is to search through the file for a particular string of characters. To
do this, type one of the following commands.

/ to search from the current cursor location forward to the end of the file
? to search from the current cursor location backward to the beginning of the file

The cursor will move to the bottom of the screen. Type the string to be located. If the string is found
on the current screen, the cursor will move directly to the string. If the string is found elsewhere in the
file, the screen will display the section of the file where the text was found, with the line containing the
text in the middle of the screen.

Making Changes in the File


The three most common types of changes are (1) insert needed text; (2) delete unwanted text and (3)
replace text.

1. Inserting Text: Move your cursor to the place you wish to insert the text. Press one of the
following letters to enter input mode

a to insert after the cursor


i to insert in front of the cursor
o to insert a line below the current line
O to insert a line above the current line
A to append to the end of the current line
I to insert at the beginning of the current line

Next, type the characters you wish to insert.

When you have finished typing the characters you wish to insert, press the Esc key to end input
mode and return to edit mode.

Beginner's Guide to the VI Editor 5


2. Deleting Text: To delete a character, move the cursor to the character to be deleted and type
x.

To delete an entire line, move the cursor to the line. Type dd and the complete line will be
removed.

You can also delete part of a line. If your cursor is in the middle of the line and you type D, the
text will be deleted from the cursor forward to the end of the line.

3. Replacing text: To replace a single incorrect character, move your cursor to the character to be
replaced and type

rx

where x is the correct character to replace the current character.

To replace more than one character, use one of the following commands.

s to replace one character with several characters


ns to replace n characters with one or more characters
S to replace a line
cw to replace a word
C replace characters from cursor to end of line

These five commands put you into input mode. Be sure to end input mode by pressing the Esc
key.

Undo
vi has an undo command in case of mistakes. At any point, you can press the u key to undo the last
change you made in your file or you can press the U key to undo all the changes you have made on
the current line.

Moving Text Within a File


vi allows you to move lines of text from one location to another within the file. To do this, the text is
copied to a buffer, the cursor is moved to the new location, and the text is copied from the buffer to
the file.

1. Move the cursor to the beginning of the text to be moved.

2. Copy text to the buffer using one of the following:

a) Delete the text to be moved using the delete commands described previously. The deleted text
will be copied to the buffer automatically.

b) Copy the text to the buffer without deleting it using the following commands.

Beginner's Guide to the VI Editor 6


nyw to copy n words into buffer
nYY to copy n lines into buffer
nY) to copy n sentences into buffer

3. Move the cursor to the place you which to insert.

4. Type p which will copy the text from the buffer into the file following the current line.

Moving Text Between Files


You may also wish to copy text from one file to another. vi allows you to 1) copy part of the text
from the current file into another file or 2) copy the contents of another file into your current file.

1) To another file: To copy text from the current file to another file, designate the line numbers of
the lines you wish to copy. The format of the command is

:nw filename (the cursor will move to the bottom of the screen)

where filename is the name of the file into which you wish to copy the text and n is the line
number of the line you wish to copy. For example, 3w file2 will copy line number 3 of the current
file into another file named file2.

You can also copy a section of text into another file by designating the line numbers of the first and
last lines of the section to be copied. The format of this command is

:n,Nw filename (The cursor will move to the bottom of the screen.)

where filename is the name of the file into which you wish to copy the text, n is the line number of
the beginning line of the block of text you wish to copy, and N is the line number of the ending line
of the block. For example, 3,6w file2 will copy lines number 3, 4, 5, and 6 of the current file
into another file named file2.

There are two useful symbols you may use when designating line numbers. A period . designates
the current line and a dollar sign $ designates the last line. Thus, .,$w file2 would copy all the text
from the line where your cursor is located to the last line into the file named file2.

2) From another file: To copy another file into your current file, first move your cursor to the
location in which the text from the other file should be inserted. Then, type

:r filename (The cursor will move to the bottom of the screen.)

where filename is the name of the file which you wish to copy into your current file.

The contents of the file named filename will be inserted into the current file, following the line
where your cursor is located.

Beginner's Guide to the VI Editor 7


COMMAND SUMMARY

vi filename enter vi
:wq leave vi and save the changes
:q! leave vi without saving the changes
u undo last change
U undo all changes on current line

Commands for Moving Around in the File

h left
j down
k up
l right
Space Bar forward one character
^H backward one character
+ beginning of next line
- beginning of previous line
^U scrolls up
^D scrolls down
B backward one word
W forward one word
e end of current word
w word after this word
H home screen line
M middle screen line
L last screen line
G go to specified line
/ search forward for the string
? search backward for the string

Commands for Making Changes

ESC end the text to be inserted


i insert text before the cursor
a insert text after the cursor
o insert a new line after the current line
O insert a new line before the current line
A append at end of current line
I insert at beginning of current line
x delete the character on which the cursor is set
dd delete a line
rz replace the current character with z
s replace one character with several characters
ns replace n characters with one or more characters

Beginner's Guide to the VI Editor 8


cw replace a word with text you type in
cc replace line with text you type in
C replace text from cursor to end of line
nYY copy n lines into buffer
p copy text from buffer into file
nw filename copy line n into file named filename
r filename copy file named filename into current file

Beginner's Guide to the VI Editor 9

You might also like