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

Re: Reverse history search and match for a specific token



On Tue, Sep 7, 2021 at 3:54 AM Eric Smith <es@xxxxxxxxxxxxx> wrote:
>
> I use event history matching a fair amount and can subscript the command to return a specific token like this.
> $ !?foo?:%
> for the token containing foo
>
> How would I match for the token containing `bar`?

If I understand the question, you want the token containing bar from
the most recent command that contains foo, even if some other
intervening command also contains bar?

There's no single expansion that will do this.  What you would need is:

!?foo?:p
!?bar?:%

The :p step duplicates the most recent entry that contains foo,
without executing it, thus making it also the most recent that
contains bar.  You can then use :% to extract  the bar token.

To do it in one step you'd need ${(M)${(z)history[(r)*foo*]}:#*bar*}
but that's four and a half times as much typing.




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