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