Repeat last command matching “two words” zsh

command historyoh-my-zshzsh

I want to repeat the last command that matches foo bar. Using !! doesn't work (it's not the last command I used), and neither does !foo, because my history looks something like

foo bar dee zep
foo boo lee kee
foo bee

If I try to type !foo bar, zsh auto-completes to foo bee (which is the same as !! anyway) as I hit the space. How would I do this?

Best Answer

!?foo bar

See http://zsh.sourceforge.net/Doc/Release/Expansion.html#Event-Designators

Edit: No, you must not escape the spaces. If you need to add something that is not part of the history expansion, separate it with another ?, e.g.:

echo hello
echo foo
!?echo hello? world # runs "echo hello world"
Related Question