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