Beginner'S Guide To The Vi Editor: General Information 2
Beginner'S Guide To The Vi Editor: General Information 2
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
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
where xxx is the terminal type. To change your terminal type, type
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.
Calling vi
To create the new file, you must first call vi and name your new file. To do this, type
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:
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.
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
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
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.
direction key
down j
up k
right l
left h
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.
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
When you have finished typing the characters you wish to insert, press the Esc key to end input
mode and return to edit mode.
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
To replace more than one character, use one of the following commands.
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.
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.
4. Type p which will copy the text from the buffer into the file following the current line.
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
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.
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
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