DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Configuring and working with the shells

Recalling and editing previous commands

The Korn and C shells provide facilities for making it easier to enter commands:


C shell
Provides command history recall. The shell maintains a list of previously executed commands; you can refer back to entire command lines or sections of commands, and re-execute them if necessary.

Korn shell
Provides command history editing. In addition to remembering a list of previously executed commands, the Korn shell allows you to edit previous command lines interactively, using the keystrokes of the vi or emacs text editors.
Many people prefer the facilities offered by the Korn shell to those of the C shell because of the interactive editing feature.

Korn shell history editing

Every time you issue a command to the Korn shell, in addition to executing the command, the shell adds it to a list of previously executed commands.

To view the list of previously executed commands, issue the history command. This displays a number (up to the number set in $HISTSIZE) of previously issued commands. $HISTSIZE is set in .profile.

Before you can recall and edit the history list directly, you must issue the ksh command set -o vi to set the vi editing mode in the shell. Similarly, the following command line sets emacs editing mode in the shell:

set -o emacs

Either of these commands may be issued automatically by your .profile file. set -o on its own displays all the current ksh options; you may want to see what options are available.

To enable history editing permanently, add the following to your .profile file:

if [ -z "$VISUAL" -a -z "$EDITOR ]
then
set -o vi
fi

(This may also be found in your .kshrc file.) See ksh(C) for details of the VISUAL and EDITOR variables; see test(C) for an explanation of the [ ... ] notation and the options used in the example.

Once the vi option, for example, is available, you can edit your command history using the vi editing keys. The shell initially behaves like vi in text insertion mode; if you type a command it inserts text, and when you press <Enter> it executes the line you just typed. Here is a quick overview:

For example, if you want to reissue the last command you typed, simply press <Esc> (to switch to command mode), then ``k'' to move up to the previous command, then <Enter> to execute the command.

A full list of the available vi mode command editing keystrokes is given in the ksh(C) manual page. For an introduction to the vi editing keystrokes, see ``A quick tour of vi''.

Note that the contents of the history file are maintained across logouts and environment resettings. Accordingly, previously executed command lines are still available for editing or re-execution even after something like the following:

. $HOME/.profile

C shell history editing

The C shell does not let you edit previous commands interactively. However, you can recall entire command lines or portions of commands, and make changes to them.

To repeat the last command you typed, use the following command:

   % !!
The exclamation mark (!) tells csh to expect a history command, and the second exclamation mark specifically refers to the last command entered.

To display your command history, use the following command:

   % history
The C shell lists out the last few commands (the precise number being controlled by the history environment variable). Each command is listed with an event number. For example:
   9 vi chapter.6
   10 wc -l chapter.6
   11 l ch*
   12 diff chapter.6 chapter.6.old | more
   13 vi chapter.7
To recall a given event, type an exclamation mark followed by the event number; for example, to edit chapter.6 again, the command would be !9.

To alter a command, you can follow the event number with a colon (:) and a modifier which is applied to the word.

For example, to edit chapter.8 using the above history, you could issue the instruction !9:s/8/6/, which substitutes ``8'' for ``6''.

A full list of modifiers is provided in the csh(C) manual page.


Next topic: Using aliases
Previous topic: Adding a logout script

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005