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