Abstract
Vi was the first full-screen editor in existence. It is one of the main programs UNIX® detractors point to, but also one of the better arguments of its defenders: while it is complicated to learn, it is also an extremely powerful tool once you get into the habit of using it. With a few keystrokes, a Vi user can move mountains, and other than Emacs, few text editors can make the same claims.
The version supplied with Mandriva Linux is in fact Vim, for VI iMproved, but we will refer to it as Vi throughout this chapter.
If you wish to learn more about Vi, you can have a look at this Hands-On Introduction to the Vi Editor or at the Vim home page.
To begin using Vi we use the same sort of command line as we did with Emacs. So let us go back to our two files and type:
$ vi file1 file2
At this point, you will find yourself looking at a window resembling the following one:
You are now in command mode in front of the first opened file. In this mode you cannot insert text into a file. To do so you must switch to insert mode.
Here are some shortcuts to inserting text:
![]() | Note |
---|---|
Please note that keyboard shortcut combinations must be typed exactly as shown, Vi distinguishes between capital and lowercase letters in commands, so the a command is not the same as the A one. |
In insert mode, you will
see the string --INSERT--
appear at the bottom of the
screen (so you know which mode you are in). This is the only mode which
will allow you to insert text. To return to command mode, press the
Esc key.
In insert mode, you can use the Backspace and Del keys to delete text as you go along. The arrow keys will allow you to move around within the text in Command mode and Insert mode. In command mode, there are also other key combinations which we will look at later.
ex mode is
accessed by pressing the : key in command mode. A
:
will appear at the bottom left of the screen with the
cursor positioned after it. Vi will consider everything you
type up to the Enter key as an ex
command. If you delete the command and the : you typed
in, you will be returned to command mode and the cursor will go back to
its original position in the text.
![]() | Tip |
---|---|
You have command completion available while on ex mode, type the first letters of the command and press the Tab key to complete it. |
To save changes to a file type
:w
in command mode. If you want to save the
contents of the buffer to another file, type :w
<file_name>
.
To move, in the same
buffer, between the files whose names were passed on the command line,
type :next
to move to the next file and
:prev
to move to the previous file. You can also
use :e <file_name>
, which allows
you to either change to the desired file if it is already open, or to
open another file. You may also use completion.
As with Emacs, you can have
several buffers displayed on the screen. To do this, use the
:split
command.
To change buffers,
type
Ctrl-w
j to go to the buffer below or
Ctrl-w
k to go to the buffer above. You can also use
the up and down arrow keys instead of j or
k. The :close
command
hides a buffer and the :q
command closes
it.
You should be aware that if you try to hide or close a buffer without saving the changes, the command will not be carried out and Vi will display this message:
Apart from the Backspace and Del keys in edit mode, Vi has many other commands for deleting, copying, pasting, and replacing text in command mode. All the commands shown hereafter are in fact separated into two parts: the action to be performed and its effect. The action may be:
The effect defines which group of characters the command acts upon.
h, j, k, l: one character left, down, up, right[20]
e, b, w: to the end, beginning of the current word and the beginning of the next word.
^, 0, $: to the first non-blank character of the current line, the beginning of the current line, and the end of current line.
f<x>
: go to next
occurrence of character
<x>
. For example,
fe
moves the cursor to the next
occurrence of the character e
.
/<string>
,
?<string>
: to the next and
previous occurrence of string or regexp
<string>
. For example,
/foobar
moves the cursor to the next
occurrence of the word foobar
.
Each of these “effect” characters or move commands can be preceded by a repetition number. For G, (“Go”) this references the line number in the file. Based on this information, you can make all sorts of combinations.
While many of these commands are not very intuitive, the best method to remember the commands is to practice them. But you can see that the expression “move mountains with a few keys” is not much of an exaggeration.
Vi contains a command which we have already seen for copying text: the y command. To cut text, simply use the d command. There are 27 memories or buffers for storing text: an anonymous memory and 26 memories named after the 26 lowercase letters of the English alphabet.
To use the anonymous
memory you enter the command “as is”. So the command
y12w
will copy the 12 words after the cursor into
anonymous memory[21]. Use d12w
if you want to cut this
area.
To use one of the
26 named memories, enter the sequence
"<x>
before the command, where
<x>
gives the name of the
memory. Therefore, to copy the same 12 words into the memory
k
, you would write
"ky12w
, or
"kd12w
to cut them.
To paste the
contents of the anonymous memory, use the commands
p
or P
(for
Paste), to insert text after or
before the cursor. To paste the contents of a named memory, use
"<x>p
or
"<x>P
in the same way (for
example "dp
will paste the contents
of memory d
after the cursor).
To carry out this action, we will:
recopy the
first 6 words of the sentence into memory
r
(for example):
"ry6w
[22];
We get the expected result, as shown in Figure 8.6, “VIM, after having copied the text block”.
Searching for text
is very simple: in command mode, you simply type
/ followed by the string to search for, and
then press the Enter key. For example,
/party
will search for the string
party
from the current cursor
position. Pressing n takes you to the next
occurrence, and if you reach the end of the file, the search
will start again from the beginning. To search backwards, use
? instead of /.
The command to quit is
:q
(in fact, this command actually closes the
active buffer, as we have already seen, but if it is the only buffer
open, you will quit Vi). There is a shortcut: most of the
time you edit only one file. So to quit, you will use:
You should note that if you have
several buffers, :wq
will only write the active
buffer and then close it.