Chapter 2. Command-Line Editing
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. 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.
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. 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.
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.
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. An excellent way to set VISUAL is to put a line like the following in your .profile or environment file:
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.
set -o emacs
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:
Copyright © 2003 O'Reilly & Associates. All rights reserved.