While pairing with a co-worker the other night I learned about the
!! command to repeat your last command. Which I think is just amazing and I have no idea how I have been living all these years without it.
The scenario in which we used it was I was copying an existing file into a file with a different name so that we could edit it:
$ cp really/long/path/filename.txt really/long/path/filename2.txt
I then needed to open the file up in vim and proceeded to type this command:
$ vim really/long/path/filename2.txt
but before I could finish typing that long path I was interrupted with the question, "Haven't you heard of the bang bang command??".
So apparently you can repeat your last command and only grab specific arguments of the last command like this:
$ vim !!:2
This will use our last command and just pull the 2nd argument which happens to be the name of our new filename. Amazing right!!??
Repeat last command with sudo
Here is where I'm super interested in using the
!! command. Have you have typed in a command and then were faced with this error message:
I forget to use sudo all the time and then like a barbaric I either type sudo and then retype the entire command or I use the up arrow and then the left arrow a billion times until I'm at the beginning of the line and then type sudo. But watch this:
$ sudo !!
This will repeat your last command with sudo!!