2. Command-line Editing
Contents:
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, Input/Output and Command-line Processing we'll see complex command lines like:
$ eval cat $srcname | ccom | as | optimize > $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 RETURN, it's gone forever! The C shell provided 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 . [1] It also provides a much-extended analog to the C shell history mechanism called fc (for fix command) that, among other things, allows you to use your favorite editor directly for editing your command lines.
In this chapter, we will discuss features common to all of the Korn shell's command-history facilities; then we will 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. [2] 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, Korn Shell Basics 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'll 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'll show in Chapter 8, Process Handling you're on your own as far as the editing modes are concerned. 2.1 Enabling Command-line EditingThere 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 . [3] An excellent way to set VISUAL is to put a line like the following in your .profile or environment file:
VISUAL=$(whence emacs) or
VISUAL=$(whence vi) As you will find out in Chapter 3, Customizing Your Environment and Chapter 4, Basic Shell Programming 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 You will find that the vi- and emacs-editing modes are good at emulating the basic commands of these editors but not their advanced features; their main purpose is to let you transfer "finger habits" from your favorite editor to the shell. fc is quite 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 fc is mainly recommended to C shell users and those who don't use either standard editor. |
|