Commit | Line | Data |
---|---|---|
7be570e7 JM |
1 | @ignore |
2 | This file documents the user interface to the GNU History library. | |
3 | ||
5bdf8622 | 4 | Copyright (C) 1988-2002 Free Software Foundation, Inc. |
7be570e7 JM |
5 | Authored by Brian Fox and Chet Ramey. |
6 | ||
7 | Permission is granted to make and distribute verbatim copies of this manual | |
8 | provided the copyright notice and this permission notice are preserved on | |
9 | all copies. | |
10 | ||
11 | Permission is granted to process this file through Tex and print the | |
12 | results, provided the printed document carries copying permission notice | |
13 | identical to this one except for the removal of this paragraph (this | |
14 | paragraph not being relevant to the printed manual). | |
15 | ||
16 | Permission is granted to copy and distribute modified versions of this | |
17 | manual under the conditions for verbatim copying, provided also that the | |
18 | GNU Copyright statement is available to the distributee, and provided that | |
19 | the entire resulting derived work is distributed under the terms of a | |
20 | permission notice identical to this one. | |
21 | ||
22 | Permission is granted to copy and distribute translations of this manual | |
23 | into another language, under the above conditions for modified versions. | |
24 | @end ignore | |
25 | ||
26 | @node Using History Interactively | |
27 | @chapter Using History Interactively | |
28 | ||
1b17e766 EZ |
29 | @c @ifclear BashFeatures |
30 | @c @defcodeindex bt | |
31 | @c @end ifclear | |
32 | ||
7be570e7 | 33 | @ifset BashFeatures |
1b17e766 EZ |
34 | This chapter describes how to use the @sc{gnu} History Library |
35 | interactively, from a user's standpoint. | |
36 | It should be considered a user's guide. | |
37 | For information on using the @sc{gnu} History Library in other programs, | |
38 | see the @sc{gnu} Readline Library Manual. | |
7be570e7 JM |
39 | @end ifset |
40 | @ifclear BashFeatures | |
5bdf8622 | 41 | This chapter describes how to use the @sc{gnu} History Library interactively, |
1b17e766 | 42 | from a user's standpoint. It should be considered a user's guide. |
5bdf8622 DJ |
43 | For information on using the @sc{gnu} History Library in other programs, |
44 | see the @sc{gnu} Readline Library Manual. | |
7be570e7 JM |
45 | @end ifclear |
46 | ||
47 | @ifset BashFeatures | |
48 | @menu | |
49 | * Bash History Facilities:: How Bash lets you manipulate your command | |
50 | history. | |
51 | * Bash History Builtins:: The Bash builtin commands that manipulate | |
52 | the command history. | |
53 | * History Interaction:: What it feels like using History as a user. | |
54 | @end menu | |
55 | @end ifset | |
56 | @ifclear BashFeatures | |
57 | @menu | |
58 | * History Interaction:: What it feels like using History as a user. | |
59 | @end menu | |
60 | @end ifclear | |
61 | ||
62 | @ifset BashFeatures | |
63 | @node Bash History Facilities | |
64 | @section Bash History Facilities | |
65 | @cindex command history | |
66 | @cindex history list | |
67 | ||
5bdf8622 | 68 | When the @option{-o history} option to the @code{set} builtin |
7be570e7 | 69 | is enabled (@pxref{The Set Builtin}), |
5bdf8622 | 70 | the shell provides access to the @dfn{command history}, |
1b17e766 | 71 | the list of commands previously typed. |
5bdf8622 | 72 | The value of the @env{HISTSIZE} shell variable is used as the |
1b17e766 | 73 | number of commands to save in a history list. |
5bdf8622 | 74 | The text of the last @env{$HISTSIZE} |
1b17e766 EZ |
75 | commands (default 500) is saved. |
76 | The shell stores each command in the history list prior to | |
77 | parameter and variable expansion | |
7be570e7 JM |
78 | but after history expansion is performed, subject to the |
79 | values of the shell variables | |
5bdf8622 | 80 | @env{HISTIGNORE} and @env{HISTCONTROL}. |
1b17e766 | 81 | |
7be570e7 | 82 | When the shell starts up, the history is initialized from the |
5bdf8622 DJ |
83 | file named by the @env{HISTFILE} variable (default @file{~/.bash_history}). |
84 | The file named by the value of @env{HISTFILE} is truncated, if | |
1b17e766 | 85 | necessary, to contain no more than the number of lines specified by |
5bdf8622 | 86 | the value of the @env{HISTFILESIZE} variable. |
1b17e766 | 87 | When an interactive shell exits, the last |
5bdf8622 DJ |
88 | @env{$HISTSIZE} lines are copied from the history list to the file |
89 | named by @env{$HISTFILE}. | |
7be570e7 JM |
90 | If the @code{histappend} shell option is set (@pxref{Bash Builtins}), |
91 | the lines are appended to the history file, | |
92 | otherwise the history file is overwritten. | |
5bdf8622 | 93 | If @env{HISTFILE} |
7be570e7 JM |
94 | is unset, or if the history file is unwritable, the history is |
95 | not saved. After saving the history, the history file is truncated | |
5bdf8622 DJ |
96 | to contain no more than @env{$HISTFILESIZE} |
97 | lines. If @env{HISTFILESIZE} is not set, no truncation is performed. | |
98 | ||
99 | If the @env{HISTTIMEFORMAT} is set, the time stamp information | |
100 | associated with each history entry is written to the history file. | |
7be570e7 JM |
101 | |
102 | The builtin command @code{fc} may be used to list or edit and re-execute | |
103 | a portion of the history list. | |
1b17e766 | 104 | The @code{history} builtin may be used to display or modify the history |
7be570e7 | 105 | list and manipulate the history file. |
1b17e766 | 106 | When using command-line editing, search commands |
7be570e7 | 107 | are available in each editing mode that provide access to the |
1b17e766 | 108 | history list (@pxref{Commands For History}). |
7be570e7 JM |
109 | |
110 | The shell allows control over which commands are saved on the history | |
5bdf8622 | 111 | list. The @env{HISTCONTROL} and @env{HISTIGNORE} |
7be570e7 JM |
112 | variables may be set to cause the shell to save only a subset of the |
113 | commands entered. | |
114 | The @code{cmdhist} | |
115 | shell option, if enabled, causes the shell to attempt to save each | |
116 | line of a multi-line command in the same history entry, adding | |
117 | semicolons where necessary to preserve syntactic correctness. | |
118 | The @code{lithist} | |
119 | shell option causes the shell to save the command with embedded newlines | |
120 | instead of semicolons. | |
1b17e766 | 121 | The @code{shopt} builtin is used to set these options. |
7be570e7 JM |
122 | @xref{Bash Builtins}, for a description of @code{shopt}. |
123 | ||
124 | @node Bash History Builtins | |
125 | @section Bash History Builtins | |
126 | @cindex history builtins | |
127 | ||
1b17e766 | 128 | Bash provides two builtin commands which manipulate the |
7be570e7 JM |
129 | history list and history file. |
130 | ||
131 | @table @code | |
132 | ||
133 | @item fc | |
1b17e766 | 134 | @btindex fc |
7be570e7 JM |
135 | @example |
136 | @code{fc [-e @var{ename}] [-nlr] [@var{first}] [@var{last}]} | |
137 | @code{fc -s [@var{pat}=@var{rep}] [@var{command}]} | |
138 | @end example | |
139 | ||
140 | Fix Command. In the first form, a range of commands from @var{first} to | |
141 | @var{last} is selected from the history list. Both @var{first} and | |
142 | @var{last} may be specified as a string (to locate the most recent | |
143 | command beginning with that string) or as a number (an index into the | |
144 | history list, where a negative number is used as an offset from the | |
145 | current command number). If @var{last} is not specified it is set to | |
146 | @var{first}. If @var{first} is not specified it is set to the previous | |
5bdf8622 DJ |
147 | command for editing and @minus{}16 for listing. If the @option{-l} flag is |
148 | given, the commands are listed on standard output. The @option{-n} flag | |
149 | suppresses the command numbers when listing. The @option{-r} flag | |
7be570e7 JM |
150 | reverses the order of the listing. Otherwise, the editor given by |
151 | @var{ename} is invoked on a file containing those commands. If | |
152 | @var{ename} is not given, the value of the following variable expansion | |
153 | is used: @code{$@{FCEDIT:-$@{EDITOR:-vi@}@}}. This says to use the | |
5bdf8622 DJ |
154 | value of the @env{FCEDIT} variable if set, or the value of the |
155 | @env{EDITOR} variable if that is set, or @code{vi} if neither is set. | |
7be570e7 JM |
156 | When editing is complete, the edited commands are echoed and executed. |
157 | ||
158 | In the second form, @var{command} is re-executed after each instance | |
159 | of @var{pat} in the selected command is replaced by @var{rep}. | |
160 | ||
161 | A useful alias to use with the @code{fc} command is @code{r='fc -s'}, so | |
162 | that typing @samp{r cc} runs the last command beginning with @code{cc} | |
163 | and typing @samp{r} re-executes the last command (@pxref{Aliases}). | |
164 | ||
165 | @item history | |
1b17e766 | 166 | @btindex history |
7be570e7 | 167 | @example |
1b17e766 EZ |
168 | history [@var{n}] |
169 | history -c | |
170 | history -d @var{offset} | |
7be570e7 JM |
171 | history [-anrw] [@var{filename}] |
172 | history -ps @var{arg} | |
173 | @end example | |
174 | ||
1b17e766 | 175 | With no options, display the history list with line numbers. |
5bdf8622 | 176 | Lines prefixed with a @samp{*} have been modified. |
1b17e766 | 177 | An argument of @var{n} lists only the last @var{n} lines. |
5bdf8622 DJ |
178 | If the shell variable @env{HISTTIMEFORMAT} is set and not null, |
179 | it is used as a format string for @var{strftime} to display | |
180 | the time stamp associated with each displayed history entry. | |
181 | No intervening blank is printed between the formatted time stamp | |
182 | and the history line. | |
183 | ||
1b17e766 | 184 | Options, if supplied, have the following meanings: |
7be570e7 JM |
185 | |
186 | @table @code | |
1b17e766 EZ |
187 | @item -c |
188 | Clear the history list. This may be combined | |
189 | with the other options to replace the history list completely. | |
7be570e7 | 190 | |
1b17e766 EZ |
191 | @item -d @var{offset} |
192 | Delete the history entry at position @var{offset}. | |
193 | @var{offset} should be specified as it appears when the history is | |
194 | displayed. | |
7be570e7 JM |
195 | |
196 | @item -a | |
197 | Append the new | |
198 | history lines (history lines entered since the beginning of the | |
199 | current Bash session) to the history file. | |
200 | ||
201 | @item -n | |
202 | Append the history lines not already read from the history file | |
203 | to the current history list. These are lines appended to the history | |
204 | file since the beginning of the current Bash session. | |
205 | ||
1b17e766 EZ |
206 | @item -r |
207 | Read the current history file and append its contents to | |
208 | the history list. | |
7be570e7 | 209 | |
1b17e766 EZ |
210 | @item -w |
211 | Write out the current history to the history file. | |
7be570e7 JM |
212 | |
213 | @item -p | |
214 | Perform history substitution on the @var{arg}s and display the result | |
215 | on the standard output, without storing the results in the history list. | |
1b17e766 EZ |
216 | |
217 | @item -s | |
218 | The @var{arg}s are added to the end of | |
219 | the history list as a single entry. | |
220 | ||
7be570e7 JM |
221 | @end table |
222 | ||
5bdf8622 | 223 | When any of the @option{-w}, @option{-r}, @option{-a}, or @option{-n} options is |
7be570e7 JM |
224 | used, if @var{filename} |
225 | is given, then it is used as the history file. If not, then | |
5bdf8622 | 226 | the value of the @env{HISTFILE} variable is used. |
7be570e7 JM |
227 | |
228 | @end table | |
229 | @end ifset | |
230 | ||
231 | @node History Interaction | |
232 | @section History Expansion | |
233 | @cindex history expansion | |
234 | ||
235 | The History library provides a history expansion feature that is similar | |
236 | to the history expansion provided by @code{csh}. This section | |
237 | describes the syntax used to manipulate the history information. | |
238 | ||
239 | History expansions introduce words from the history list into | |
240 | the input stream, making it easy to repeat commands, insert the | |
241 | arguments to a previous command into the current input line, or | |
242 | fix errors in previous commands quickly. | |
243 | ||
244 | History expansion takes place in two parts. The first is to determine | |
245 | which line from the history list should be used during substitution. | |
246 | The second is to select portions of that line for inclusion into the | |
247 | current one. The line selected from the history is called the | |
248 | @dfn{event}, and the portions of that line that are acted upon are | |
249 | called @dfn{words}. Various @dfn{modifiers} are available to manipulate | |
250 | the selected words. The line is broken into words in the same fashion | |
251 | that Bash does, so that several words | |
252 | surrounded by quotes are considered one word. | |
253 | History expansions are introduced by the appearance of the | |
254 | history expansion character, which is @samp{!} by default. | |
255 | @ifset BashFeatures | |
256 | Only @samp{\} and @samp{'} may be used to escape the history expansion | |
257 | character. | |
258 | @end ifset | |
259 | ||
260 | @ifset BashFeatures | |
261 | Several shell options settable with the @code{shopt} | |
262 | builtin (@pxref{Bash Builtins}) may be used to tailor | |
263 | the behavior of history expansion. If the | |
264 | @code{histverify} shell option is enabled, and Readline | |
265 | is being used, history substitutions are not immediately passed to | |
266 | the shell parser. | |
267 | Instead, the expanded line is reloaded into the Readline | |
268 | editing buffer for further modification. | |
269 | If Readline is being used, and the @code{histreedit} | |
270 | shell option is enabled, a failed history expansion will be | |
271 | reloaded into the Readline editing buffer for correction. | |
5bdf8622 | 272 | The @option{-p} option to the @code{history} builtin command |
7be570e7 | 273 | may be used to see what a history expansion will do before using it. |
5bdf8622 | 274 | The @option{-s} option to the @code{history} builtin may be used to |
7be570e7 JM |
275 | add commands to the end of the history list without actually executing |
276 | them, so that they are available for subsequent recall. | |
277 | This is most useful in conjunction with Readline. | |
278 | ||
279 | The shell allows control of the various characters used by the | |
280 | history expansion mechanism with the @code{histchars} variable. | |
281 | @end ifset | |
282 | ||
283 | @menu | |
284 | * Event Designators:: How to specify which history line to use. | |
285 | * Word Designators:: Specifying which words are of interest. | |
286 | * Modifiers:: Modifying the results of substitution. | |
287 | @end menu | |
288 | ||
289 | @node Event Designators | |
290 | @subsection Event Designators | |
291 | @cindex event designators | |
292 | ||
293 | An event designator is a reference to a command line entry in the | |
294 | history list. | |
295 | @cindex history events | |
296 | ||
297 | @table @asis | |
298 | ||
299 | @item @code{!} | |
5bdf8622 | 300 | @ifset BashFeatures |
7be570e7 | 301 | Start a history substitution, except when followed by a space, tab, |
5bdf8622 DJ |
302 | the end of the line, @samp{=} or @samp{(} (when the |
303 | @code{extglob} shell option is enabled using the @code{shopt} builtin). | |
304 | @end ifset | |
305 | @ifclear BashFeatures | |
306 | Start a history substitution, except when followed by a space, tab, | |
307 | the end of the line, or @samp{=}. | |
308 | @end ifclear | |
7be570e7 JM |
309 | |
310 | @item @code{!@var{n}} | |
311 | Refer to command line @var{n}. | |
312 | ||
313 | @item @code{!-@var{n}} | |
314 | Refer to the command @var{n} lines back. | |
315 | ||
316 | @item @code{!!} | |
317 | Refer to the previous command. This is a synonym for @samp{!-1}. | |
318 | ||
319 | @item @code{!@var{string}} | |
320 | Refer to the most recent command starting with @var{string}. | |
321 | ||
322 | @item @code{!?@var{string}[?]} | |
323 | Refer to the most recent command containing @var{string}. The trailing | |
324 | @samp{?} may be omitted if the @var{string} is followed immediately by | |
325 | a newline. | |
326 | ||
327 | @item @code{^@var{string1}^@var{string2}^} | |
328 | Quick Substitution. Repeat the last command, replacing @var{string1} | |
329 | with @var{string2}. Equivalent to | |
330 | @code{!!:s/@var{string1}/@var{string2}/}. | |
331 | ||
332 | @item @code{!#} | |
333 | The entire command line typed so far. | |
334 | ||
335 | @end table | |
336 | ||
337 | @node Word Designators | |
338 | @subsection Word Designators | |
339 | ||
340 | Word designators are used to select desired words from the event. | |
341 | A @samp{:} separates the event specification from the word designator. It | |
342 | may be omitted if the word designator begins with a @samp{^}, @samp{$}, | |
343 | @samp{*}, @samp{-}, or @samp{%}. Words are numbered from the beginning | |
344 | of the line, with the first word being denoted by 0 (zero). Words are | |
345 | inserted into the current line separated by single spaces. | |
346 | ||
1b17e766 EZ |
347 | @need 0.75 |
348 | For example, | |
349 | ||
350 | @table @code | |
351 | @item !! | |
352 | designates the preceding command. When you type this, the preceding | |
353 | command is repeated in toto. | |
354 | ||
355 | @item !!:$ | |
356 | designates the last argument of the preceding command. This may be | |
357 | shortened to @code{!$}. | |
358 | ||
359 | @item !fi:2 | |
360 | designates the second argument of the most recent command starting with | |
361 | the letters @code{fi}. | |
362 | @end table | |
363 | ||
364 | @need 0.75 | |
365 | Here are the word designators: | |
366 | ||
7be570e7 JM |
367 | @table @code |
368 | ||
369 | @item 0 (zero) | |
370 | The @code{0}th word. For many applications, this is the command word. | |
371 | ||
372 | @item @var{n} | |
373 | The @var{n}th word. | |
374 | ||
375 | @item ^ | |
376 | The first argument; that is, word 1. | |
377 | ||
378 | @item $ | |
379 | The last argument. | |
380 | ||
381 | @item % | |
382 | The word matched by the most recent @samp{?@var{string}?} search. | |
383 | ||
384 | @item @var{x}-@var{y} | |
385 | A range of words; @samp{-@var{y}} abbreviates @samp{0-@var{y}}. | |
386 | ||
387 | @item * | |
388 | All of the words, except the @code{0}th. This is a synonym for @samp{1-$}. | |
389 | It is not an error to use @samp{*} if there is just one word in the event; | |
390 | the empty string is returned in that case. | |
391 | ||
392 | @item @var{x}* | |
393 | Abbreviates @samp{@var{x}-$} | |
394 | ||
395 | @item @var{x}- | |
396 | Abbreviates @samp{@var{x}-$} like @samp{@var{x}*}, but omits the last word. | |
397 | ||
398 | @end table | |
399 | ||
400 | If a word designator is supplied without an event specification, the | |
401 | previous command is used as the event. | |
402 | ||
403 | @node Modifiers | |
404 | @subsection Modifiers | |
405 | ||
406 | After the optional word designator, you can add a sequence of one or more | |
407 | of the following modifiers, each preceded by a @samp{:}. | |
408 | ||
409 | @table @code | |
410 | ||
411 | @item h | |
412 | Remove a trailing pathname component, leaving only the head. | |
413 | ||
414 | @item t | |
415 | Remove all leading pathname components, leaving the tail. | |
416 | ||
417 | @item r | |
418 | Remove a trailing suffix of the form @samp{.@var{suffix}}, leaving | |
419 | the basename. | |
420 | ||
421 | @item e | |
422 | Remove all but the trailing suffix. | |
423 | ||
424 | @item p | |
425 | Print the new command but do not execute it. | |
426 | ||
427 | @ifset BashFeatures | |
428 | @item q | |
429 | Quote the substituted words, escaping further substitutions. | |
430 | ||
431 | @item x | |
432 | Quote the substituted words as with @samp{q}, | |
433 | but break into words at spaces, tabs, and newlines. | |
434 | @end ifset | |
435 | ||
436 | @item s/@var{old}/@var{new}/ | |
437 | Substitute @var{new} for the first occurrence of @var{old} in the | |
438 | event line. Any delimiter may be used in place of @samp{/}. | |
439 | The delimiter may be quoted in @var{old} and @var{new} | |
440 | with a single backslash. If @samp{&} appears in @var{new}, | |
441 | it is replaced by @var{old}. A single backslash will quote | |
442 | the @samp{&}. The final delimiter is optional if it is the last | |
443 | character on the input line. | |
444 | ||
445 | @item & | |
446 | Repeat the previous substitution. | |
447 | ||
448 | @item g | |
5bdf8622 | 449 | @itemx a |
7be570e7 JM |
450 | Cause changes to be applied over the entire event line. Used in |
451 | conjunction with @samp{s}, as in @code{gs/@var{old}/@var{new}/}, | |
452 | or with @samp{&}. | |
453 | ||
5bdf8622 DJ |
454 | @item G |
455 | Apply the following @samp{s} modifier once to each word in the event. | |
456 | ||
7be570e7 | 457 | @end table |