Learning the Korn Shell

Learning the Korn ShellSearch this book
Previous: 1.9 Special Characters and Quoting Chapter 2 Next: 2.2 The History File

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, 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.

[1] For some unknown reason, the documentation on emacs-mode has been removed from 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 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.

[2] 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 the O'Reilly & Associates Nutshell Handbooks Learning the vi Editor , by Linda Lamb, and Learning GNU Emacs , by Debra Cameron and Bill Rosenblatt.

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 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 . [3] An excellent way to set VISUAL is to put a line like the following in your .profile or environment file:

[3] GNU Emacs is often installed as gmacs or gnumacs .

VISUAL=$(whence emacs)


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


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.

Previous: 1.9 Special Characters and Quoting Learning the Korn Shell Next: 2.2 The History File
1.9 Special Characters and Quoting Book Index 2.2 The History File

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System