Bang Bang

This article was published 6 years ago. Due to the rapidly evolving world of technology, some concepts may no longer be applicable.

Two quick terminal input manipulation commands today. The first is bangbang (!!). !! is a shortcut that means “substitute this with the last command run”. It’s most commonly used when you forget to sudo a command.

[email protected]:~$ apt-get install tmux
E: Could not open lock file /var/lib/dpkg/lock - 
open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/),
are you root?

[email protected]:~$ sudo !!
sudo apt-get install tmux
[sudo] password for vagrant:

If you want to make sure the command is right before executing it, you can add :p to the end to say “print this” instead of “execute this”.

[email protected]:~$ sudo !!:p
sudo apt-get install tmux

Then you can hit the up arrow and enter to run the command.

If you want a bit more versatility, you can use ^caret^substitution. This means “find this first string in the last command, replace it with the second one and run the command again”.

[email protected]:~$ sudo apt-get install tumx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package tumx

[email protected]:~$ ^tumx^tmux
sudo apt-get install tmux
Reading package lists... Done

Thoughts on this post

Leave a comment?