vi Hints and Tips for Beginners
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 classroom/lab machines starts one of them -- vim
It has many features not found in standard vi,
including online help and optional syntax coloring.
Start it with one of the following commands:
An interactive tutorial can be started by typing vimtutor
from the command line.
- vim, or just vi.
- To access the online help,
type :help and press the return key.
There's lots of good information here,
though navigating it is a little quirky.
- 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.
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:
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.
- 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
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.
This is a list of the more basic standard vi commands.
In the following:
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.
- <ENTER> after the command
means that you must press the ENTER key after
- 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.
- 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
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.
Puts you in insert mode, before the character under the
cursor. To go back to command mode, press the ESC key.
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>
- :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.
Deletes the character the cursor points to.
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) or
P (similar but adds before the current line,
and in my experience more likely to do what you want
with regard to newlines).
"Yanks" (copies) the line the cursor is currently on.
You can copy multiple lines by preceding the command
with a number of lines to delete, e.g., 4yy to
copy 4 lines.
You can then use p or P to insert
the copied lines, as described for deleted lines above.
"Delete word" --
deletes from the cursor position to the end of the "word"
(indicated by white space or punctuation).
Deletes the contents of the current line and puts you in
"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.
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
- /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.,
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.
- There are move and copy commands (and they're very useful
in standard vi),
but in vim you will probably prefer to use
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 or P
as described earlier.
:help visual-mode for more information.
Possibly-confusing vim behaviors
vim has many features that while useful can also
be sources of confusing. Here are some that are frequently
- By default vim is configured to automatically
indent lines in types of source code it knows about.
well for entering text by typing, but if you're inserting using
a window manager's copy-and-paste, it can result in lines being
inserted with more indenting than you want. To stop this annoying
you can type :set paste <ENTER> before inserting
(and :set nopaste <ENTER> after,
to go back to auto-indent).
- If you type just q rather than :,
vim thinks you want to record a macro.
The status line at the bottom of the screen
will show "recording".
Press q to make it stop.
- If you type q: rather than :q,
vim thinks you want it to display
a history of commands and shows them to you in a subwindow.
Type :q to make that go away.
- Sometimes when you start vim to edit an existing
file, you get
a screenful of messages starting "ATTENTION" and
"Found a swap file".
What this means:
vim creates a hidden file that saves information
that can help with recovery if it crashes.
This file is deleted when you exit the program normally
(with q), but if vim crashes
it is kept, in the hope that it will be useful in
recovering from the crash.
And if you exit from vim by
just closing the terminal window
(rather than exiting with q),
vim interprets that as a crash.
What you should do:
At the end of all those messages there should be a prompt
asking you whether you want to open the file anyway or what.
If you respond R vim
will try to recover unsaved changes; otherwise not.
To actually delete this hidden file, so you don't get
that same screenful of messages next time, respond D.