hsuser.texi 18 KB

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