Lab 3: The vi Editor
You will create two files in this lab and edit a third, all using vi. Start your VM and log in using
your own account. Open a terminal window.
1. Type vi. Although the program we use is called vim (vi improved), vi has been aliased to
vim. Upon starting vi/vim, you are placed in a buffer that consists of lines that start with a ~
and some welcome information. The cursor’s location is in the upper left at location 0,0.
The word “All” indicates that the whole file is being displayed. You are in command mode.
To enter text, you must enter either insert or replace mode. Type i (insert mode, you can also
use I to enter insert mode). What appears at the bottom of the window? Enter the following,
pressing <enter> to end each line.
Colorado
Kentucky
Indiana
Minnesota
North Carolina
Ohio
Texas
Type <Esc> key to exit the insert mode. What happens at the bottom of your browser?
Type :w states.txt<enter>. The : brings up a minibuffer to enter a command and
our command is w (write to) states.txt to save the file under the name states.txt.
2. Experiment with moving around the document. First, try using: h, j, k, l (lower case L), G,
1G, $, 0 and nG where n is a line number.
a. Type 1G. Where did your cursor move to? From the top of the file, what keystrokes can
you enter to move to the end of Indiana? To the beginning of Ohio? The end of Texas?
Attempt to solve these with the fewest keystrokes possible.
b. Move to the bottom of the file, enter insert mode. Enter Georgia and return to
command mode. How did you accomplish this entire step?
3. Let’s explore how to edit what you already have in place.
a. Move to the beginning of North Carolina. We will change North to South using replace
mode. Type R which puts us into replace mode and now type Sou to overwrite Nor.
You don’t have to type the ‘th’ because it’s already there. Press <Esc>.
b. Type u. What happened? U also undoes changes but u undoes the last change while U
undoes on change on the current line (both would have done the same thing in this case).
Move the cursor back to the beginning of this line. Type r. This replace mode allows
you to change only one letter. Change the ‘N’ to an ‘S’. You do not need to change the
next letter, ‘o’, it is correct. Explain how to move the cursor without using the arrow
keys. Change the ‘r’ in North to the ‘u’ in South. How did you do that? Save the file by
typing :w <enter>. If you wanted to change the filename, you would type :w
newname <enter> but that is not necessary here.
c. Northern Kentucky has decided to form its own state so we want to add North Kentucky
to the list. We will copy, paste and edit Kentucky. Move the cursor to any point in the
word Kentucky. How did you get the cursor there? Type yy. This copies the entire line
of the cursor into a buffer. Type p to paste the copied line into a new line above the
current line (P places it below the current line – since we are copying and pasting the
same line, it doesn’t matter which we use, p or P). Move to the beginning of the new
line. Enter insert mode and type “North ” (North and a space). Exit insert mode.
4. We will wrap up this file by looking at other vi features.
a. You can enter commands by typing :command, one of which is sort. Type :sort
<enter>. What happened? Save and exit vi (:wq). Restart vi. Type :r
states.txt to load the file back into place. Type :q <enter> to exit vi. Now,
restart vi typing vi states.txt. Either approach allows us to start vi with an existing
file. Move the cursor to somewhere in Minnesota and type o to move into insert mode in
a blank new line that appears in the line underneath the cursor. Enter New
Mexico<Esc>. Move the cursor to somewhere in Indiana, and press O (capital o).
Insert Illinois<Esc>. What is the difference between i, o, O, r and R?
b. Using copy and paste, copy New Mexico and paste it and change the second occurrence
to New York. Explain how you accomplished this in a step-by-step description.
c. Move to New York. Move the cursor after the ‘w’ in New (it can either be on the blank
or on the ‘Y’ in York). Type D. What happens? Replace York with Jersey (the line is
now New Jersey). Since New Mexico and New Jersey are in the wrong positions, let’s
swap them. From anywhere in the line with New Jersey, type dd. This deletes the entire
line. Move to the line with New Mexico and type p to paste the deleted line above this
line. Note that when you use one of the d/D commands to delete, or yy to copy, the item
is placed into a buffer and we can paste from that buffer using P or p. Save your file.
How did you do this?
d. Move to the top of the file. To search for string, type /string <enter>. To search
for further instances, type /<enter>. Type /I to search for a capital I. What do you
see in your buffer? Type /. What happens? You can search backward for string using ?
string. Move to the bottom of the file. How did you do that? Type ?New. Where
did your cursor wind up? Type ?. Where is the cursor now? Type ? again. What
happens?
5. Start a new file from the command line by typing vi courses.dat. Enter insert mode
and enter all of the courses you are taking this semester. Each line should contain the course
designator, number and section (e.g., CIT 371.001), a tab, the teacher’s last name, a tab, the
number of hours, a tab, the location of the course (e.g., GH 160), a tab and the time the
course meets (e.g., TR 12:15-1:30 pm). Do not put blank spaces around the – in the time. If
you do not know the exact information, you can make it up (that is, don’t worry about be
correct) and if you are not taking at least 4 courses, make up some courses to add to your list
so that you have a list of at least 5. Save the file. An entry might look like this:
CIT 371.001 Fox 3 GH 160 TR 9:25-10:40 am
a. Let’s add blank spaces around the – in each line, as in 12:15 – 1:30. To do this, you
want to position the character at each hyphen. You can move the cursor forward to the
Page | 1
character char by typing fchar, or f- in this case (use tchar to place the cursor
immediately before the character). Explain how you will add two blank spaces to the
first line. Continue to do this for each line of the file (you only have to explain it once).
You can repeat the previous t/f operation using ; (semicolon).
b. How can you search your file for an appearance of MWF? How can you move your
cursor to the end of the third line? You want to order the courses by time. This will
require using cut and paste. Cut a line to the buffer using dd and paste using p/P. Explain
in a step by step manner how to perform this sort? Save your file and exit from vi.
6. From Linux, type cp ~zappaf/ZAPPA/overnite.sensation . to copy this file to
Student’s home directory. Now, open this file in vi. How did you do that?
a. Move down to the 30th line in the file. How did you do that? The commands H, M, L,
control+f, control+d, control+b, and control+u move your view in the file.
Try each one. What is the difference between H, M and L? Between control+f and
control+b? Between control+f and control+d?
b. Search forward for the word POODLE and count the number of occurrences found. How
do you search forward after the first instance is found? How many POODLES did you
find?
c. Move to the front of the line of the first occurrence of POODLE. Enter the two characters
xp as a command. What does this do? Move forward to the P in POODLE and repeat
xp. Move forward to the B in the word BITES. Type dw. What does this command do?
Type the letter U. What did this command do?
d. Move down one line so that the cursor is on the (. Type %. What happens? Type %
again. What happens?
e. Search forward for dandruff. Once on that line, type J. What happens?
f. To repeat a command in vi, use . To repeat a command multiple types, type the
command as in #command where # is a number and command is the keystroke. For
instance, to delete 4 lines, you would use 4dd. The lyrics for Fifty-Fifty has 40 lines.
Move the start of this song (search backward for 50). Type 40dd. What happened? The
entire set of lyrics for this song should be in the buffer. Move to the bottom of the file.
How did you do this? Type o to enter insert mode, and add a two blank lines. Type
<Esc> and from the command mode, enter P. What happened? Summarize how you
can cut and paste. Summarize how you can copy and paste (recall copy is yy).
g. Search backward for the word Pair. Once there, use the command w several times.
What does w appear to do? Try W. Also try each of: b, B, e, E. Summarize what each
does. NOTE: try all of these on several lines that include punctuation marks.
h. Move to the top of the file. Recall that #command repeats the given command # times.
The command x deletes one character. Type 100x. What happens? Did it delete 100
characters? Type u to undo this. The command dw deletes an entire word. Try 100dw.
What happens? Again, undo this. What can you conclude about x and dw from this?
i. Save this file and exit vi.
For more practice using vi, work through the example in chapter 2.6.2.
Close your terminal window and shut down your VM.
Page | 2
Page | 3