[Next] [Previous] [Up] [Top] [Contents] [Index]

Chapter 5: Important UNIX Concepts

5.3 Command Recall

Command recall is quite different in each shell. One common feature for all shells that support command recall is the history mechanism. It maintains a list of commands that have been entered and allows them to be reexecuted. The history variable, set to some number at login time in the start-up files, determines the number of commands that are saved in the list. The savehist variable specifies how many commands are to be saved for your next session after you log out. The history command displays the list of saved commands:

% history 

We discuss the following shells separately: csh, tcsh, and bash/ksh. There is no command recall facility for sh.

csh

There is no command line editing native to csh. Before describing the standard csh command recall facility, we should mention a Fermilab product called cedit that we recommend for use with csh instead. It was designed to mimic VMS line editing, and turns out to provide similar command recall and editing functionality to tcsh. To use cedit, you need to set it up initially. Enter:

% setup cedit

To execute it, type:

% m

followed by <Return>. m stands for modify. Use the up or down arrow keys to scroll to the desired command. The right and left arrow keys and your backspace key allow you to edit the command before reexecuting it. There are several control characters that perform functions within cedit. Typing <Ctrl-i> in cedit displays the available commands.

Recalling history commands using standard csh syntax is fairly easy. Use the commands listed below.

!!

Reexecute the previous command

!n

Reexecute command n from the history list

!text

Reexecute the most recent command beginning with text

!?text?

Reexecute the most recent command containing text

For example, to reexecute the 4th command from the history list, enter:

% !4

and to reexecute the last command starting with ls:

% !ls 

The dollar sign ($) can be used to recall the last word of a command. !$ causes substitution of the last word of the last command. For example, you can check the contents of myfile.f and then compile it using the following command sequence:

% less myfile.f
% f77 !$

A couple of nice features you can use with these reexecution commands are preview (p) and substitute (s). To substitute a string in the previous command and preview it before execution, use the syntax:

% !:p:s/oldstring/newstring

To do the same for the nth command in the history list, use:

% !n:p:s/oldstring/newstring

To execute after previewing (and/or substituting), simply type:

% !!

tcsh

Recalling commands is easy if you are using tcsh. The up/down arrows on the keyboard can be used to recall commands and the left/right arrows can be used to move around within the command to edit it (VMS users will be familiar with this concept).

A command line correction algorithm is available in tcsh. To enable it, enter:

% set correct=all

This causes all words on the command line to be checked. If any part gets corrected, the system notifies you, and gives you a chance to accept or reject it. For example, say you type in:

% lz /usr/bin

The system will return with:

CORRECT ls /usr/bin (y|n|e|a|)?

Where y=yes, n=no, e=edit, and a=abort. You must provide one of these responses.

To turn off command line correction, enter:

% set correct=none

ksh

Two styles of command recall are supported; emacs and vi. The style is determined in one of two ways:

When set to emacs, use the usual emacs commands to display and modify previous commands, for instance <Ctrl-p> for previous line. When set to vi, command recall is initiated by typing the Escape (or <Ctrl-[>) key. Then all the standard vi commands can be used. Some of the basic vi and emacs commands are listed in section 11.3.

bash

Both csh and ksh-style recall are supported.


UNIX at Fermilab - 10 Apr 1998

[Next] [Previous] [Up] [Top] [Contents] [Index]