History expansion is a way that zsh allows you to
to quickly recall previously executed commands from the current
A history expansion begins with a
! (1). They may occur
anywhere on a command line, but they do not nest. If you'd like to
use a literal
! on the command line, quote it out.
After you hit
ENTER on the command line, zsh evaluates
all of the history expansions before doing anything else. It then
echoes the resultant command-line thus far, and continue on to other
types of substitution and expansion.
(Some people like to double-check their intended history expansion
before the shell processes the rest of the command line. See
HIST_VERIFY on the Related Options page.)
Some Handy Tricks
- You can use the < UP-ARROW > and < DOWN-ARROW > keys to cycle through
your past commands. This will only work, however, if your terminal's arrow keys
are bound to the apprpriate line editing commands. (See the zsh
line editor section for details). [Larry: Add link later]
- By entering
the command line, you can run the previous command with any occurance
old replaced with
- You can put an event into the history list without
having to actually execute the command. You can do this with the
-s argument to the
There are three parts to a history expansion: event designators,
word designators (optional), and modifiers (optional). The syntax is
An event designator is simply something you type on the command line
that refers to an entry in the history list. There are many useful
event designators, summarized below.
- Start a history expansion, except when followed by a
blank, newline, `=' or `('
- Refer to the previous command. By itself, this expansion
repeats the previous command.
- Refer to command-line n.
- Refer to the current command-line minus n.
- Refer to the most recent command starting with
- Refer to the most recent command containing str
- Refer to the current command line typed in so far. The
line is treated as if it were complete up to and
including the word before the one with the
- Insulate a history reference from adjacent characters
When making a history reference, you don't need to recover the whole
command. Perhaps you only want a few words from the command, You can
use a word designator to indicate which word or words
of a given history entry you want included in a history reference.
Simply specify an event designator from above, followed by a
and then the word designator.
Here are the various word designators that you can use:
- The first input word (command)
- The nth argument
- The first argument
- The last argument
- The word matched by (the most recent)
- A range of words.
x defaults to 0.
- All of the arguments, or a null value if there are none
- An abbreviation for
x*, but omitting word
These are the same set of modifiers that work on filename generation
and parameter expansion. See the Modifiers page.
Syntax and Usage Notes
Related Options and Parameters