home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 11.12 Pass History to Another Shell Chapter 11
The Lessons of History
Next: 11.14 More Ways to Do Interactive History Editing
 

11.13 Shell Command-Line Editing

[This article was written for the Korn Shell. It also applies to bash and tcsh , though there are some differences. Check your shell's manpage. -JP]

If you make a typing mistake in your shell command line and press RETURN, it's gone. After you get the pesky error message, you'll have to type the whole thing in again - unless you happen to be using the C shell and happen to remember its Byzantine "history" commands (11.2 ) . If you use the Korn shell, you may know that it gives you a vi -like editing capability; if you've actually tried this, you probably agree that vi makes a lousy command-line editor. [I don't agree. :-) If you know vi , hit ESC on a command line to go to vi command mode; a or i takes you to input mode, as always. Hit RETURN from either mode to execute a command line - or CTRL-c to cancel it. -JP  ]

However, fewer people know that the Korn shell has another editing mode, one that emulates the Emacs (32.1 ) editor. The emacs mode editing commands act like a natural extension to traditional, simple shell editing commands (like Delete or Backspace for character erase), so even if you aren't familiar with the emacs editor, you should find emacs mode useful. To use emacs mode, put this line in your .profile :

set -o emacs

We'll cover only the most useful emacs mode commands here. For a more complete description, see O'Reilly & Associates' Learning the Korn Shell , by Bill Rosenblatt.

Emacs mode figures out what your character-erase key (5.9 ) is and lets you use it in the same way. In addition, it gives you the basic commands for editing a line listed in Table 11.1 .

Table 11.1: Korn Shell Emacs Mode Line Editing Commands
Command Function
CTRL-b Move backward one character (without deleting).
CTRL-f Move forward one character.
CTRL-d Delete one character forward.
CTRL-z Move to beginning of line.
CTRL-e Move to end of line.
CTRL-k Delete ("kill") forward to end of line.
CTRL-w Delete ("wipe") backward to beginning of line.
CTRL-y Retrieve ("yank") last deleted item.
CTRL-c Delete entire line.

In addition, emacs mode maintains a history file that enables you to recall previous commands. The commands in Table 11.2 are the most important of those that let you navigate the history file.

Table 11.2: Korn Shell Emacs Mode History Commands
Command Function
CTRL-p Go to previous command.
CTRL-n Go to next command.
CTRL-rstring Search backward for command containing string .

The first of these is the most useful by far - it's the "I made a mistake, so I'll go back and fix it" key. The search capability lets you bring back specific commands you may have typed awhile ago without having to go through the history file line by line with CTRL-p. Just enter CTRL-r followed by a search string and RETURN, and the Korn shell will bring back the most recent command that contains the search string (or beep at you if it finds no match). Assuming it is the command you want, you would then press RETURN again to run the command. If you begin your search string with a caret (^ ), it will only match commands that begin with the search string; this will be familiar behavior to users of such UNIX tools as grep , sed , and awk .

Another extremely useful feature of emacs mode is the filename completion facility, which should be familiar to C shell experts as well as Emacs users (see article 9.8 ). Emacs mode supports two completion commands, the most useful of which is ESC ESC (the Escape key pressed twice). If you type in a word and press ESC ESC, the shell will search for a filename that begins with what you typed and try to finish it. If there is only one filename that begins with your word, the shell will complete the name. If there is more than one, it will only complete out as far as it can without having to make a choice.

For example, if your directory contained the file program.c and you typed pro followed by ESC ESC, the shell would automatically complete the filename and leave you an extra space. But if your directory also contained the file program.o , the shell would only complete out to program. and let you complete the filename yourself.

[Another way to do history editing is with your own editor: the fc command (11.14 ) . -JP  ]

- BR


Previous: 11.12 Pass History to Another Shell UNIX Power Tools Next: 11.14 More Ways to Do Interactive History Editing
11.12 Pass History to Another Shell Book Index 11.14 More Ways to Do Interactive History Editing

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