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; two interesting alternatives are the sam and Acme editors from Bell Labs. Vim also provides an X-based interface.
vi is the most useful standard text editor on your system. (vi is short for visual editor and is pronounced “vee-eye.” This is illustrated graphically in Figure 1-1.) Unlike Emacs, it is available in nearly identical form on every modern Unix system, thus providing a kind of text-editing lingua franca. The same might be said of ed and ex, but screen editors are generally much easier to use. (So much so, in fact, that line editors have generally fallen into disuse.) 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.
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, they give you much more control over the appearance of your documents than a word processor. Unix supports the troff formatting package. The TeX and LaTeX formatters are popular, commonly available alternatives.
(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. In addition, Vim version 7 provides automatic spellchecking.)
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 new or 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.)
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. You never have to remove your hands from the keyboard, or mess around with multiple levels of menus! Most of the commands can be remembered by the letters that perform 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 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
 These days, the term “Unix” includes both commercial systems derived from the original Unix code base, and Unix work-alikes whose source code is available. Solaris, AIX, and HP-UX are examples of the former, and GNU/Linux and the various BSD-derived systems are examples of the latter. Unless otherwise noted, everything in this book applies across the board to all those systems.
 GNU Emacs has become the universal version of Emacs. The only problem is that it doesn’t come standard with most commercial Unix systems; you must retrieve and install it yourself.
 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. Today, anyone using troff uses the GNU version, groff. See http://www.gnu.org/software/groff/ for more information.