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

1
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.

1
!100

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.

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

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

1
cd !:2

The above command will evaluate to.

1
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-$.

1
2
3
notmuch search tag:unread from:e@mail.com

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 ...