vi and vim

Vi is a very powerful command-line text editor. It's used for everything from quick fixes in configuration files to professional programming and even for writing large, complex documents like this book. It's popular on the one hand because it's fast and light-weight, and you can accomplish a lot with a few keystrokes. On the other hand it's also powerful: highly configurable, with many built-in functions.

Vim is an enhanced version of Vi, offering a lot of features that make life easier for both the novice and expert (Vim stands for "Vi IMproved'). On many modern systems, Vim is installed as the default version of Vi. So if you invoke the vi command you actually run Vim. This is usually not confusing, because everything in Vi works in Vim as well. We will look at vim in this chapter, but if your system has vi you can apply these techniques, just replace any reference to vim in the commands to vi.

The best feature about Vim/Vi is that it's shipped with virtually all GNU/Linux variants by default. Once you learn Vim, whenever you are, you can have the power of efficient editing.

The main drawback of Vim is, as for Emacs (another command line editor), the learning curve. The keyboard short-cuts a can be daunting to learn.

Fortunately, you can work around those draw backs by using a graphical version of Vim (GVim) with all the buttons and menus for more graphical users. You can also try easy-Vim, with a Notepad style editing.

These simplified versions of Vim reduce the learning curve a lot and expose less advanced users to the power of efficient editing, which in turn increases one's will to learn a more powerful editor.

Basic commands

To open Vim and begin creating a new text file, get a command-line open and type:

$ vim

This presents you with a blank screen, or (if the program running in Vim) a screen of information looking something like this:

vim

If you want to open an existing file, just specify it on the command line as an argument. For instance, the following opens a file called /etc/fstab:

$ vim /etc/fstab

This file already exists on most GNU/Linux systems, but if you open a non-existent file, you'll get a blank screen. The next section shows you how to insert text; when you're finished you can then save the file.

Inserting Text

Whether you have a blank screen or a file with text in it, you can add text by entering what's edit mode. Just press i. You should see this on the bottom of the screen:

-- INSERT --

Whenever this appears on the bottom of the screen, you are in edit mode. Whatever you type becomes part of the file. For instance, try entering "This is line 1." Then press the Enter key and enter "This is line 2". Here's what this fascinating contribution to literature looks like in Vim:

vim_insert

When you are finished inserting text, press the Escape key to leave edit mode; that puts you in normal mode.

Only One Place At a Time: The Cursor

Vim, like every editor, keeps track of where you are and shows a cursor at that point, which may look like an underline or a box in a different color. In edit mode, you can backspace to remove characters. Vim also allows you to move around using arrow keys and edit the whole document freely. But normal Vi doesn't let you move around; it restricts you to adding text or backspacing to remove it.

In normal Vi, if you want to go back over what you edited, or move to another place in the file, you must press the Escape key, move to the place where you want to insert text, and enter edit mode again. This may seem cumbersome. But Vi provides so many alternate ways of moving around and adding text that you'll find, with some practice, that it's no barrier to productivity. As already mentioned, Vim lets you move around freely and edit the whole file when you're in edit mode--but you'll find yourself leaving edit mode often in order to make use of Vim's powerful commands.

Basic Movement Commands

To practice moving around in a file, you can press the Escape key to get out of insert mode. If you have only a small amount of text in the file, you may prefer to find another text file on your system that's larger, and open that. Remember that when you open a file you're in normal mode, not edit mode.

To move around use the arrow keys.

To jump to a specific line use the colon button followed by a line number. The following will jump to line 20:

:20

You can move quickly up or down a text file by pressing the PgUp and PgDn keys.

Search for text by pressing the slash key (/) and then typing the text you want to find:

/birthday party

You can simply repeat the / key to search for the next occurrence of the string. The search is case-sensitive. To search backward, press the question-mark key (?) instead of the slash key.

Saving and exiting

If you're in edit mode, you can save your changes by pressing Escape to go to normal mode, typing :w and pressing the Enter key. This saves your changes to the file you specified when you opened Vim.

Note: Vim, by default, does not save a backup of the original version of the file. Your :w command deletes the old contents forever. Vim can be configured to save backups, though.

If you opened Vim without specifying a file name, you receive an error message when you press :w:

E32: No file name

To fix this, specify :w with a filename:

:w mytestfile.txt

This must also be followed by the Enter key.

To exit Vim, press :q. If you have unsaved text, you receive an error message:

E37: No write since last change (add ! to override)

Like most word processors, Vim tries to warn you when you might make a mistake that costs you work.  As the message suggests, you can abandon your text and exit by pressing :q!. Or use :w to save your changes and then enter :q again. You can combine writing and quitting through any of the following:

:wq (followed by Enter)
:x (followed by Enter)
ZZ

Practice!