Syntax and Usage Notes: History Expansion

1. History expansions actually begin with the first character of the histchars shell parameter. ! is the default

2. A ! may be quoted out with a / or single quotes, but not double quotes.

3. Sometimes you might need to use a lot of literal !'s on your command line. (IE, you don't want them interpreted as history expansions.) As an alternative to quoting each literal !, you can put the !" two-character sequence somewhere on the command line. Then, all !'s on the command line appearing after !" will be taken as literal !'s. Note that the !" is silently discarded by zsh before further processing.

4. A colon can be omitted if the word designator begins with a `^', `$', `*', `-' or `%'.

5. Note that a % word designator will only work when used as !% , !:% , or !?str?:% , and only when used after a !? expansion. Anything else will result in an error, although the error may not be the most obvious one.

Miscellaneous

It is possible to type in a history reference without giving an event specification. !:$ is an example. Since such a history reference doesn't refer to an event directly, zsh makes it refer to the same event as the previous history reference on the current command-line. (Read that last sentence again, it will make sense eventually.)

Example

In the following line: echo !-3 !:^ !:1*

!:^ doesn't refer to a specific event by itself. However, the previous history reference, !-3, refers to the event three entries before the current command line. Thus, !:^ also refers to that same event.

Of course, !:^ could've been the first history reference on the command line. In that case, it would refer to the previous command by default.

You may like all history expansions with no events to refer to the previous command line. In that case, turn on the option CSH_JUNKIE_HISTORY.