Book HomeBook TitleSearch this book

Chapter 2. Command-Line Editing

Contents:

Enabling Command-Line Editing
The History File
Emacs Editing Mode
Vi Editing Mode
The hist Command
Finger Habits

It's always possible to make mistakes when you type at a computer keyboard, but perhaps even more so when you are using a Unix shell. Unix shell syntax is powerful, yet terse, full of odd characters, and not particularly mnemonic, making it possible to construct command lines that are as cryptic as they are complex. The Bourne and C shells exacerbate this situation by giving you extremely limited ways of editing your command lines.

In particular, there is no way to recall a previous command line so that you can fix a mistake. For example, in Chapter 7 we'll see complex command lines like:

eval cat \$srcname \| ccom \| optimize \| as \> \$objname

If you are an experienced Bourne shell user, undoubtedly you know the frustration of having to retype lines like this. You can use the backspace key to edit, but once you hit ENTER, it's gone forever!

The C shell provides a small improvement via its history mechanism, which provides a few very awkward ways of editing previous commands. But there are more than a few people who have wondered, "Why can't I edit my Unix command lines in the same way I can edit text with an editor?"

This is exactly what the Korn shell allows you to do. It has editing modes that allow you to edit command lines with editing commands similar to those of the two most popular Unix editors, vi and Emacs.[18] It also provides a much-extended analogue to the C shell history mechanism called hist (for "history") that, among other things, allows you to use your favorite editor directly for editing your command lines.

[18] For some unknown reason, the documentation on emacs-mode has been removed from the ksh(1) manual pages on some Unix systems. This does not mean, however, that the mode doesn't exist or doesn't work properly.

In this chapter, we discuss features common to all of the Korn shell's command-history facilities; then we deal with each such facility in detail. If you use vi or Emacs, you may wish to read only the section on the emulation mode for the one you use.[19] If you use neither vi or Emacs but are interested in learning one of the editing modes anyway, we suggest emacs-mode, because it is more of a natural extension of the minimal editing capability you get with the bare shell.

[19] You will get the most out of these sections if you are already familiar with the editor(s) in question. Good sources for more complete information on the editors are Learning the vi Editor by Linda Lamb and Arnold Robbins and Learning GNU Emacs by Debra Cameron, Bill Rosenblatt, and Eric Raymond. Both are published by O'Reilly & Associates.

We should mention up front that both emacs- and vi-modes introduce the potential for clashes with control keys set up by the Unix terminal interface. Recall the control keys shown in Chapter 1 in Table 1-7 and the sample stty command output. The control keys shown there override their functions in the editing modes.

During the rest of this chapter, we warn you when an editing command clashes with the default setting of a terminal-interface control key. But if you (or your system administrator) choose to customize your terminal interface, as we show in Chapter 8, you're on your own as far as the editing modes are concerned.

2.1. Enabling Command-Line Editing

There are two ways of entering either editing mode. First, you can set your editing mode by using the environment variable VISUAL. The Korn shell checks to see if this variable ends with vi or macs.[20] An excellent way to set VISUAL is to put a line like the following in your .profile or environment file:

[20] GNU Emacs is sometimes installed as gmacs or gnumacs.

VISUAL=$(whence emacs)

or

VISUAL=$(whence vi)

As you will find out in Chapter 3 and Chapter 4, the whence built-in command takes the name of another command as its argument and writes the command's full pathname on the standard output; the form $(command) returns the standard output generated by command as a string value. Thus, the line above finds out the full pathname of your favorite editor and stores it in the environment variable VISUAL. The advantage of this code is that it is portable to other systems, which may have the executables for editors stored in different directories.

The second way of selecting an editing mode is to set the option explicitly with the set -o command:

set -o emacs

or

set -o vi

vi users may wish to add:

set -o viraw

along with set -o vi. This enables TAB completion in recent versions of ksh93. The additional overhead, particularly on single-user systems, is nominal and, in any case, is no worse than that of emacs-mode. (Starting with ksh93n, the viraw option is automatically enabled when you use vi-mode.)

You will find that the vi and emacs editing modes are good at emulating the basic commands of these editors, but not advanced features; their main purpose is to let you transfer "finger habits" from your favorite editor to the shell. hist is a powerful facility; it is mainly meant to supplant C shell history and as an "escape hatch" for users of editors other than vi or Emacs. Therefore the section on hist is recommended mostly to C shell users and those who don't use either standard editor.

Before diving into the details, it's worth mentioning two other points that apply to both editing modes:



Library Navigation Links

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