home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning the vi EditorSearch this book

Chapter 1. The vi Text Editor

UNIX has a number of editors that can process the contents of text files, whether those files contain data, source code, or sentences. There are line editors, such as ed and ex, which display a line of the file on the screen; and there are screen editors, such as vi and emacs, which display a part of the file on your terminal screen. Text editors based on the X Window System are also commonly available, and are becoming increasing popular. Both GNU emacs and its derivative xemacs provide multiple X windows; an interesting alternative is the sam editor from Bell Labs. All but one of the vi clones described in Part II of this book also provide X-based interfaces.

vi is the most useful standard text editor on your system. (vi is short for visual editor and is pronounced "vee-eye.") Unlike emacs, it is available in nearly identical form on almost every UNIX system, thus providing a kind of text-editing lingua franca.[3] The same might be said of ed and ex, but screen editors are generally much easier to use. With a screen editor, you can scroll the page, move the cursor, delete lines, insert characters, and more, while seeing the results of your edits as you make them. Screen editors are very popular, since they allow you to make changes as you read through a file, like you would edit a printed copy, only faster.

[3]Actually, these days, GNU emacs is pretty much the universal version of emacs; the only problem is it doesn't come standard with most commercial UNIX systems; you must retrieve and install it yourself.

To many beginners, vi looks unintuitive and cumbersome—instead of using special control keys for word processing functions and just letting you type normally, it uses all of the regular keyboard keys for issuing commands. When the keyboard keys are issuing commands, vi is said to be in command mode. You must be in a special insert mode before you can type actual text on the screen. In addition, there seem to be so many commands.

Once you start learning, however, you realize that vi is well designed. You need only a few keystrokes to tell vi to do complex tasks. As you learn vi, you learn shortcuts that transfer more and more of the editing work to the computer—where it belongs.

vi (like any text editor) is not a "what you see is what you get" word processor. If you want to produce formatted documents, you must type in codes that are used by another formatting program to control the appearance of the printed copy. If you want to indent several paragraphs, for instance, you put a code where the indent begins and ends. Formatting codes allow you to experiment with or change the appearance of your printed files, and in many ways, give you much more control over the appearance of your documents than a word processor. UNIX supports the troff formatting package.[4] The and formatters are popular, commonly available alternatives.

[4]troff is for laser printers and typesetters. Its "twin brother" is nroff, for line printers and terminals. Both accept the same input language. Following common UNIX convention, we refer to both with the name troff.

(vi does support some simple formatting mechanisms. For example, you can tell it to automatically wrap when you come to the end of a line, or to automatically indent new lines.)

As with any skill, the more editing you do, the easier the basics become, and the more you can accomplish. Once you are used to all the powers you have while editing with vi, you may never want to return to any "simpler" editor.

What are the components of editing? First, you want to insert text (a forgotten word or a missing sentence), and you want to delete text (a stray character or an entire paragraph). You also need to change letters and words (to correct misspellings or to reflect a change of mind about a term). You might want to move text from one place to another part of your file. And, on occasion, you want to copy text to duplicate it in another part of your file.

Unlike many word processors, vi's command mode is the initial or "default" mode. Complex, interactive edits can be performed with only a few keystrokes. (And to insert raw text, you simply give any of the several "insert" commands and then type away.)

One or two characters are used for the basic commands. For example:

i
insert

cw
change word

Using letters as commands, you can edit a file with great speed. You don't have to memorize banks of function keys or stretch your fingers to reach awkward combinations of keys. Most of the commands can be remembered by the letter that performs them, and nearly all commands follow similar patterns and are related to each other.

In general, vi commands:

There is also a group of commands that echo on the bottom line of the screen. Bottom-line commands are preceded by different symbols. The slash (/) and the question mark (?) begin search commands, and are discussed in Chapter 3. A colon (:) begins all ex commands. ex commands are those that are used by the ex line editor. The ex editor is available to you when you use vi, because ex is the underlying editor, and vi is really just its "visual" mode. ex commands and concepts are discussed fully in Chapter 5, but this chapter introduces you to the ex commands to quit a file without saving edits.

1.1. Opening and Closing Files

You can use vi to edit any text file. vi copies the file to be edited into a buffer (an area temporarily set aside in memory), displays the buffer (though you can see only one screenful at a time), and lets you add, delete, and change text. When you save your edits, vi copies the edited buffer back into a permanent file, replacing the old file of the same name. Remember that you are always working on a copy of your file in the buffer, and that your edits will not affect your original file until you save the buffer. Saving your edits is also called "writing the buffer," or more commonly, "writing your file."

1.1.1. Opening a File

Figure Figure vi is the UNIX command that invokes the vi editor for an existing file or for a brand new file. The syntax for the vi command is:

$ vi [filename]

The brackets shown on the above command line indicate that the filename is optional. The brackets should not be typed. The $ is the UNIX prompt. If the filename is omitted, vi will open an unnamed buffer. You can assign the name when you write the buffer into a file. For right now, though, let's stick to naming the file on the command line.

A filename must be unique inside its directory. On older System V UNIX systems, it cannot exceed 14 characters in length (most common UNIX systems allow much longer names). A filename can include any 8-bit character except a slash (/), which is reserved as the separator between files and directories in a pathname, and ASCII NUL, the character with all zero bits. You can even include spaces in a filename by typing a backslash (\) before the space. In practice, though, filenames generally consist of any combination of uppercase and lowercase letters, numbers, and the characters dot (.) and underscore (_). Remember that UNIX is case-sensitive: lowercase letters are distinct from uppercase letters. Also remember that you must press RETURN to tell UNIX that you are finished issuing your command.

When you want to open a new file in a directory, give a new filename with the vi command. For example, if you want to open a new file called practice in the current directory, you would enter:

$ vi practice

Since this is a new file, the buffer is empty and the screen appears as follows:

~
~
~
"practice" [New file].

The tildes (~) down the left-hand column of the screen indicate that there is no text in the file, not even blank lines. The prompt line (also called the status line) at the bottom of the screen echoes the name and status of the file.

You can also edit any existing text file in a directory by specifying its filename. Suppose that there is a UNIX file with the pathname /home/john/letter. If you are already in the /home/john directory, use the relative pathname. For example:

$ vi letter

brings a copy of the file letter to the screen.

If you are in another directory, give the full pathname to begin editing:

$ vi /home/john/letter

1.1.2. Problems Opening Files



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.