Guy I know - Oliver - command line ninja. Never makes a mistake. Can configure an AWS in a single long bash command. Typing speed through the roof. Bet you know someone like that too.
We mere mortals make mistakes and, while it's always good to learn from your mistakes, the first thing you have to do is fix them.
And to fix them you need to learn how to fix them.
Say you've typed an impossibly long command into the terminal with one
irritating mistake. For me, it's usually something to do with
curl -s -I -X POST https://en.wikipedia.org/wiki/CURL | grep HTTP | cut -d ' ' -f 2
Not the greatest command - but say I couldn't spell wikipedia...
curl -s -I -X POST https://en.wikpedia.org/wiki/CURL | grep HTTP | cut -d ' ' -f 2
Naive, and effective. Press up to show the last command, keep tapping left until you get the the bit of the command you need to change, backspace to remove what you don't need and then enter what you do need
Bash has a vi mode, which can be activated by adding the following to your
set -o vi
If you're comfortable with vi you can now hit
Escape to bounce into normal
Ctrl-P to go back to the last command,
b a few times to get to the
word you need to change... etc.
Vi mode is great - if you know a bit of vi. But you might not. So...
How about something a little smarter:
This is the bash quick substitution history expansion command - it runs the last command, substituting the first instance of the charaters after the first caret with the characters after the second caret.
Pretty neat huh? But that will olny work for the first instance - what if we
need to replace every instance of
wikpedia in the last command?
Bash uses the
! character as the history expansion character - it
is used to substitute a part of your current command with a previously executed
! does nothing - but the previous command can be accessed with
!! sequence. So, to print out the last command, try:
These history expansions can also take modifier options to change the string
before it gets inserted. The syntax is
<select>:<modifier>. For instance, to
put the last command in quotes:
And to perform a global substitution on it:
There is lots that can be done with the above syntax - just take a look at the documentation.
Seriously, by the time you've remembered how to do some of the above, wouldn't it have just been easier to type it out again.
Just don't mess it up this time, right?
And this is the reason I have to escape
! whenever I use it in commit messages ↩