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