CS 1320 (Principles of Algorithm Design I):
vi Hints and Tips
Tutorials and other help
- If you are using the CS lab computers, after you start
vi, you can type :help
(followed by a carriage return) to bring up an extensive
online help.
- The Useful links and other resources
page contains some links to tutorials and suggestions about
books.
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 vi commands mentioned in class, and some other
useful ones.
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
vimyfile.
- 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.
- 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; the vi installed on the CS lab machines
retains several 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.