3.2. Vi: the ancestor

Vi was the first full-screen editor in existence. Vi makes for one of the main objections of UNIX detractors, but also one of the main arguments of its defenders: while it is complicated to learn, it is also an extremely powerful tool once one gets into the habit of using it. With a few keystrokes, a Vi user can move mountains, and apart from Emacs, few text editors can boast the same.

The version supplied with Mandrake Linux is in fact vim, for VI iMproved, but we will call it Vi throughout this chapter.

3.2.1. Insert mode, command mode, ex mode...

First, we need to start Vi, which is identical to Emacs. So let us go back to our two files and type:

$ vi file1 file2

At this point, you find yourself in front of a window resembling figure 3-4.

Figure 3-4. Starting position in vim

You are now in command mode in front of the first open file. In command mode, you cannot insert text into a file... To do this, you have to go into insert mode, and therefore enter one of the commands which allows you to do so:

In insert mode, you will see the string --INSERT-- appear at the bottom of the screen (so you know what mode you are in). It is in this and only this mode that you can enter 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. To move around text, both in command mode and in insert mode, you use the arrow keys. 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. The same : will appear at the bottom of the screen, and the cursor will be positioned on it. Everything you type subsequently, followed by pressing Enter, will be considered by Vi to be an ex command. If you delete the command and the : you typed in, you will return to command mode and the cursor will go back to its original position.

To save changes to a file you type :w in command mode. If you want to save the contents of the buffer to another file, type :w <file_name>.

3.2.2. Handling buffers

As with Emacs, you can have several buffers displayed on the screen. To do this, use the :split command.

To move from one file to another, in a buffer, you 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 either to change to the desired file if this is already open, or to open another file. Here again completion is available.

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, the :q command closes it.

Watch out, Vi is finicky: if you try to hide or close a buffer without saving the changes, the command will not be carried out and you will get this message:

No write since last change (use! to override)

In this case, do as you are told and type :q! or :close!.

3.2.3. Editing text and move commands

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. Here, we will look at a few. All the commands shown here 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. These also effect commands entered as they are in command mode correspond to movements:

Each of these effect characters or move commands can be preceded by a repetition number. For G, this references the line number in the file. On this basis, you can make all sorts of combinations.

Some examples:

It is true that these commands are not very intuitive, but as always the best method is practice. But you can see that the expression "move mountains with a few keys" is not much of an exaggeration :-)

3.2.4. Cut, copy, paste

Vi has a command that we have already seen for copying text: the y command. To cut text, simply use the d command. You have 27 memories for storing text: an anonymous memory and 26 memories named after the 26 lowercase letters of the alphabet.

To use the anonymous memory you enter the command as it is. So the command y12w copies to the anonymous memory the 12 words after the cursor[2]. 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. Thus, to copy the same 12 words into the memory k, you would write "ky12w, and "kd12w to cut them.

To paste the contents of the anonymous memory, you use the commands p or P (for Paste), to insert text after or before the cursor respectively. 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).

Let us look at the example of figure 3-5.

Figure 3-5. vim, before copying the text block

To carry out this action, we will:

We get the expected result, as shown in figure 3-6.

Figure 3-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 /.

3.2.5. Quit Vi

To quit, the command is :q (in fact, this command closes the active buffer, as we have already seen, but if it is the only buffer present, you quit Vi). There is a shortcut: most of the time you edit only one file. So to quit, you will use:

You will have noticed that if you have several buffers, :wq will write the active buffer then close it.

Notes

[1]

A shortcut for dl (delete one character forward) is x; a shortcut for dh is X; dd deletes the current line.

[2]

... if the cursor is positioned at the beginning of the first word!

[3]

y6w literally means: " Yank 6 words ".


Tux on Star from MandrakeSoft Linux is a registered trademark of Linus Torvalds. All other trademarks and copyrights are the property of their respective owners.
Unless otherwise stated, all the content of these pages and all images are Copyright MandrakeSoft S.A. and MandrakeSoft Inc. 2002.
http://www.mandrakelinux.com/