7 Bash history shortcuts you will actually use

Save time on the command line with these essential Bash shortcuts.
535 readers like this.
Command line prompt

Opensource.com

Most guides to Bash history shortcuts exhaustively list every single one available. The problem with that is I would use a shortcut once, then glaze over as I tried out all the possibilities. Then I'd move onto my working day and completely forget them, retaining only the well-known !! trick I learned when I first started using Bash.

So most of them were never committed to memory.

This article outlines the shortcuts I actually use every day. It is based on some of the contents of my book, Learn Bash the hard way; (you can read a preview of it to learn more).

When people see me use these shortcuts, they often ask me, "What did you do there!?" There's minimal effort or intelligence required, but to really learn them, I recommend using one each day for a week, then moving to the next one. It's worth taking your time to get them under your fingers, as the time you save will be significant in the long run.

1. The "last argument" one: !$

If you only take one shortcut from this article, make it this one. It substitutes in the last argument of the last command into your line.

Consider this scenario:

$ mv /path/to/wrongfile /some/other/place
mv: cannot stat '/path/to/wrongfile': No such file or directory

Ach, I put the wrongfile filename in my command. I should have put rightfile instead.

You might decide to retype the last command and replace wrongfile with rightfile completely. Instead, you can type:

$ mv /path/to/rightfile !$
mv /path/to/rightfile /some/other/place

and the command will work.

There are other ways to achieve the same thing in Bash with shortcuts, but this trick of reusing the last argument of the last command is one I use the most.

2. The "nth argument" one: !:2

Ever done anything like this?

$ tar -cvf afolder afolder.tar
tar: failed to open

Like many others, I get the arguments to tar (and ln) wrong more often than I would like to admit.

xkcd comic

When you mix up arguments like that, you can run:

$ !:0 !:1 !:3 !:2
tar -cvf afolder.tar afolder

and your reputation will be saved.

The last command's items are zero-indexed and can be substituted in with the number after the !:.

Obviously, you can also use this to reuse specific arguments from the last command rather than all of them.

3. The "all the arguments" one: !:1-$

Imagine I run a command like:

$ grep '(ping|pong)' afile

The arguments are correct; however, I want to match ping or pong in a file, but I used grep rather than egrep.

I start typing egrep, but I don't want to retype the other arguments. So I can use the !:1$ shortcut to ask for all the arguments to the previous command from the second one (remember they’re zero-indexed) to the last one (represented by the $ sign).

$ egrep !:1-$
egrep '(ping|pong)' afile
ping

You don't need to pick 1-$; you can pick a subset like 1-2 or 3-9 (if you had that many arguments in the previous command).

4. The "last but n" one: !-2:$

The shortcuts above are great when I know immediately how to correct my last command, but often I run commands after the original one, which means that the last command is no longer the one I want to reference.

For example, using the mv example from before, if I follow up my mistake with an ls check of the folder's contents:

$ mv /path/to/wrongfile /some/other/place
mv: cannot stat '/path/to/wrongfile': No such file or directory
$ ls /path/to/
rightfile

I can no longer use the !$ shortcut.

In these cases, I can insert a -n: (where n is the number of commands to go back in the history) after the ! to grab the last argument from an older command:

$ mv /path/to/rightfile !-2:$
mv /path/to/rightfile /some/other/place

Again, once you learn it, you may be surprised at how often you need it.

5. The "get me the folder" one: !$:h

This one looks less promising on the face of it, but I use it dozens of times daily.

Imagine I run a command like this:

$ tar -cvf system.tar /etc/system
 tar: /etc/system: Cannot stat: No such file or directory
 tar: Error exit delayed from previous errors. 

The first thing I might want to do is go to the /etc folder to see what's in there and work out what I've done wrong.

I can do this at a stroke with:

$ cd !$:h
cd /etc

This one says: "Get the last argument to the last command (/etc/system) and take off its last filename component, leaving only the /etc."

6. The "the current line" one: !#:1

For years, I occasionally wondered if I could reference an argument on the current line before finally looking it up and learning it. I wish I'd done so a long time ago. I most commonly use it to make backup files:

$ cp /path/to/some/file !#:1.bak
cp /path/to/some/file /path/to/some/file.bak

but once under the fingers, it can be a very quick alternative to …

7. The "search and replace" one: !!:gs

This one searches across the referenced command and replaces what's in the first two / characters with what's in the second two.

Say I want to tell the world that my s key does not work and outputs f instead:

$ echo my f key doef not work
my f key doef not work

Then I realize that I was just hitting the f key by accident. To replace all the fs with ses, I can type:

$ !!:gs/f /s /
echo my s key does not work
my s key does not work

It doesn't work only on single characters; I can replace words or sentences, too:

$ !!:gs/does/did/
echo my s key did not work
my s key did not work

Test them out

Just to show you how these shortcuts can be combined, can you work out what these toenail clippings will output?

$ ping !#:0:gs/i/o
$ vi /tmp/!:0.txt
$ ls !$:h
$ cd !-2:h
$ touch !$!-3:$ !! !$.txt
$ cat !:1-$

Conclusion

Bash can be an elegant source of shortcuts for the day-to-day command-line user. While there are thousands of tips and tricks to learn, these are my favorites that I frequently put to use.

If you want to dive even deeper into all that Bash can teach you, pick up my book, Learn Bash the hard way or check out my online course, Master the Bash shell.


This article was originally posted on Ian's blog, Zwischenzugs.com, and is reused with permission.

What to read next
Avatar
Open Source Coder/Speaker/Author/Blogger/Trainer

12 Comments

Wow! Really useful! Thanks for share.

Wow, this is great. I knew about the first one but the other six are going to save me so much swearing under my breath!

Your "current line" example is too contrived. Your example is copying to a backup like this:

$ cp /path/to/some/file !#:1.bak

But a better way to write that is with filename generation:

$ cp /path/to/some/file{,.bak}

That's not a history expansion though... I'm not sure I can come up with a good reason to use `!#:1`.

I've been doing bash for about 19 years and I never use these except for !!$. Users are really better off learning how to edit their previous command line with up arrow and the key sequence that triggers edit-and-execute-command. You'll get a emacs or vi editor that makes it super fast to change arguments around.

The problem with bash's history shorcuts for me is... that I never had the need to learn them.

Provided that your shell is readline-enabled, I find it much easier to use the arrow keys and modifiers to navigate through history than type !:1 (or having to remeber what it means).

Examples:

Ctrl+R for a Reverse search
Ctrl+A to move to the begnining of the line (Home key also)
Ctrl+E to move to the End of the line (End key also)
Ctrl+K to Kill (delete) text from the cursor to the end of the line
Ctrl+U to kill text from the cursor to the beginning of the line
Alt+F to move Forward one word (Ctrl+Right arrow also)
Alt+B to move Backward one word (Ctrl+Left arrow also)
etc.

YMMV of course.

These are some great history substitution tips. I know of some of them but not all of them.

```
cp /path/to/some/file !#:1.bak
```

I think that example would be easier with brace expansion like this:

```
cp /path/to/some/{file,file.bak}
```

Great tips!:0 Many thx!!

Great article, but it's missing one pattern I use a lot, !* It repeats all
the parameters in the previous command.

One use case is to run ls to ensure all the right files are matched
prior to running rm. For example, to remove all the pdf files that
are tutorials...

ls *[Tt]ut*.pdf
rm !*

Regarding item 3:
$ grep '(ping|pong)' afile

You don't have to type out the correct command (egrep, as opposed to grep), as just the "e" will work if you include 0 in your range:

$ e!:0-2
egrep '(ping|pong)' afile
pingpang
pangpong
pingpong

Regarding item 3:
$ grep '(ping|pong)' afile

You don't have to type out the correct command (egrep, as opposed to the incorrectly entered grep), as just the "e" will work if you include 0 in your range:

$ e!:0-2
egrep '(ping|pong)' afile
pingpang
pangpong
pingpong

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.