Command-line editing is a very popular shell feature. It was introduced in tcsh (csh does not support command-line editing) and carried over to the Korn shell and bash. Command-line editing lets you use a special version of either the vi or emacs text editor to edit your current command line, or any of the commands in your history list. On most systems, command-line editing is enabled by default, although you may choose to switch editors. Chapter 5 compares vi and emacs and describes how to use each of them.
The command-line editor shows you a one-line “window” on your command history, starting with your current command. You can use the up/down arrow keys to move backward and forward in your history Once you edit a line, you can execute it by pressing ENTER.
The command-line editing features greatly enhance the value of the history list. You can use them to correct command-line errors and to modify previous commands. Command-line editing also makes it much easier to search through your command history list, because you can use the same search commands you use in vi or emacs.
Suppose you want to search your command history for your most recent use of the file project.backup. If your command-line editor is set to emacs, you can search by typing CTRL-R followed by the filename. As soon as you enter part of the string, emacs will begin to search your history
$ [CTRL] R (reverse-i-search)'pr': lpr directions
To search further back in your history list, type CTRL-R again.
To perform the same search with the vi editor, you would type ESC followed by a / (slash) and the beginning of the filename, as shown.
$ ESC /proj
When you hit ENTER, the editor will search for the most recent command in your history list that contains the string “proj”. To find an earlier command containing that string, type “n” to repeat the search.
Table 4–5 shows the most useful commands for line editing. Note that the vi command-line editor begins in input mode. To use the vi commands, you must enter command mode by pressing ESC. You can use the emacs commands at any time. For this reason, some users who normally prefer vi use emacs as their line editor.
Movement Commands | vi | emacs |
---|---|---|
One character left | h | CTRL-B |
One character right | l | CTRL-F |
One word left | b | ESC-B |
One word right | w | ESC-F |
Beginning of line | A | CTRL-A |
End of line | $ | CTRL-E |
Back up one entry in history list | k | CTRL-P |
Search for string xxx in history list | /xxx | CTRL-R XXX |
Editing Commands | vi | emacs |
Delete current character | X | CTRL-D |
Delete current word | dw | ESC-D |
Delete line | dd | (kill char) |
Change word | cw | |
Append text | a | |
Insert text | i |
To enable command-line editing in bash or ksh, use
$ set -o vi
to turn on vi-style editing, or
$ set -o emacs
to enable the emacs line editor. In ksh, if you do not set either of these options, the shell will try to use the editor specified by the variable VISUAL.
Since command-line editing is such a useful feature, you may want to add this setting to your .kshrc or .bashrc file.
The bindkey command in tcsh determines whether it uses emacs or vi for command-line editing, as shown:
bindkey -e # use emacs-style editing bindkey -v # use vi-style editing
You may want to add one of these settings to your .tcshrc file.