|
|
1.1 root 1: .TI CSHELL/HISTORY
2: Reusing Previous Commands and Command Parts [DRAFT]
3:
4:
5: History substitution allows you to include special, easily-typed
6: constructs within a command and have the C shell replace them
7: with parts of previous command lines, or events,
8: possibly with small changes.
9: Previous command lines are called events instead of commands
10: in order to emphasize that events contain all the special constructs
11: you entered originally (except history constructs).
12: The history feature makes it easy for you to redo a previous command,
13: to redo a previous command with a correction, and to re-use
14: previous command parts.
15:
16: Although these interactive applications predominate,
17: there are two non-interactive uses: defining the arguments to shell
18: aliases and setting the shell's prompt to contain the current event number.
19: To use alias arguments extensively requires the thorough grounding
20: in history subtleties that only a systematic approach can give,
21: and as the mechanisms involved are very complex and not particularly
22: powerful I will postpone a systematic approach until the end.
23:
24: Setting the shell's prompt is such a special and simple case that I will
25: deal with it now.
26: If you set your prompt (usually "% ") to any string containing !
27: (which you must quote using \\!),
28: the shell will replace the ! with the current event number when it
29: prints the prompt.
30: For instance, to set your prompt to contain the event number
31: followed by a string of your choosing, you might do
32: .IP
33: set prompt = "\\! Command Me O Great One: "
34: .LP
35: Note that ! can only be prevented its special meaning by placing a \\
36: before it; no other kind of quoting will work.
37:
38:
39: History for the Lazy Typist
40:
41: A pre-requisite for gaining access to events (previous command lines)
42: prior to the very last one is to tell the C shell how many events
43: to keep track of.
44: Do this by setting the C shell variable "history" to a
45: number greater than one.
46: Thus the command
47: .IP
48: set history = 20
49: .LP
50: will cause the shell to keep a record of your previous 20 events
51: in an internal list called the history list.
52: You can display the current history list at any time
53: by typing the command "history".
54: If you want to the history list to be saved when you logout,
55: and then be reinstated when you login next,
56: set the variable "savehist" to the same number.
57: To have the these variables set automatically upon logging-in,
58: enter the appropriate commands into your ".cshrc" file.
59:
60: All history substitutions are triggered on a command line
61: when the user enters special constructs using the
62: characters ! and ^ (circumflex).
63: Whenever the shell makes a history substitution, it always
64: displays the resulting command as you would have entered it
65: without using the history feature.
66: So if the result is other than you expected,
67: you have a chance to interrupt the command before it does harm.
68:
69: The ^ has only one purpose, and that is to re-do the last command
70: with a correction using a command of the form
71: .IP
72: \fI^oldstring^newstring\fP
73: .LP
74: It works something like the substitute command in the editor,
75: but with the restriction that \fIoldstring\fP
76: be entirely contained in a command word and that none of the
77: special characters significant to the editor have any meaning here.
78: For example,
79: .IP
80: .nf
81: % nroff -ms -Tdtc intro chap1 chap2
82: Cannot open intro
83: % ^tro^troduction
84: nroff -ms -Tdtc introduction chap1 chap2
85: .LP
86: corrects a mistaken file name (the shell printed the resulting command
87: before executing it), but the following fails:
88: .IP
89: .nf
90: % ^o c^oduction c
91: Modifier failed
92: .LP
93: As with the editor, substitutions of this type occur at the
94: first possible point in the command line:
95: .IP
96: .nf
97: % ^ro^roduction
98: nroductionff -ms -Tdtc intro chap1 chap2
99: nroductionff: Command not found.
100: .LP
101: The restrictions on this construct make some kinds of corrections
102: impossible using history.
103:
104: Another very useful aspect of history is the ability to re-use the
105: last word of the last command using the !$ construct.
106: Typically, a command takes a filename as the last argument,
107: and the next command will likely include the filename again
108: as the last argument, and the next, etc.
109: The command sequence below demonstrates this labor-saving device.
110: .LP
111: .nf
112: .ta 3.5i
113: % vi chapter15.4.new % myCprogram
114: % spell !$ % vi !$.c
115: spell chapter15.4.new vi myCprogram.c
116: % nroff -ms !$ > out % cc !$
117: nroff -ms chapter15.4.new > out cc myCprogram.c
118: % more !$ % pr !$ | lpr
119: more out pr myCprogram.c | lpr
120: .LP
121: A previous event (command) can be redone without changes by
122: using the !\fIevent\fP construct, where you replace
123: \fIevent\fP with the first few letters of a previous event.
124: Typically, useful work during a terminal session consists of
125: refining a project with the editor, testing it out, refining some more,
126: then testing some more, etc., and often this means revisiting the same
127: commands time and again.
128: Here is a sample terminal session illustrating how you can take advantage
129: of such behavior.
130: .LP
131: .nf
132: % more mytext % !v
133: mytext: No such file or directory vi mytext
134: % cd confidential % !s
135: % !mor spell mytext
136: more mytext % nroff-ms !$ | more
137: mytext: Permission denied nroff-ms mytext
138: % chmod 644 !$ nroff-ms: Command not found.
139: chmod 644 mytext % ^ff-^ff -
140: % !m nroff -ms mytext | more
141: more mytext % !v
142: % vi !$ vi mytext
143: vi mytext % !n
144: % spell !$ nroff -ms mytext | more
145: spell mytext % logout
146: .LP
147: Previous commands may also be specified after ! by using an event
148: number, a numerical offset from the current event, or with the
149: ?\fIstring\fP construct, which finds the last event containing
150: \fIstring\fP not necessarily at the beginning.
151: You can find out event numbers with the "history" command, and
152: just type, say !14, to redo event number 14.
153: A numerical offset such as !-2 will redo the next to last event.
154: As a special case, !! redoes the last event.
155:
156: Sometimes you want to redo a previous command with one of the
157: above constructs, but only after making a change.
158: This is possible by placing a \fB:s^\fIoldstring\fB^\fInewstring\fR
159: construct after the \fB!\fIevent\fR event specifier.
160: Moreover, the change can be made throughout the event by
161: putting a \fBg\fR before the \fBs\fR.
162: If the history list contained
163: .IP
164: .nf
165: 53 more /usr/man/man1/csh.1
166: 54 readnews -n net.general net.micro net.announce
167: 55 nroff -ms -Tlpr mytext | lpr
168: 56 nroff -ms paper > outfile
169: 57 cp chap1 chap1.old
170: .LP
171: the following commands could exercise these ideas as shown:
172: .IP
173: .nf
174: % !54
175: readnews -n net.general net.micro net.announce
176: % !nro
177: nroff -ms paper > outfile
178: % !?-Tlpr
179: nroff -ms -Tlpr mytext | lpr
180: % !more:s^man1^cat1
181: more /usr/man/cat1/csh.1
182: % !-2:s^mytext^paper
183: nroff -ms -Tlpr paper | lpr
184: % !cp:gs^chap1^chap2
185: cp chap2 chap2.old
186: .LP
187: This concludes the section describing those history features
188: relevant to most normal practices.
189:
190:
191: History for Masochists
192:
193: Any history construct whatever is a special case, very often
194: abbreviated, of the following form for the general history construct:
195: .IP
196: .BI ! event-specifier : word-specifier : modifier : modifier ...
197:
198:
199: [to be continued ... jak]
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.