hsuser.texi 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. @ignore
  2. This file documents the user interface to the GNU History library.
  3. Copyright (C) 1988--2016 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{Bash Builtins}, 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). If @var{last} is not specified it is set to
  133. @var{first}. If @var{first} is not specified it is set to the previous
  134. command for editing and @minus{}16 for listing. If the @option{-l} flag is
  135. given, the commands are listed on standard output. The @option{-n} flag
  136. suppresses the command numbers when listing. The @option{-r} flag
  137. reverses the order of the listing. Otherwise, the editor given by
  138. @var{ename} is invoked on a file containing those commands. If
  139. @var{ename} is not given, the value of the following variable expansion
  140. is used: @code{$@{FCEDIT:-$@{EDITOR:-vi@}@}}. This says to use the
  141. value of the @env{FCEDIT} variable if set, or the value of the
  142. @env{EDITOR} variable if that is set, or @code{vi} if neither is set.
  143. When editing is complete, the edited commands are echoed and executed.
  144. In the second form, @var{command} is re-executed after each instance
  145. of @var{pat} in the selected command is replaced by @var{rep}.
  146. @var{command} is intepreted the same as @var{first} above.
  147. A useful alias to use with the @code{fc} command is @code{r='fc -s'}, so
  148. that typing @samp{r cc} runs the last command beginning with @code{cc}
  149. and typing @samp{r} re-executes the last command (@pxref{Aliases}).
  150. @item history
  151. @btindex history
  152. @example
  153. history [@var{n}]
  154. history -c
  155. history -d @var{offset}
  156. history [-anrw] [@var{filename}]
  157. history -ps @var{arg}
  158. @end example
  159. With no options, display the history list with line numbers.
  160. Lines prefixed with a @samp{*} have been modified.
  161. An argument of @var{n} lists only the last @var{n} lines.
  162. If the shell variable @env{HISTTIMEFORMAT} is set and not null,
  163. it is used as a format string for @var{strftime} to display
  164. the time stamp associated with each displayed history entry.
  165. No intervening blank is printed between the formatted time stamp
  166. and the history line.
  167. Options, if supplied, have the following meanings:
  168. @table @code
  169. @item -c
  170. Clear the history list. This may be combined
  171. with the other options to replace the history list completely.
  172. @item -d @var{offset}
  173. Delete the history entry at position @var{offset}.
  174. @var{offset} should be specified as it appears when the history is
  175. displayed.
  176. @item -a
  177. Append the new history lines to the history file.
  178. These are history lines entered since the beginning of the current
  179. Bash session, but not already appended to the history file.
  180. @item -n
  181. Append the history lines not already read from the history file
  182. to the current history list. These are lines appended to the history
  183. file since the beginning of the current Bash session.
  184. @item -r
  185. Read the history file and append its contents to
  186. the history list.
  187. @item -w
  188. Write out the current history list to the history file.
  189. @item -p
  190. Perform history substitution on the @var{arg}s and display the result
  191. on the standard output, without storing the results in the history list.
  192. @item -s
  193. The @var{arg}s are added to the end of
  194. the history list as a single entry.
  195. @end table
  196. When any of the @option{-w}, @option{-r}, @option{-a}, or @option{-n} options is
  197. used, if @var{filename}
  198. is given, then it is used as the history file. If not, then
  199. the value of the @env{HISTFILE} variable is used.
  200. @end table
  201. @end ifset
  202. @node History Interaction
  203. @section History Expansion
  204. @cindex history expansion
  205. The History library provides a history expansion feature that is similar
  206. to the history expansion provided by @code{csh}. This section
  207. describes the syntax used to manipulate the history information.
  208. History expansions introduce words from the history list into
  209. the input stream, making it easy to repeat commands, insert the
  210. arguments to a previous command into the current input line, or
  211. fix errors in previous commands quickly.
  212. @ifset BashFeatures
  213. History expansion is performed immediately after a complete line
  214. is read, before the shell breaks it into words.
  215. @end ifset
  216. History expansion takes place in two parts. The first is to determine
  217. which line from the history list should be used during substitution.
  218. The second is to select portions of that line for inclusion into the
  219. current one. The line selected from the history is called the
  220. @dfn{event}, and the portions of that line that are acted upon are
  221. called @dfn{words}. Various @dfn{modifiers} are available to manipulate
  222. the selected words. The line is broken into words in the same fashion
  223. that Bash does, so that several words
  224. surrounded by quotes are considered one word.
  225. History expansions are introduced by the appearance of the
  226. history expansion character, which is @samp{!} by default.
  227. @ifset BashFeatures
  228. Only @samp{\} and @samp{'} may be used to escape the history expansion
  229. character, but the history expansion character is
  230. also treated as quoted if it immediately precedes the closing double quote
  231. in a double-quoted string.
  232. @end ifset
  233. @ifset BashFeatures
  234. Several shell options settable with the @code{shopt}
  235. builtin (@pxref{Bash Builtins}) may be used to tailor
  236. the behavior of history expansion. If the
  237. @code{histverify} shell option is enabled, and Readline
  238. is being used, history substitutions are not immediately passed to
  239. the shell parser.
  240. Instead, the expanded line is reloaded into the Readline
  241. editing buffer for further modification.
  242. If Readline is being used, and the @code{histreedit}
  243. shell option is enabled, a failed history expansion will be
  244. reloaded into the Readline editing buffer for correction.
  245. The @option{-p} option to the @code{history} builtin command
  246. may be used to see what a history expansion will do before using it.
  247. The @option{-s} option to the @code{history} builtin may be used to
  248. add commands to the end of the history list without actually executing
  249. them, so that they are available for subsequent recall.
  250. This is most useful in conjunction with Readline.
  251. The shell allows control of the various characters used by the
  252. history expansion mechanism with the @code{histchars} variable,
  253. as explained above (@pxref{Bash Variables}). The shell uses
  254. the history comment character to mark history timestamps when
  255. writing the history file.
  256. @end ifset
  257. @menu
  258. * Event Designators:: How to specify which history line to use.
  259. * Word Designators:: Specifying which words are of interest.
  260. * Modifiers:: Modifying the results of substitution.
  261. @end menu
  262. @node Event Designators
  263. @subsection Event Designators
  264. @cindex event designators
  265. An event designator is a reference to a command line entry in the
  266. history list.
  267. Unless the reference is absolute, events are relative to the current
  268. position in the history list.
  269. @cindex history events
  270. @table @asis
  271. @item @code{!}
  272. @ifset BashFeatures
  273. Start a history substitution, except when followed by a space, tab,
  274. the end of the line, @samp{=} or @samp{(} (when the
  275. @code{extglob} shell option is enabled using the @code{shopt} builtin).
  276. @end ifset
  277. @ifclear BashFeatures
  278. Start a history substitution, except when followed by a space, tab,
  279. the end of the line, or @samp{=}.
  280. @end ifclear
  281. @item @code{!@var{n}}
  282. Refer to command line @var{n}.
  283. @item @code{!-@var{n}}
  284. Refer to the command @var{n} lines back.
  285. @item @code{!!}
  286. Refer to the previous command. This is a synonym for @samp{!-1}.
  287. @item @code{!@var{string}}
  288. Refer to the most recent command
  289. preceding the current position in the history list
  290. starting with @var{string}.
  291. @item @code{!?@var{string}[?]}
  292. Refer to the most recent command
  293. preceding the current position in the history list
  294. containing @var{string}.
  295. The trailing
  296. @samp{?} may be omitted if the @var{string} is followed immediately by
  297. a newline.
  298. @item @code{^@var{string1}^@var{string2}^}
  299. Quick Substitution. Repeat the last command, replacing @var{string1}
  300. with @var{string2}. Equivalent to
  301. @code{!!:s/@var{string1}/@var{string2}/}.
  302. @item @code{!#}
  303. The entire command line typed so far.
  304. @end table
  305. @node Word Designators
  306. @subsection Word Designators
  307. Word designators are used to select desired words from the event.
  308. A @samp{:} separates the event specification from the word designator. It
  309. may be omitted if the word designator begins with a @samp{^}, @samp{$},
  310. @samp{*}, @samp{-}, or @samp{%}. Words are numbered from the beginning
  311. of the line, with the first word being denoted by 0 (zero). Words are
  312. inserted into the current line separated by single spaces.
  313. @need 0.75
  314. For example,
  315. @table @code
  316. @item !!
  317. designates the preceding command. When you type this, the preceding
  318. command is repeated in toto.
  319. @item !!:$
  320. designates the last argument of the preceding command. This may be
  321. shortened to @code{!$}.
  322. @item !fi:2
  323. designates the second argument of the most recent command starting with
  324. the letters @code{fi}.
  325. @end table
  326. @need 0.75
  327. Here are the word designators:
  328. @table @code
  329. @item 0 (zero)
  330. The @code{0}th word. For many applications, this is the command word.
  331. @item @var{n}
  332. The @var{n}th word.
  333. @item ^
  334. The first argument; that is, word 1.
  335. @item $
  336. The last argument.
  337. @item %
  338. The word matched by the most recent @samp{?@var{string}?} search.
  339. @item @var{x}-@var{y}
  340. A range of words; @samp{-@var{y}} abbreviates @samp{0-@var{y}}.
  341. @item *
  342. All of the words, except the @code{0}th. This is a synonym for @samp{1-$}.
  343. It is not an error to use @samp{*} if there is just one word in the event;
  344. the empty string is returned in that case.
  345. @item @var{x}*
  346. Abbreviates @samp{@var{x}-$}
  347. @item @var{x}-
  348. Abbreviates @samp{@var{x}-$} like @samp{@var{x}*}, but omits the last word.
  349. @end table
  350. If a word designator is supplied without an event specification, the
  351. previous command is used as the event.
  352. @node Modifiers
  353. @subsection Modifiers
  354. After the optional word designator, you can add a sequence of one or more
  355. of the following modifiers, each preceded by a @samp{:}.
  356. @table @code
  357. @item h
  358. Remove a trailing pathname component, leaving only the head.
  359. @item t
  360. Remove all leading pathname components, leaving the tail.
  361. @item r
  362. Remove a trailing suffix of the form @samp{.@var{suffix}}, leaving
  363. the basename.
  364. @item e
  365. Remove all but the trailing suffix.
  366. @item p
  367. Print the new command but do not execute it.
  368. @ifset BashFeatures
  369. @item q
  370. Quote the substituted words, escaping further substitutions.
  371. @item x
  372. Quote the substituted words as with @samp{q},
  373. but break into words at spaces, tabs, and newlines.
  374. @end ifset
  375. @item s/@var{old}/@var{new}/
  376. Substitute @var{new} for the first occurrence of @var{old} in the
  377. event line. Any delimiter may be used in place of @samp{/}.
  378. The delimiter may be quoted in @var{old} and @var{new}
  379. with a single backslash. If @samp{&} appears in @var{new},
  380. it is replaced by @var{old}. A single backslash will quote
  381. the @samp{&}. The final delimiter is optional if it is the last
  382. character on the input line.
  383. @item &
  384. Repeat the previous substitution.
  385. @item g
  386. @itemx a
  387. Cause changes to be applied over the entire event line. Used in
  388. conjunction with @samp{s}, as in @code{gs/@var{old}/@var{new}/},
  389. or with @samp{&}.
  390. @item G
  391. Apply the following @samp{s} modifier once to each word in the event.
  392. @end table