Using Vi/Vim: A Brief Tutorial

In This Section

For those of you who have finally seen the light and desire to learn the ways, here is a brief tutorial on Vi. Other, more comprehensive, instructional guides can easily be found on the Internet.

To start out using Vim, run the command vim at the command prompt. That will open up a blank document. To open up a document for editing, type the command vim [FILE]. Harvard's Computer Science department recommends starting with Vi rather than Vim because Vi is guaranteed to be on almost every Unix machine; in practice Vim has reached that status as well.

As mentioned above, Vi is structured around two modes: command mode and insert mode. When in insert mode, you simply type text, as in any other word processor. To switch from insert mode to command mode, press the <Esc> key.

In command mode, there are two types of commands. Commands that begin with a colon (:) are called "Ex" commands, because they are based on ex, an old line editor on which Vi is based. Other commands do not start with a colon. Generally the colon commands perform "large" operations on the text (saving the file, performing search-and-replace) whereas the non-colon commands are for moving around in the text or changing it in small ways (deleting words or characters, pasting in copied text).

Lesson one: how to quit Vi so you aren't stuck in it forever. Use the Ex command:

:q

To save your file, use:

:w

To both save and quit:

:wq

Since there really isn't anything to be said about insert mode, the rest of this discussion will deal with command mode. (There's actually quite a few interesting commands which may be used in insert mode, but no one really uses them--they're too Emacs-like...)

Motion commands

The "motion" commands move the cursor around in the text. Here are the most common of them.
Key Effect
h Left one character
j Down one line
k Up one line
l Right one character
w Forward one word
b Back one word
Caret (^) Start of line
Dollar ($) End of line
Ctrl-F Forward one screenful
Ctrl-B Back one screenful
G Last line of text
1G (2 keys)First line of text

Note that the h, j, k, and l keys are all on the middle row for the right hand. The j and k commands are probably the most commonly used motion commands. The ^ and $ commands are like standard Unix regular expressions.

Note that the ^ command actually moves to the first non-whitespace character on the line. The 0 command moves to the first character, whitespace or not.

There are also several search commands, which move the cursor to a position based on searching for a word or pattern. The slash (/) command searches for the next occurrence of a regular expression. So to find the word "hello" in your document, type /hello. Generally egrep-compatible regular expressions are accepted; Vim also implements some extended syntax. the question mark (?) command searches just like slash, but backwards through the document. Once a search has been done, the n command will find the next occurrence of it (with a question-mark search it will continue searching backwards); the N command will search in the opposite direction.

Key Effect
/<pattern> Searches forward for a pattern
?<pattern> Searches backward for a pattern
nSearches for the next occurrence of the pattern
NSearches for the previous occurrence of the pattern

As an exercise, open up a large text file, and practice moving around in it. Try using all of the above commands where appropriate. Find the ones you are most comfortable with, get used to them; you're going to be using them a lot.

There are many, many more motion commands, enough to merit their own page. Generally you will find that most of them aren't all that useful, but it's good to have a fairly large repertoire of motion commands (if only because it's impressive to be able to move somewhere with just one keystroke).

Insertion Commands

The insertion commands put you into insert mode. There are essentially six major insertion commands. The most common of these is i. This will put the cursor into insert mode exactly where it is. For example, say your text currently is:

The quick fox jumps over the lazy dog.

with the cursor on the "f" in "fox." Then, from command mode, you would type the keystroke sequence ibrown<Space> to get:

The quick brown fox jumps over the lazy dog.

The cursor is still over the same character. Remember to press <Esc> when you are done; otherwise you are still in insert mode.

The a command is similar to the i command except that it starts insert mode in the position after the current cursor position. So, using the same starting text as above, if we were to type the keystroke sequence abrown<Space> we would get:

The quick fbrownox jumps over the lazy dog.

The a command stands for "append," since it appends text after the current cursor position.

Two similar commands are I and A. The I command inserts text before the first non-whitespace character of the line; it is equivalent to typing ^i (caret followed by i). A appends after the end of the line; it is identical to $a.

The two remaining commands create a blank line for text. These are the o and O commands (those are the alphabetic letter, not the number zero). The lowercase o command creates a new line below the current cursor position; the uppercase O command creates it above the cursor. Both place the cursor on the new blank line and start insert mode.

To remember the difference, Harley Hahn in his Student Guide to Unix suggests that the uppercase O looks like an inflated balloon, so the new line "rises" above the cursor; the lowercase o looks deflated so the new line sinks. Whatever helps you remember...

Here is a summary of the insert commands.

Key Effect
i Inserts before cursor
a Appends after cursor
I Inserts at start of line
A Appends at end of line
o Inserts into new line below cursor
O Inserts into new line above cursor
Try opening up a scratch document and inserting text for practice.

Changing and Deleting Text

No, we never need to change or delete text. It's perfect the first time it's written, right?

The simplest way to change or delete text: go into insert mode, use the Backspace key. (In Vim the arrow and backspace keys work; this is not always true in original Vi). Why is this a bad idea?

  1. It's slow.
  2. It doesn't look cool.
  3. There's a reason for using command mode.
Let's start with the deletion commands, done in command mode. The simplest ones are x and X, which are exactly like Delete and Backspace on other word processors (x deletes the character the cursor is on, X deletes the character before it). These are used for small, simple changes like typos.

The real deletion command is d. The d is what one might call an "action-motion" command. These commands are followed by a single motion command, and apply the action between the current cursor position and the position the cursor would land on after the motion. In the case of d, everything between the cursor and the end of the motion is deleted. So, for instance, if you typed dw, you would delete from the cursor to the end of the word. If you typed dj, you would delete the current line and the line below it (since the cursor moved down by a line). So starting with the text:

The quick brown fox jumps over the lazy dog.

typing in dw would give:

The quick fox jumps over the lazy dog.

For action-motion commands, doubling the command letter generally applies the action to just the current line. So dd will delete just the current line. By far, my two favorite deletion commands are dd and dw. The c command changes text. It is actually very similar to the d command, except that it places you in insert mode after deleting the area of motion. This is useful if you made a typo or wish to change a word. For instance, starting with:

The quick green fox jumps over the lazy dog.

you could type cwbrown to fix it. Unless, of course, you have a green fox.

There are two other substitution commands I use often. The r command replaces the character the cursor is on with the next character typed; this is useful for one-character typos. The s command deletes the current character and starts insert mode (it means "substitute," as in substituting the character for some other text). Capital R is also sometimes useful; it puts the cursor into "Replace" mode, which is analogous to overwrite mode in other word processors.

Here is the summary table for this section.

Key Effect
x Deletes character under cursor
X Deletes character before cursor
d <motion> Deletes characters to end of the motion
c <motion> Changes characters to the end of the motion
r Replaces one character
R Enters replace mode
s Substitutes one character for text

Repeating Commands

For pretty much every non-colon command it is possible to repeat the command an arbitrary number of times by prefixing a number before it. The number will be interpreted as logically as possible, especially by Vim. Examples:
5j Moves the cursor down five lines
7x Deletes 7 characters from the current cursor position
12dd Deletes 12 lines
d4w Deletes four words
Note from the last example that, since 4w is a single motion command, we can use it as part of an action-motion command such as d. This feature makes action-motion commands especially powerful, because they can thus be applied to pretty much any range of text that you would like.

A few commands interpret their prefix number in a special way. For example, the G motion command, which normally jumps to the end of the file, will jump to the line number specified; this is why 1G jumps to the first line of the file. Also, the s command treats numbers specially, so 5s will substitute the next five characters for whatever is typed in insert mode. Generally these special cases only arise when it is ridiculous to perform the normal action multiple times (jumping to the end of the file seven times is not very useful).

Make sure, at this point, that you have tried out and are familiar with all of the above commands. In time, as you become more and more experienced in Vi and/or Vim, they will come naturally and you won't even have to think about them.

Undo, Redo, Repeat

KeyEffect
uUndoes the last command or insert text
Ctrl-RRedoes the last command undone
. (dot)Repeats the last command or insert text
These are pretty much self-explanatory. Some important points, though:

Next Page