Last Updated: February 25, 2016
·
537
· lepht

Insert previous shell command

In the bash and zsh shells, !! will be replaced with the previous command wherever you use it. When combined with the back-tick operator, you can do some useful and clever things aside from just re-running the previous command.

A couple examples I use often:

which n' vim

Sometimes, I want to see what a particular command is actually running, and after seeing it's (for example) actually a script being executed from my $PATH:

$ which fasd
/Users/lepht/scripts/fasd

Now I decide I want to modify that script, I just use that previous command via the !! syntax to specify the file path for my vim command:

vim `!!`

forgot the sudo, bro

Don't you hate it when you forget to prefix a command with sudo?

ls -a ~root  
ls: root: Permission denied

!! makes it easy to quickly correct your mistake:

sudo !!
root_secret_stuff .vimrc .config