< Zurück | Inhalt | Weiter >

2.2.4 Starting Off Right: .exrc

You can preset certain behaviors in vi by putting ex commands in a file called

.exrc in your home directory. Those commands will be read whenever you invoke vi and before you begin typing commands.

Here’s a simple but useful .exrc example:

" set my favorite options: set autoindent shiftwidth=4 set ignorecase

As you can see from the example, settings can be combined on one line. Note also that these lines do not begin with a colon. A colon is needed if you type these lines from within vi—because you need to get into ex mode; but since these are assumed to be ex commands (hence the name .exrc) they are going straight to the ex side of vi and no colon is needed. Comment lines begin with a double quote; the rest of the line is ignored, and doesn’t need a matching quote.

The ignorecase command tells vi to ignore any difference between upper- and lowercase characters when searching for text with the / or ? com- mands. The single character searches on the current line (f and F) are not

affected by this setting. The default for vi is noignorecase, which means case is significant.

The autoindent setting (can be abbreviated ai) means that when you do an o or O to open a line after or before (o versus O) the line on which your cursor rests, vi will automatically add whitespace so that the text that you enter begins at the same column where the current line begins.

For example, suppose you are editing an if statement like this:

if (userBalance < minDaily) { userAccount.chargeFees();


With your cursor on the middle line, if you type an o or O, the new (blank)

line will open with your cursor at the fifth character position, right in line with the “u” of userAccount. If you find that your cursor is flush left when you try this, then you need to set autoindent. You can do this from within vi by typing

:set ai or the longer :set autoindent. The leading “:” is important—it gets you to ex mode. (Don’t forget to press Enter at the end of the command.)

The shiftwidth setting tells vi how many character positions to move

text left or right with each << (left) or >> (right) command, as well as when typing ^D and ^T in input mode. When typing text in input mode, people often use the Tab key to indent their text, for example inside an if or for statement. You can do this in vi, and the actual tab character will be the character in your text. But if you want tighter indenting, use the ^T (that’s Control-T, “T” for Tab, we suppose) to increase your indent and ^D to decrease your indent while in input mode. The vi editor will automatically compute how much whitespace to use and will put an optimal combination of tabs and spaces as needed to line things up. Alternately, you can have vi always expand tabs into spaces and not mix tabs and spaces, but just use spaces, with set expandtab. (Remember to add a “:” if you want to type this from the command line.)


There are many editors available to a programmer on a Linux system. Some are text-based editors, typically antedating GUI interfaces. Many have graphical interfaces, with mouse-based cut and paste and the like. We will mention sev- eral here and encourage you to find the one with which you are comfortable.

The list of choices for editors is quite long. One of the beauties of the Open Source approach is that personal choices like favorite editors aren’t squashed by arbitrary decisions: If you want, you can write an editor; others can adopt it.

Test drive a few; try them on for size. Remember that there can be a learning curve to climb. Don’t necessarily settle for the easiest to learn—it may not be able to handle all that you’ll need it to do, which may cost you more in the long run.

Speaking of editors that aren’t easy to learn, we can’t discuss editors without a mention of Emacs. To quote the GNU Emacs project home page:4

Emacs is the extensible, customizable, self-documenting real-time display editor. If this seems to be a bit of a mouthful, an easier explanation is Emacs is a text editor and more. At its core is an interpreter for Emacs Lisp (“elisp,” for short), a dialect of the Lisp programming language with extensions to support text editing. Some of the features of GNU Emacs include:

• Content sensitive major modes for a wide variety of file types, from plain text to source code to HTML files.

• Complete online documentation, including a tutorial for new users.

• Highly extensible through the Emacs Lisp language.

• Support for many languages and their scripts, including all the Euro- pean “Latin” scripts, Russian, Greek, Japanese, Chinese, Korean, Thai, Vietnamese, Lao, Ethiopian, and some Indian scripts. (Sorry, Mayan hieroglyphs are not supported.)

• A large number of extensions which add other functionality. The GNU Emacs distribution includes many extensions; many others are available separately—even a Web browser.

There is another variant of Emacs called XEmacs. It came from the same code base but split over differences both technical and philosophical.5 Now if you thought that vi had a lot of obscure key sequences, you ain’t seen nothin’ yet. With its Lisp interpreter Emacs is incredibly extensible and powerful, but


4. This is from http://www.gnu.org/software/emacs/emacs.php#Whatis.

5. If you want to read more about those differences, and how they came about, see


has a huge learning curve—which is why we aren’t going to cover Emacs or XEmacs at all in this book.

Our favorite editor is still vi—in part, we’re sure, because we already know

it so well. But like any skilled craftsman, even though you may have a favorite hammer or saw that you use on most of your work, you will still have several others ready in your toolkit, and use specialized ones for certain tasks.