Last Updated: March 28, 2017
· copenhas

Zsh rerun last command with search/replace

Heads up, you can rerun the last command with a replace in one swoop:

!!:s/find it/replace it/

That'll do the first match, but to replace all matches on the line:

!!:gs/gotta/replace them all/

If that's not short enough for you (this is UNIX command line after all), rerun last command and perform a replace on first match:

^find it^replace it

Oh, two more things...

it's stackable:

!!:s/find this/replace it/:s/find that/replace it too/

same thing works with variables:

echo ${PATH:gs/copenhas/YOU/}
Say Thanks
Filed Under