CSCI 1320 (Principles of Algorithm Design I):
vi Hints and Tips
Caveat: Last updated in 2007. Use with caution!
Versions/variants of vi
vi is available on just about every Unix system you will ever
encounter, but it is a somewhat primitive editor.
Newer and more capable variants exist, and in fact vi
on the lab machines starts one of them -- vim
("vi improved").
It has many features not found in standard vi,
including online help and optional syntax coloring.
Start it with one of the following commands:
- vim, or just vi.
- To access the online help,
type :help and press the return key.
- To read about syntax coloring, and to find out whether
vimcan be installed on your computers, see the
Vim home page.
- gvim ("GUI version of vim"). Also available on the CS lab
machines, but requires graphical access so may be a problem
if you are logged in remotely.
An interactive tutorial can be started by typing vimtutor
from the command line.
Modes in vi
vi, unlike many editors, has two distinct "modes", and what happens
when you press keys depends on which mode you are in.
The modes are:
- Insert mode, in which the characters you enter are inserted
into your file.
- Command mode, in which the characters you enter are
interpreted as commands (see below for some possible
commands).
Several commands put you into insert mode -- i, a,
etc. The version of vi installed on the CS lab machines displays
INSERT at the bottom of the screen if you are in insert mode.
To get out of insert mode, press the ESC key.
If you try to type commands while in insert mode, the commands will
just be entered in your file. Press the ESC key, delete the commands
from your file, and try the commands again.
If you try to enter text while not in insert mode, the text will be
interpreted as commands, often with surprising results. If this
happens, first get out of insert mode if necessary and then use
the "undo" command (u) repeatedly to clean up.
Useful commands
This is a list of the more basic standard vi commands.
In the following:
- <ENTER> after the command
means that you must press the ENTER key after
the command.
- Typewriter font (like this) denotes text you should
type exactly as shown.
- Italics (like this) denotes something
you should supply (e.g., a filename),
- Square brackets (like these []) enclose optional text.
In typing a command, do not include the square brackets used to
denote optional text.
We assume you've started the editor with vi myfile.
- Arrow keys
Move the cursor around. In vim, these work in both
modes. Touch-typing fanatics may be interested to learn
that you can also move the cursor around in command mode
using the
h, j, k, and l keys.
In command mode, 0 takes you to the start of
the current line, and $ takes you to the end.
- i
Puts you in insert mode, before the character under the
cursor. To go back to command mode, press the ESC key.
- a
Puts you in insert mode, after the character under the
cursor. To go back to command mode, press the ESC key.
- :w <ENTER>
Saves the file (as myfile).
You can do this any time -- not just before exiting --
and it's a good idea to do so periodically, just in case.
You can also say
:w otherfile to save it with another name.
- :q <ENTER>
Quit vi.
- :q! <ENTER>
Quit vi, without saving the current file.
This can be
useful if you get very confused and just want to quit without
messing up your file.
- :set number <ENTER>,
:set nonumber <ENTER>
Toggles "numbered mode" on and off. In numbered mode, lines
are displayed preceded by line numbers. This can be useful
in finding a specific line, as you might want to do to
decipher a compiler error message. Or you can type
:g n to go to line n.
- x
Deletes the character the cursor points to.
- dd
Deletes the line the cursor is currently on.
You can delete multiple lines by preceding the command
with a number of lines to delete, e.g., 4dd to
delete 4 lines.
One way to move lines is to delete them first with this
command, move the cursor to the line just before where you
want to insert, and enter p (to retrieve the
deleted lines and add them after the current line).
- dw
"Delete word" --
deletes from the cursor position to the end of the "word"
(indicated by white space or punctuation).
- cc
Deletes the contents of the current line and puts you in
insert mode.
- cw
"Change word" -- deletes from the cursor position to the
end of the "word" (as for "dw") and then puts you in insert
mode to enter replacement text.
- rX
Replaces the character the cursor points to with X
(X can be any character). Does not put you
in insert mode, so this is useful if you only want to change one
character.
- /searchstring <ENTER>
Searches for next occurrence of "searchstring".
If the search string contains a / character,
precede it with the "escape" character \.
To repeat the search, enter the single character n.
To search backward, replace the / character with a
question mark (?searchstring <ENTER>).
- :%s/old/new/g <ENTER>
Replaces all occurrences of "old" with "new".
To do an interactive replacement (in which you will be asked,
for each potential replacement, whether you really want to do it),
add a c to the end of the command, e.g.,
:%s/old/new/gc <ENTER>.
- u
Undoes the previous command. Standard vi
only retains the most
recent command;
vim retains many commands,
so you can "undo" repeatedly to undo a series of commands.
- .
Repeats the previous command. For example, you can delete
several lines without counting them by putting the cursor
on the first line to delete and typing first dd
and then . as often as needed.
- %
If the cursor is on a parenthesis or curly brace, finds the
"matching" parenthesis or brace. If instead it beeps,
there is no matching parenthesis or brace. This can be
quite useful in spotting programming errors, since in
programs parentheses and braces
usually come in "matching" pairs.
- :line1,line2m.
Moves the lines from line line1 through line2
to just after the current line (the one the cursor is on).
line1 and line2 can be line numbers, or see
below.
- mletter
"Marks" the current line (the one the cursor is on) with
letter letter. Note that you won't see anything on
the screen indicating that this has been done, but
you can use this command together
with the "m" command described just above to move lines.
If you have marked the first line to move with letter1
and the last line to move with letter2, the syntax
to move these line to just after the current line is:
:'letter1,'letter2m.
vim has one additional feature that those accustomed to
more recent editors will likely find useful for working with
blocks of text, namely "visual mode".
To select a block of text using this feature, put the cursor
at the start of the block of text and type v. Now
move the cursor to the end of the block. The selected text
should show up highlighted. You can now type d to
delete the text. Typing p inserts the deleted text
just after the current position of the cursor, so you can
move a block of text by highlighting it, deleting it,
moving to the new location, and typing p.