History substitutions place words from previous command input as portions of new commands, making it easy to repeat commands, repeat arguments of a previous command in the current command, or fix spelling mistakes in the previous command with little typing and a high degree of confidence. History substitutions begin with the character `!' and may begin
anywhere in the input stream (with the proviso that they
do not nest.) This `!' may be preceded by a `\' to prevent its special meaning; for convenience, an `!' is passed unchanged when it is followed by a blank, tab, newline, `=' or `('. (History substitutions also occur when an input line begins with `↑'. This special abbreviation will be described later.) Any input line that contains history substitution is echoed on the terminal before it is executed as it would have been typed without history substitution.
Commands input from the terminal that consist of one or more words are saved on the history list. The history substitutions reintroduce sequences of words from these saved commands into the input stream. The size of the history list is controlled by the
history variable; the previous command is always retained, regardless of the value of the history variable. Commands are numbered sequentially from 1.
For example, consider the following output from the
history command:
09 write michael
10 ex write.c
11 cat oldwrite.c
12 diff *write.c
The commands are shown with their event numbers. It is not usually necessary to use event numbers, but the current event number can be made part of the
prompt by placing an `!' in the prompt string.
With the current event 13 we can refer to previous events by event number `!11', relatively as in `!-2' (referring to the same event), by a prefix of a command word as in `!d' for event 12 or `!wri' for event 9, or by a string contained in a word in the command as in `!?mic?' also referring to event 9. These forms, without further change, simply reintroduce the words of the specified events, each separated by a single blank. As a special case, `!!' refers to the previous command; thus `!!' alone is a
redo.
To select words from an event we can follow the event specification by a `:' and a designator for the desired words. The words of an input line are numbered from 0, the first (usually command) word being 0, the second word (first argument) being 1, etc. The basic word designators are:
↑
first argument, i.e., `1'
%
word matched by (immediately preceding) ?s? search
*
abbreviates `↑-$', or nothing if only 1 word in event
x-
like `x*́ but omitting word `$'
The `:' separating the event specification from the word designator can be omitted if the argument selector begins with a `↑', `$', `*', `-' or `%'. After the optional word designator can be placed a sequence of modifiers, each preceded by a `:'. The following modifiers are defined:
h
Remove a trailing pathname component, leaving the head.
r
Remove a trailing `.xxx' component, leaving the root name.
e
Remove all but the extension `.xxx' part.
s/l/r/
Substitute l for r
t
Remove all leading pathname components, leaving the tail.
&
Repeat the previous substitution.
g
Apply the change once on each word, prefixing the above, e.g., `g&'.
a
Apply the change as many times as possible on a single word, prefixing the above. It can be used together with `g' to apply a substitution globally.
p
Print the new command line but do not execute it.
q
Quote the substituted words, preventing further substitutions.
x
Like q, but break into words at blanks, tabs and newlines.
Unless preceded by a `g' the change is applied only to the first modifiable word. With substitutions, it is an error for no word to be applicable.
The left hand side of substitutions are not regular expressions in the sense of the editors, but instead strings. Any character may be used as the delimiter in place of `/'; a `\' quotes the delimiter into the
l and
r strings. The character `&' in the right hand side is replaced by the text from the left. A `\' also quotes `&'. A null
l (`//') uses the previous string either from an
l or from a contextual scan string
s in `!?
s\?'. The trailing delimiter in the substitution may be omitted if a newline follows immediately as may the trailing `?' in a contextual scan.
A history reference may be given without an event specification, e.g., `!$'. Here, the reference is to the previous command unless a previous history reference occurred on the same line in which case this form repeats the previous reference. Thus `!?foo?↑ !$' gives the first and last arguments from the command matching `?foo?'.
A special abbreviation of a history reference occurs when the first non-blank character of an input line is a `↑'. This is equivalent to `!:s↑' providing a convenient shorthand for substitutions on the text of the previous line. Thus `↑lb↑lib' fixes the spelling of `lib' in the previous command. Finally, a history substitution may be surrounded with `{' and `}' if necessary to insulate it from the characters that follow. Thus, after `ls -ld ~paul' we might do `!{l}a' to do `ls -ld ~paula', while `!la' would look for a command starting with `la'.