Although these interactive applications predominate, there are two non-interactive uses: defining the arguments to shell aliases and setting the shell's prompt to contain the current event number. To use alias arguments extensively requires the thorough grounding in history subtleties that only a systematic approach can give, and as the mechanisms involved are very complex and not particularly powerful we will not include a full systematic approach.
Setting the shell's prompt is such a special and simple case that we will deal with it now. If you set your prompt (usually "% ") to any string containing ! (which you must quote using \!), the shell will replace the ! with the current event number when it prints the prompt. For instance, to set your prompt to contain the event number followed by a string of your choosing, you might do
set prompt = "\! Command Me O Great One: "
Note that ! can only be prevented its special meaning by placing a \
before it; no other kind of quoting will work.
set history = 20
will cause the shell to keep a record of your previous 20 events in an
internal list called the history list. You can display the current
history list at any time by typing the command "history". If you want to
the history list to be saved when you logout, and then be reinstated when
you login next, set the variable "savehist" to the same number. To have
the these variables set automatically upon logging-in, enter the
appropriate commands into your ".cshrc" file.All history substitutions are triggered on a command line when the user enters special constructs using the characters ! and ^ (circumflex). Whenever the shell makes a history substitution, it always displays the resulting command as you would have entered it without using the history feature. So if the result is other than you expected, you have a chance to interrupt the command before it does harm.
The ^ has only one purpose, and that is to re-do the last command with a correction using a command of the form
^oldstring^newstring
It works something like the substitute command in the editor, but
with the restriction that oldstring be entirely contained in a
command word and that none of the special characters significant
to the editor have any meaning here. For example,
% nroff -ms -Tdtc intro chap1 chap2
Cannot open intro
% ^tro^troduction
nroff -ms -Tdtc introduction chap1 chap2
corrects a mistaken file name (the shell printed the resulting command
before executing it), but the following fails:
% ^o c^oduction c
Modifier failed
As with the editor, substitutions of this type occur at the first possible
point in the command line:
% ^ro^roduction
nroductionff -ms -Tdtc intro chap1 chap2
nroductionff: Command not found.
The restrictions on this construct make some kinds of corrections
impossible using history.Another very useful aspect of history is the ability to re-use the last word of the last command using the !$ construct. Typically, a command takes a filename as the last argument, and the next command will likely include the filename again as the last argument, and the next, etc. The command sequence below demonstrates this labor-saving device.
% vi chapter15.4.new % myCprogram
% spell !$ % vi !$.c
spell chapter15.4.new vi myCprogram.c
% nroff -ms !$ > out % cc !$
nroff -ms chapter15.4.new > out cc myCprogram.c
% more !$ % pr !$ | lpr
more out pr myCprogram.c | lpr
A previous event (command) can be redone without changes by using the
!event construct, where you replace event with the first few letters of a
previous event. Typically, useful work during a terminal session consists
of refining a project with the editor, testing it out, refining some more,
then testing some more, etc., and often this means revisiting the same
commands time and again. Here is a sample terminal session illustrating
how you can take advantage of such behavior.
% more mytext % !v
mytext: No such file or directory vi mytext
% cd confidential % !s
% !mor spell mytext
more mytext % nroff-ms !$ | more
mytext: Permission denied nroff-ms mytext
% chmod 644 !$ nroff-ms: Command not found.
chmod 644 mytext % ^ff-^ff -
% !m nroff -ms mytext | more
more mytext % !v
% vi !$ vi mytext
vi mytext % !n
% spell !$ nroff -ms mytext | more
spell mytext % logout
Previous commands may also be specified after ! by using an event number,
a numerical offset from the current event, or with the ?string construct,
which finds the last event containing string not necessarily at the
beginning. You can find out event numbers with the "history" command, and
just type, say !14, to redo event number 14. A numerical offset such as
!-2 will redo the next to last event. As a special case, !! redoes the
last event.Sometimes you want to redo a previous command with one of the above constructs, but only after making a change. This is possible by placing a :s^oldstring^newstring construct after the !event event specifier. Moreover, the change can be made throughout the event by putting a g before the s. If the history list contained
53 more /usr/man/man1/csh.1
54 readnews -n net.general net.micro net.announce
55 nroff -ms -Tlpr mytext | lpr
56 nroff -ms paper > outfile
57 cp chap1 chap1.old
the following commands could exercise these ideas as shown:
% !54
readnews -n net.general net.micro net.announce
% !nro
nroff -ms paper > outfile
% !?-Tlpr
nroff -ms -Tlpr mytext | lpr
% !more:s^man1^cat1
more /usr/man/cat1/csh.1
% !-2:s^mytext^paper
nroff -ms -Tlpr paper | lpr
% !cp:gs^chap1^chap2
cp chap2 chap2.old