Yale University Library

 

Workstation and Technology Services

Quick Links
Hands-On Introduction to the Vi Editor


Vi is the most commonly available screen editor for UNIX. It's the only one you can count on being installed on every Unix system.

Free versions are also available on just about any other operating system, from Atari to Xenix, including Windows and MacOS. There are even graphical versions with tear-off menus and color highlighting! See the links page for more info.

The focus of this page is to get you started which is often the hardest part of learning. More powerful features are left out, but I'll demonstrate some of them at then end.

I've handed out a reference card that I think is a good one. The author's web site is on the links page. I also recommend the book Unix in a Nutshell, which is available at most bookstores. There are books devoted to the just the Vi editor too.

Like most programs, Vi has a large number of commands, but you'll find yourself using only 4 or 5 most of the time.

IMPORTANT THING TO LEARN FIRST:
Editing Modes (or , "why Vi is (supposedly) 'confusing' ")

The most important thing to know about Vi is that it has two modes, Command Mode and Insert Mode.
In Command Mode, you can invoke editing commands, move the cursor, save or exit the file, or enter Insert Mode.
In Insert Mode, you can insert stuff.


Before We Start

We need to create a new file alphabet.html under vi_class directory.

Ensure that you are in your ~/public_html directory.

$ cd ~/public_html

$ mkdir vi_class

$ cd vi_class

$ vi alphabet.htm

Now type :set showmode and hit Enter.


Editing Your File

When you open a file with vi, you are automatically placed in command mode.

Press the i key (insert) to switch to input mode.

Input the following text (the alphabet) exactly as presented. Put a <P> tag at the end of each line.
Don't worry if you make a mistake you can't correct. We'll deal with it later.

[Image]

Return to command mode Esc

Save your file :w


Moving in Command Mode

Moving by characters

Try these commands out

h left a character
j down a character
k up a character
l right a character
or use the arrow keys

Moving by words

b back one word (B to ignore punctuation)
w forward one word (W to ignore punctuation)
What would 2w do?

Moving by lines

0 beginning of line
$ end of line
:number (:14 moves to line 14)
G end of file
1G beginning of file
What would 4G do?

Moving by screens

Ctrl-u scroll up 1/2 screen
Ctrl-d scroll down 1/2 screen
Ctrl-f scroll forward 1 screen
Ctrl-b scroll backward 1 screen
H move to the top of the screen
M move to the middle of the screen
L move to the last line of the screen


Fixing Mistakes and Making Changes

Deleting
x delete the character under the cursor
dw delete to the end of the current word
dd delete the current line

Yanking
yw yank a word into the buffer
yy yank the current line
What would 2yy do?

Pasting
p
paste the buffer after current line (or cursor if you didnt yank an entire line)
P paste the buffer before current line/cursor
Try yanking a line, moving the cursor several lines down and pasting.

cw change to the end of the current word (hit Esc when you're done)
r replace one character (you're automatically put back in Command mode)

Try using dW and cW. What's the difference? Notice the effect when an HTML tag is next to the word.


Operators and Objects

You've probably noticed that d c y take an action on an object or objects. dw deletes a word, etc.

Try out some others:
w word forward
e end of word
$ end of line
G end of file
These are just a few....

It's becoming easier now that you understand the logic?


Undoing Changes

u undo the last change you made anywhere in the file
U undo all recent changes to the current line


Adding text

On the same line:

a insert after the cursor
A add at end of current line
i insert text before cursor
I insert at beginning of current line

On a new line:

o open a new line after the current
O open a new line before the current


Saving and Exiting

:x save changes and exit
:w save changes
:q! quit without saving changes
:e! start over without saving changes


Searching

/pattern search forward
?pattern search backward
/repeat previous search forward
?repeat previous search backward


Adding Some More Text and Searching
  1. Make this the first line of the file: "The quick brown fox jumps over the lazy dog."<P>
    How do you get to line 1?
    Instead of inserting the file before the existing line 1, how could you open up another line above?
  2. Make this the last line of the file, type: "Every good boy deserves fudge."<P>
    How do you get to the last line?
    Using A to append to the existing line and then hitting Enter is one way to open a new line. What's another?
  3. Move your cursor to the top of the file.
  4. Search for the word fudge. Your cursor should be on the f.
  5. Switch to input mode and add the word chocolate BEFORE fudge.
  6. Now search backwards to the line with y. Make it a link to Yahoo.
  7. Do the same with h. Make it a link to Hotbot.
  8. Save your work without exiting the editor

Deleting, Replacing, Simple Use of Buffers, Recovering from Errors
  1. Remove the alphabetic characters b,e,z and the <P> tags, but leave the blank lines in their place.
    Use the search command  rather than the arrow keys to move about the file.
  2. Now delete the blank lines.
  3. Now replace f with z. What command do you use to replace a single character?
  4. Save your work without leaving the editor.
  5. Go to the first line in the file.
    Now delete the first 5 lines of the file with one command.
    Remember that most commands can be applied to multiple objects. You know how to delete 1 line. How do you delete 5?
  6. You could delete the next 5 (repeat the most recent command) with a single . (a dot). Try it.
  7. Then, undo this second delete. What's the command to do this?
  8. Remove the first 10 lines of the file and put them at the end.
    Go to line 1 of the file.
    Delete the first 10 lines with one command. What is it? Now you have 10 lines in the buffer.
    Go to the last line of the file.
    You want to paste the buffer after this line. How?
  9. Now you've decided that you've completely screwed up your file and you want to start over (from the point of the most recent save). How can you bail out and start over? (There are several ways).

Miscellaneous and Complex Stuff

Using set to customize the editing session
Options can be set temporarily in an editing session using the set command.
:set showmode
:set number

Undo them with
:set noshowmode
:set nonumber

Put them in your .exrc file in your home directory to make them the default.  Type set: to see your current settings.  Type :set all to display all options. Look at the man page for vi for explanations.

J joins lines

Ctrl-T tab (note that this command is run in INSERT mode)

>> Shift text right
<< Shift text left

Ctrl-G or :f shows information about the file you are editing

:r  filename Read in a file.  You could use this for templates.  :r footer

:r! command   Read in the output of a command. :r! date

:n Go to the next file (when you have opened multiple files) vi *.html for example.

:p Previous file

Substitution and filters
We're moving far afield here, but I do want to demonstrate these powerful features

:1,$! tr -d '\015'
Filter out that pesky ^M in files you edited on your PC.
Let's break down this command
:1,$  Line addressing :  Apply some action to lines 1 thru the end of the file ($).
!  Use a Unix command as a filter: In this case we use the tr command to remove \015 which is the ascii code for ^M

: 1,$ s/seperate/separate/gc
On all lines (1,$), substitute(s) /existing text/replacement text/ do it globally(not just the first instance on a line)(g) and prompt for confirmation(c)

:14,32 s/$/<P>
Put a paragraph tag at the end of lines 14-32.  Could've used that one earlier :)

Other applications are limited only by your imagination

What if you wanted to create a macro to do the ^M edit?
You can. Here's how:
Open up the .exrc file in your home directory
Add the following line to map the key combination *8:
map *8 :1,$! tr -d '\015'
If you want the Enter key too, you have to do a little gyration:
Type Ctrl-V before you type Enter when editing your .exrc file. Ctrl-V means take the next character literally.
It will look like this:
map *8 :1,$! tr -d '\015'^M
Just typing ^M won't work.

How about abbreviations in insert mode?
:ab hr <HR>
Now when you type hr in your text it automatically expands to <HR>. Try it

You can make abbreviations and macros permanent by adding them to your .exrc file, or you can make temporary ones by preceding them with a colon :ab hr <HR>

You can undo it for the current session with :unab hr

john.coleman@yale.edu Revised 4/20/99