The history command has loads of hidden features. I can never remember all of them so the main purpose of this article is to me to reference back to when I next forget.

Searching the history with grep

This command will search the history for git

history | grep git

History Bang

The history can be used by typing a !. This with a number will run that command from the history. You can find the number of the command you need by searching the history.

This command will run the 100th command in the history.


Double Bang

You can run the last command you run by typing !!

Using history params

All the arguments passed to command can be broken down into parts.

0   1       2
_ _________
| | | |
ls -al ~/Documents

So if we run the above command and then cd into ~/Documents we can run.

cd !:2

The above command will evaluate to.

cd ~/Documents

You can also use a range of params by using the pattern x-y. The will pass a range of params to the command. You can also use the $ as the last param, also a * is a shortcut for 1-$.

notmuch search tag:unread

notmuch tag 2-$

Skiping the history

You skip adding a command into the history by prefixing the command with a space, this is handy for searching the history so you don’t fill up your history with history | grep ...