The Tutorial
Basic Editing
Editing a Little Faster
Searching
Text Blocks and Multiple Files
1
2
3
4
5 Windows
6
7
8
9
10
11
12
13
14
15 Miscellaneous Commands
16
17
Basic Visual Mode
Commands for Programmers
Basic Abbreviations, Keyboard Mapping, and Initialization Files
Basic Command-Mode Commands
Basic GUI Usage
Dealing with Text Files
Automatic Completion
Autocommands
File Recovery and Command-Line Arguments
Cookbook
Topics Not Covered
Basic Editing
THE VIM EDITOR IS ONE OF THE MOST powerful text editors around. It is also
extremely efficient, enabling the user to edit files with a minimum of keystrokes.This
power and functionality comes at a cost, however:When getting started, users face a
steep learning curve.
This chapter teaches you the basic set of 10 Vim commands you need to get started
editing. In this chapter, you learn the following:
n The four basic movement commands
n How to insert and delete text
n How to get help (very important)
n Exiting the editor
After you get these commands down pat, you can learn the more advanced editing
commands.
Before You Start
If you have not installed Vim, you need to read Appendix A, “Installing Vim,” and
install the editor.
4
Chapter 1 Basic Editing
If you are running on UNIX, execute the following command:
$ touch ~/.vimrc
By creating a ~/.vimrc, you tell Vim that you want to use it in Vim mode. If this file is
not present, Vim runs in Vi-compatibility mode and you lose access to many of the
advanced Vim features. However, you can enable the advanced features from within
Vim at any time with this command: :set nocompatible.
If you are running on Microsoft Windows, the installation process creates the
Microsoft Windows version of this file, _vimrc, for you.
Running Vim for the First Time
To start Vim, enter this command:
$ gvim file.txt
Note that the $ is the default UNIX command prompt.Your prompt might differ.
If you are running Microsoft Windows, open an MS-DOS prompt window and
enter this command:
C:> gvim file.txt
(Again, your prompt may differ.)
In either case, Vim starts editing a file called file.txt. Because this is a new file, you
get a blank window. Figure 1.1 shows what your screen will look like.
The tilde (~) lines indicate lines not in the file. In other words, when Vim runs out
of file to display, it displays tilde lines. At the bottom of a screen, a message line indi-
cates the file is named file.txt and shows that you are creating a new file.The mes-
sage information is temporary and other information overwrites it when you type the
first character.
~
~
~
~
~
~
~
~
“file.txt” [New File]
Figure 1.1
Initial Vim window.
Editing for the First Time
5
The vim Command
The gvim command causes the editor to create a new window for editing. If you use
the command vim, the editing occurs inside your command window. In other words, if
you are running inside an xterm, the editor uses your xterm window. If you are using
an MS-DOS command prompt window under Microsoft Windows, the editing occurs
inside the window. Figure 1.2 shows a typical MS-DOS command prompt window.
The system, you see,
Ran as slow as did he,
A very intelligent turtle
Found programming UNIX a hurdle
And that's not saying much for the turtle.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"turtle.txt" 5L, 158C
1,1
All
Figure 1.2
Editing with the vim command in an MS-DOS window.
Modes
The Vim editor is a modal editor.That means that the editor behaves differently,
depending on which mode you are in. If the bottom of the screen displays the file-
name or is blank, you are in normal mode. If you are in insert mode, the indicator dis-
plays --INSERT--; and if you are in visual mode, the indicator shows --VISUAL--.
Editing for the First Time
The next few sections show you how to edit your first file. During this process, you
learn the basic commands that you have to know to use Vim. At the end of this lesson,
you will know how to edit—not fast, not efficiently, but enough to get the job done.
Inserting Text
To enter text, you need to be in insert mode.Type i, and notice that the lower left of
the screen changes to --INSERT-- (meaning that you are in insert mode).
6
Chapter 1 Basic Editing
Now type some text. It will be inserted into the file. Do not worry if you make
mistakes; you can correct them later. Enter the following programmer’s limerick:
A very intelligent turtle
Found programming UNIX a hurdle
The system, you see,
Ran as slow as did he,
And that’s not saying much for the turtle.
After you have finished inserting, press the key.The --INSERT-- indicator goes
away and you return to command mode.
Your screen should now look something like Figure 1.3.
The system, you see,
Ran as slow as did he,
A very intelligent turtle
Found programming UNIX a hurdle
And that's not saying much for the turtle.
~
~
~
~
Figure 1.3
Screen after the text has been inserted.
Getting Out of Trouble
One of the problems for Vim novices is mode confusion, which is caused by forgetting
which mode you are in or by accidentally typing a command that switches modes.To
get back to normal mode, no matter what mode you are in, press the key.
Moving Around
After you return to command mode, you can move around by using these keys: h
(left), j (down), k (up), and l (right). At first, it may appear that these commands were
chosen at random. After all, who ever heard of using l for right? But actually, there is a
very good reason for these choices: Moving the cursor is the most common thing you
do in an editor, and these keys are on the home row of your right hand. In other
words, these commands are placed where you can type them the fastest.
Note
You can also move the cursor by using the arrow keys. If you do, however, you greatly slow down your
editing—because to press the arrow keys, you must move your hand from the text keys to the arrow keys.
Considering that you might be doing it hundreds of times an hour, this can take a significant amount of
time. If you want to edit efficiently, use h, j, k, and l.
Also, there are keyboards which do not have arrow keys, or which locate them in unusual places;
therefore, knowing the use of these keys helps in those situations.
One way to remember these commands is that h is on the left, l is on the right, j is a
hook down, and k points up. Another good way to remember the commands is to
copy this information on a Post-It Note and put it on the edge of your monitor until
you get used to these commands.
Editing for the First Time
7
h
k
j
l
Deleting Characters
To delete a character, move the cursor over it and type x. (This is a throwback to the
old days of the typewriter, when you deleted things by typing xxxx over them.)
Move the cursor to the beginning of the first line, for example, and type xxxxxxx
(eight x’s) to delete the first eight characters on the line. Figure 1.4 shows the result.
To enter a correction, type iA young .This begins an insert (the i), inserts the
words A young, and then exits insert mode (the final ). Figure 1.5 shows the
results.
The system, you see,
Ran as slow as did he,
intelligent turtle
Found programming UNIX a hurdle
And that's not saying much for the turtle.
~
~
~
~
Figure 1.4
Screen after delete (xxxxxxxx).
The system, you see,
Ran as slow as did he,
A young intelligent turtle
Found programming UNIX a hurdle
And that's not saying much for the turtle.
~
~
~
~
Figure 1.5
Result of the insert.
Note
Vim is a text editor. By default, it does not wrap text. You must end each line by pressing the
key. If you don’t and just keep typing when you reach the right margin, all you will do is insert a very
long line into the editor. You will not automatically go to the next line. To do so, you need to press the
key. (This is the default mode of operation. You can configure the Vim editor to word wrap, how-
ever, as discussed in Chapter 11, “Dealing with Text Files.”)
8
Chapter 1 Basic Editing
Undo and Redo
Suppose you delete too much.Well, you could type it in again, but an easier way
exists.The u command undoes the last edit.
Take a look at this in action. Move the cursor to the A in the first line. Now type
xxxxxxx to delete A young.The result is as follows:
intelligent turtle
Type u to undo the last delete.That delete removed the g, so the undo restores the
character.
g intelligent turtle
The next u command restores the next-to-last character deleted:
ng intelligent turtle
The next u command gives you the u, and so on:
ung intelligent turtle
oung intelligent turtle
young intelligent turtle
young intelligent turtle
A young intelligent turtle
If you undo too many times, you can press CTRL-R (redo) to reverse the preceding
command. In other words, it undoes the undo.
To see this in action, press CTRL-R twice.The character A and the space after it
disappear.
young intelligent turtle
There’s a special version of the undo command, the U (undo line) command.The
undo line command undoes all the changes made on the last line that was edited.
Typing this command twice cancels the preceding U.
Note
If you are an old Vi user, note that the multilevel undo of Vim differs significantly from the single level
available to a Vi user.
Note
Throughout this book we assume that you have turned off Vi compatibility. (Vi compatiblity disables
many advanced features of Vim in order to be compatible with Vi.) This feature is automatically turned
off for Unix users when they create the $HOME/.vimrc file. For Microsoft Windows, it is turned off during
installation. (If compatibility is turned on the v command provides one level of undo.)