Using the up arrow to run previous commands in bash? If so you’re doing it
wrong! Get to know you’re bash history and how you can use it to your
advantage. When you press the up arrow you are cycling through you bash
history, which you can see at anytime with the history
command.
|
First, say you want to rerun the very last command you just issued, instead of pressing the up arrow you can use the ‘double bang’ operator, which is the last command you ran.
|
Not to bad, but there is more power to !!
than first meets the eye. Think of
it as just the text of last command that is replaced as bash picks it up.
Because of this you can tack extra items to the front or end of it.
|
You can also run a specific command from history with !n
|
My favorite so far is !?
. It looks back from the history stack until it
finds a command that a command that matches it.
|
One last trick, say you goofed a command like so
|
You can clean it up with :s
|
The :s/find/replace
will search through the last command and replace the
first instance of “find” and replace it with “replace”. If you want to replace
all occurrences then use :gs/find/replace
. This may seem like a silly thing
but imagine; however, imagine you have hand crafted some long command… With
this you can rectify it quite quickly!
It is hard to clear that muscle memory of using the up arrow to sift through my bash history, but, hopefully with these commands you’ll be able to start saving time on the command line as you get used to making fewer keystrokes ; )