Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: history puzzle



On Tue, Jun 4, 2024 at 10:01 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:


On 2024-06-04 17:33, Lawrence Velázquez wrote:
I was reading 'info zsh history'
That shows the "History Expansion" section of the manual, which
does not mention the "history" builtin at all.

Ok, at least that's clear.  I should be using 'info zshbuiltins'.  'History Expansion' ... I'm taking that to be what the history command does so ... actually now that I look closely, it's obviously the wrong place.  And 'run-help history' goes somewhere else entirely.   

The term "history expansion" refers to the expansion of references into the command history. Those start with !, for example:

zsh% vi path/to/some/file
zsh% gcc !$ # compile the file I just edited

In general, the ! is followed by a selector that determines which item from the history you want to copy from. This can be an item number (which matches the one shown by history/fc -l), another (which means the most recent command), or a prefix string (which refers to the most recent command starting with that prefix, e.g. !ls to refer to the most recent ls command). The selector can be followed by modifiers to restrict the expansion to just part of the command: :0 for the command name, :* for the arguments without the command name, :$ for just the last argument, etc. The !!:$ _expression_ (last word of the last command) is so common that it got its own shortcut, the !$ I used above.

History expansion is purely an interactive command-line feature; it doesn't work in scripts. If you need to access the history programmatically, use the fc command (or history, which is equivalent to fc -l). 

Anyway, that's what you were reading about. :)
--
Mark J. Reed <markjreed@xxxxxxxxx>


Messages sorted by: Reverse Date, Date, Thread, Author