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

Re: creating a mnemonic for a history command



> On 17/05/2024 12:03 BST Allomorphy <allomorphy@xxxxxxxxx> wrote:
> This is one of the most useful incantations for me:
> !?foo?:%
> 
> It searches in the history for the latest command that contains the
> string `foo' and returns the token (or phrase between spaces)  in that
> command.
> 
> Its a pain to type.
> What is an elegant way to set up a mnemonic like say an alias or
> something else to type
> gimme(foo)
> or
> !@#foo

Your best bet is probably to use an editor function --- not a lot
else happens before the history expansion so it's hard to do in the main
shell.

gimme() {
  LBUFFER='!?'$LBUFFER
  RBUFFER='?:%'$RBUFFER
  zle accept-line
}
zle -N gimme
bindkey '^xg' gimme

Type "foo" or whatever on the command line and then hit ^xg.

Note I have not made any attempt to add sanity checks.

pws




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