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