vi is the editor of choice among most programmers. On Linux, it has been replaced by a compatible editor named vim which is basically vi with a lot of really cool features.

You can read all you want, but the ONLY way you'll learn this stuff is to play around with what you learn. It's important that you make an honest effort to use what you learn in this tutorial, otherwise you simply won't remember this stuff. There are so many features in vi that nobody remembers all of them. Most people have some set of commands that they find useful and memorize.

At some point, these commands will be second nature to you. vi is based on 1 and 2 keystroke commands. The commands will be cryptic at first, but the tradeoff is that editing a file with vi is very fast.

Conventions

Throughout the tutorial, we'll use the following conventions:

CCP Means "the current cursor position".

^ Means the control key. so ^v means control-v

[n] Sometimes you'll see a command like [n]yy. This means that you can place an optional number before yy. So [n]yy could mean simply "yy" or "1yy" or even "1000yy".

vimtutor

If you get lost or don't understand this tutorial, most Linux systems come with a program called 'vimtutor'. If you have trouble with this tutorial, perhaps you'll find the vimtutor easier to learn from. To run it, just type:

vimtutor If you see a message like "command not found" then vimtutor isn't on the system.

Let's Begin

To create a file named newfile.c, simply type "vi newfile.c". To edit an already existing file named oldfile.c, simply type "vi filename.c".

Vi has 3 modes:

1. text entry mode: you enter text in this mode.

2. command mode: you issue commands in this mode

3. ex mode: you issue commands beginning with a colon ':' in this mode.

ex and command mode serve the same purpose. They're called different things because of historical reasons. All ex modes begin with a colon. Half the battle in learning vim is learning how to switch between the various modes, so we'll cover that right now.

From command mode, you can enter text mode by pressing any of the following keys: a, A, i, I, o, O, R and a few others. You'll learn what most of them mean later.

From text mode you can enter command mode by pressing the escape key. If your terminal doesn't have an escape key, try pressing ^[.

Go ahead and type

vi myfile

You should be staring at an essentially blank screen. You may see something like "myfile [New File]" at the bottom of the screen, along with a pair of comma separated numbers. The numbers indicate the cursor's position in your document (they should be 0,0 since your document is empty). The first number is the line and the second number is the column. You should also see a bunch of ~ (tilde) at the left hand side of the screen. The 1st tilde marks the bottom of your document.

Right now, you're in command mode. To enter text, type the letter i, which stands for "insert mode". Go ahead and type 'i'. You're now in text mode, and can enter text. Type the lines:

The Time has come, the walrus said,
To speak of many things.
Of shoes and ships and sealing wax
And cabbages and kings.
And whether pigs have wings.
After typing "wings.", press escape. You're now in command mode.

Cursor Movement in Command Mode

Play around with moving the cursor using the following commands. I've placed a star before the commands that I've found to be most useful.

* [n]w move cursor forward by one word
* [n]b move cursor back by one word
* 0 move cursor to beginning of the line
* $ move cursor to the end of the line
} move cursor down by one paragraph
{ move cursor up by one paragraph
[[ move cursor to the beginning of the document
]] move cursor to the end of the document
* G move cursor to the end of the document
* [n]G move cursor to line n of the document
Page Up move cursor one page up
Page Down move cursor one page down
* h or left arrow move cursor to the left by one character
* k or up arrow move cursor up by one character
* j or down arrow move cursor down by one character
* l or right arrow move cursor to the right by one character

Many of these commands can be preceded by a number which multiplies the effect. So, for example, 3w will move the cursor forward by 3 words and 11h will move the cursor to the left by 11 characters.

A word is defined by characters separated by a space or a tab.

Before moving on, go ahead and play with moving the cursor around until you get a feel for what each command does, especially the starred ones.

Editing Text In Command Mode

Using what you learned in the previous section, place the cursor on the s in "shoes". Now type

dw

The word "shoes" should vanish. Now press the u key. u stands for undo, and you should see "shoes" reappear. Now type

:redo

"shoes" should disappear again. This undoes the undo. Type u once more to get "shoes" back. Here are some text editing commands you can use in command mode.

* dw Delete one word, starting at the cursor position
* dd Delete the current line
* x Delete the character underneath the cursor
R Start replacing all characters (use escape key to end replacement)
* u undo the last change you've made to the document
:redo Undo the last undo

Most of these commands can be preceded by a number which multiplies the effect. So for example, 3x deletes the next 3 characters and 5dd deletes the next 5 lines.

Go ahead and play with these commands. Try to get comfortable with them, especially the starred ones, which are the ones I find most useful.

Saving and Quiting

You can only save and quit from command mode.

* :wq Save and quit
* ZZ Save and quit
:w filename Save as file filename
:q Quit without saving
* :q! Quit now with no questions.

If you try quiting without saving your work, vi will give a warning. If this happens, use q!. Type :w to save your file.

Entering Text Mode From Command Mode

These commands all take you from command mode to text entry mode. The difference between them is where the text will start to appear. When you begin to play with these commands, remember, to play with the next command, you'll have to get back into command mode and you do that by pressing the escape key. * i Insert text directly in front of the CCP
I Insert text at the beginning of the current line

* a Append text directly after the CCP

A Append text at the end of the current line

* o Insert text below the current line

* O Insert text above the current line

Searching For Text and Regular Expressions

There are two commands to search for a string: / and ?. They differ only in the direction of search. In command mode, when you type

/mystring

vim starts at the CCP and searches for "mystring" downwards, towards the end of the document. If you type

?mystring

then vim starts at the CCP and searches for "mystring" upwards, towards the beginning of the document. In either case, it will stop on the first occurance of "mystring" and place the cursor over the beginning of "mystring". If it doesn't find "mystring", then the CCP will be left alone.

If you want to find the next occurance of "mystring" then hit n. n will continue the search in the same direction. N will also continue the search, but in the opposite direction.

In your example document, try figuring out what hitting n followed by N does.

Some characters have special meanings to vi, so they must be preceded by a backslash (\) if you want to include them as part of the search. Otherwise, they can be used for special types of searches, called regular expressions, also known as "regex".

^ Beginning of the line (at the beginning of a search expression)
^ Do NOT match the next character (not at the beginning of a search)
$ End of the line (at the end of search expression)
. Matches any 1 character
* Matches zero of more of the previous character cells
[] Matches a list or range

The best way to learn this stuff is by experimentation and looking at examples. See if you can guess what they mean before reading the explanation.

regex Search for the...

/hello string "hello"
/^hello string hello at the beginning of a line
/\^hello string ^hello
/hello$ word hello at the end of a line
/hello\$ the string hello$
/hello. string "hello" followed by any 1 character
/hello\. the string "hello."
/hello[1a+] 3 strings hello1, helloa and hello+
/hello[^1a] strings beginning with hello and followed by any 1 letter NOT ending with a 1 or a.
/hello[a-c] 3 strings helloa, hellob or helloc
/hello[a-z] string hello followed by any single lowercase character
/hello[A-Z] string hello followed by any single uppercase character
/hello[A-Z] string hello followed by any number of uppercase characters
/hello[^a-c] the strings starting with hello and followed by any 1 letter which is NOT a, b or c.
/hello[1-5] the string hello followed by a single digit
/hello[1-5]* the string hello followed by 0 or more single digits
/^$ blank line
/^.$ line with only one character
/the string the, as in the, other, there, their, etc.

Really, this only barely scratches the surface. There is much, much, much more to this than what I've shown here. But this is a good start.

As an exercise to the reader, try to guess what the search pattern for the following is:

1- Search for any line beginning with a number.
2- Search for any line that is NOT empty (might be tricky).
3- Search for any of the strings cat, cet, cit, cot, cut.

Joining Two Lines

A common question is "how do I join two lines". Place the cursor on the first line and type J. The line below the cursor will be joined to the current line. Try joining two lines together and then undo the join using u.

Cutting and Pasting

People who use vi call this "yanking and putting". You'll see why. Here are the commands:

[n]yy yank n lines into the general buffer ("yy" yanks 1 line).
[n]p put general buffer below CCP
[n]P put general buffer above CCP

As an example, place the cursor at the beginning of the file (The time has come...) and type 2yy. Now move the cursor to the "And cabbages and kings" line and press p.

Go ahead and play with this feature. Make sure you get a good feel for the difference beween P and p. Remember, you can always restore your testfile by pressing u a few times.

Questions or comments should be directed to the author, Peter Jay Salzman, at p@dirac.org.
Homepage: www.dirac.org.