|
|
BSD 4.3tahoe
.TI CSHELL/HISTORY Reusing Previous Commands and Command Parts [DRAFT] History substitution allows you to include special, easily-typed constructs within a command and have the C shell replace them with parts of previous command lines, or events, possibly with small changes. Previous command lines are called events instead of commands in order to emphasize that events contain all the special constructs you entered originally (except history constructs). The history feature makes it easy for you to redo a previous command, to redo a previous command with a correction, and to re-use previous command parts. 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 I will postpone a systematic approach until the end. Setting the shell's prompt is such a special and simple case that I 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 .IP set prompt = "\\! Command Me O Great One: " .LP Note that ! can only be prevented its special meaning by placing a \\ before it; no other kind of quoting will work. History for the Lazy Typist A pre-requisite for gaining access to events (previous command lines) prior to the very last one is to tell the C shell how many events to keep track of. Do this by setting the C shell variable "history" to a number greater than one. Thus the command .IP set history = 20 .LP 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 .IP \fI^oldstring^newstring\fP .LP It works something like the substitute command in the editor, but with the restriction that \fIoldstring\fP be entirely contained in a command word and that none of the special characters significant to the editor have any meaning here. For example, .IP .nf % nroff -ms -Tdtc intro chap1 chap2 Cannot open intro % ^tro^troduction nroff -ms -Tdtc introduction chap1 chap2 .LP corrects a mistaken file name (the shell printed the resulting command before executing it), but the following fails: .IP .nf % ^o c^oduction c Modifier failed .LP As with the editor, substitutions of this type occur at the first possible point in the command line: .IP .nf % ^ro^roduction nroductionff -ms -Tdtc intro chap1 chap2 nroductionff: Command not found. .LP 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. .LP .nf .ta 3.5i % 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 .LP A previous event (command) can be redone without changes by using the !\fIevent\fP construct, where you replace \fIevent\fP 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. .LP .nf % 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 .LP Previous commands may also be specified after ! by using an event number, a numerical offset from the current event, or with the ?\fIstring\fP construct, which finds the last event containing \fIstring\fP 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 \fB:s^\fIoldstring\fB^\fInewstring\fR construct after the \fB!\fIevent\fR event specifier. Moreover, the change can be made throughout the event by putting a \fBg\fR before the \fBs\fR. If the history list contained .IP .nf 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 .LP the following commands could exercise these ideas as shown: .IP .nf % !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 .LP This concludes the section describing those history features relevant to most normal practices. History for Masochists Any history construct whatever is a special case, very often abbreviated, of the following form for the general history construct: .IP .BI ! event-specifier : word-specifier : modifier : modifier ... [to be continued ... jak]
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.