[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9. Using History Interactively

This chapter describes how to use the GNU History Library interactively, from a user's standpoint. It should be considered a user's guide. For information on using the GNU History Library in other programs, see the GNU Readline Library Manual.

9.1 Bash History Facilities  How Bash lets you manipulate your command history.
9.2 Bash History Builtins  The Bash builtin commands that manipulate the command history.
9.3 History Expansion  What it feels like using History as a user.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.1 Bash History Facilities

When the `-o history' option to the set builtin is enabled (see section 4.3 The Set Builtin), the shell provides access to the command history, the list of commands previously typed. The value of the HISTSIZE shell variable is used as the number of commands to save in a history list. The text of the last $HISTSIZE commands (default 500) is saved. The shell stores each command in the history list prior to parameter and variable expansion but after history expansion is performed, subject to the values of the shell variables HISTIGNORE and HISTCONTROL.

When the shell starts up, the history is initialized from the file named by the HISTFILE variable (default `~/.bash_history'). The file named by the value of HISTFILE is truncated, if necessary, to contain no more than the number of lines specified by the value of the HISTFILESIZE variable. When an interactive shell exits, the last $HISTSIZE lines are copied from the history list to the file named by $HISTFILE. If the histappend shell option is set (see section 4.2 Bash Builtin Commands), the lines are appended to the history file, otherwise the history file is overwritten. If HISTFILE is unset, or if the history file is unwritable, the history is not saved. After saving the history, the history file is truncated to contain no more than $HISTFILESIZE lines. If HISTFILESIZE is not set, no truncation is performed.

The builtin command fc may be used to list or edit and re-execute a portion of the history list. The history builtin may be used to display or modify the history list and manipulate the history file. When using command-line editing, search commands are available in each editing mode that provide access to the history list (see section 8.4.2 Commands For Manipulating The History).

The shell allows control over which commands are saved on the history list. The HISTCONTROL and HISTIGNORE variables may be set to cause the shell to save only a subset of the commands entered. The cmdhist shell option, if enabled, causes the shell to attempt to save each line of a multi-line command in the same history entry, adding semicolons where necessary to preserve syntactic correctness. The lithist shell option causes the shell to save the command with embedded newlines instead of semicolons. The shopt builtin is used to set these options. See section 4.2 Bash Builtin Commands, for a description of shopt.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.2 Bash History Builtins

Bash provides two builtin commands which manipulate the history list and history file.

fc
 
fc [-e ename] [-nlr] [first] [last]
fc -s [pat=rep] [command]

Fix Command. In the first form, a range of commands from first to last is selected from the history list. Both first and last may be specified as a string (to locate the most recent command beginning with that string) or as a number (an index into the history list, where a negative number is used as an offset from the current command number). If last is not specified it is set to first. If first is not specified it is set to the previous command for editing and -16 for listing. If the `-l' flag is given, the commands are listed on standard output. The `-n' flag suppresses the command numbers when listing. The `-r' flag reverses the order of the listing. Otherwise, the editor given by ename is invoked on a file containing those commands. If ename is not given, the value of the following variable expansion is used: ${FCEDIT:-${EDITOR:-vi}}. This says to use the value of the FCEDIT variable if set, or the value of the EDITOR variable if that is set, or vi if neither is set. When editing is complete, the edited commands are echoed and executed.

In the second form, command is re-executed after each instance of pat in the selected command is replaced by rep.

A useful alias to use with the fc command is r='fc -s', so that typing `r cc' runs the last command beginning with cc and typing `r' re-executes the last command (see section 6.6 Aliases).

history
 
history [n]
history -c
history -d offset
history [-anrw] [filename]
history -ps arg

With no options, display the history list with line numbers. Lines prefixed with a `*' have been modified. An argument of n lists only the last n lines. Options, if supplied, have the following meanings:

-c
Clear the history list. This may be combined with the other options to replace the history list completely.

-d offset
Delete the history entry at position offset. offset should be specified as it appears when the history is displayed.

-a
Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.

-n
Append the history lines not already read from the history file to the current history list. These are lines appended to the history file since the beginning of the current Bash session.

-r
Read the current history file and append its contents to the history list.

-w
Write out the current history to the history file.

-p
Perform history substitution on the args and display the result on the standard output, without storing the results in the history list.

-s
The args are added to the end of the history list as a single entry.

When any of the `-w', `-r', `-a', or `-n' options is used, if filename is given, then it is used as the history file. If not, then the value of the HISTFILE variable is used.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3 History Expansion

The History library provides a history expansion feature that is similar to the history expansion provided by csh. This section describes the syntax used to manipulate the history information.

History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.

History expansion takes place in two parts. The first is to determine which line from the history list should be used during substitution. The second is to select portions of that line for inclusion into the current one. The line selected from the history is called the event, and the portions of that line that are acted upon are called words. Various modifiers are available to manipulate the selected words. The line is broken into words in the same fashion that Bash does, so that several words surrounded by quotes are considered one word. History expansions are introduced by the appearance of the history expansion character, which is `!' by default. Only `\' and `'' may be used to escape the history expansion character.

Several shell options settable with the shopt builtin (see section 4.2 Bash Builtin Commands) may be used to tailor the behavior of history expansion. If the histverify shell option is enabled, and Readline is being used, history substitutions are not immediately passed to the shell parser. Instead, the expanded line is reloaded into the Readline editing buffer for further modification. If Readline is being used, and the histreedit shell option is enabled, a failed history expansion will be reloaded into the Readline editing buffer for correction. The `-p' option to the history builtin command may be used to see what a history expansion will do before using it. The `-s' option to the history builtin may be used to add commands to the end of the history list without actually executing them, so that they are available for subsequent recall. This is most useful in conjunction with Readline.

The shell allows control of the various characters used by the history expansion mechanism with the histchars variable.

9.3.1 Event Designators  How to specify which history line to use.
9.3.2 Word Designators  Specifying which words are of interest.
9.3.3 Modifiers  Modifying the results of substitution.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3.1 Event Designators

An event designator is a reference to a command line entry in the history list.

!
Start a history substitution, except when followed by a space, tab, the end of the line, `=' or `('.

!n
Refer to command line n.

!-n
Refer to the command n lines back.

!!
Refer to the previous command. This is a synonym for `!-1'.

!string
Refer to the most recent command starting with string.

!?string[?]
Refer to the most recent command containing string. The trailing `?' may be omitted if the string is followed immediately by a newline.

^string1^string2^
Quick Substitution. Repeat the last command, replacing string1 with string2. Equivalent to !!:s/string1/string2/.

!#
The entire command line typed so far.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3.2 Word Designators

Word designators are used to select desired words from the event. A `:' separates the event specification from the word designator. It may be omitted if the word designator begins with a `^', `$', `*', `-', or `%'. Words are numbered from the beginning of the line, with the first word being denoted by 0 (zero). Words are inserted into the current line separated by single spaces.

For example,

!!
designates the preceding command. When you type this, the preceding command is repeated in toto.

!!:$
designates the last argument of the preceding command. This may be shortened to !$.

!fi:2
designates the second argument of the most recent command starting with the letters fi.

Here are the word designators:

0 (zero)
The 0th word. For many applications, this is the command word.

n
The nth word.

^
The first argument; that is, word 1.

$
The last argument.

%
The word matched by the most recent `?string?' search.

x-y
A range of words; `-y' abbreviates `0-y'.

*
All of the words, except the 0th. This is a synonym for `1-$'. It is not an error to use `*' if there is just one word in the event; the empty string is returned in that case.

x*
Abbreviates `x-$'

x-
Abbreviates `x-$' like `x*', but omits the last word.

If a word designator is supplied without an event specification, the previous command is used as the event.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3.3 Modifiers

After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a `:'.

h
Remove a trailing pathname component, leaving only the head.

t
Remove all leading pathname components, leaving the tail.

r
Remove a trailing suffix of the form `.suffix', leaving the basename.

e
Remove all but the trailing suffix.

p
Print the new command but do not execute it.

q
Quote the substituted words, escaping further substitutions.

x
Quote the substituted words as with `q', but break into words at spaces, tabs, and newlines.

s/old/new/
Substitute new for the first occurrence of old in the event line. Any delimiter may be used in place of `/'. The delimiter may be quoted in old and new with a single backslash. If `&' appears in new, it is replaced by old. A single backslash will quote the `&'. The final delimiter is optional if it is the last character on the input line.

&
Repeat the previous substitution.

g
Cause changes to be applied over the entire event line. Used in conjunction with `s', as in gs/old/new/, or with `&'.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on May 3, 2002 using texi2html