Tuesday, 25 March 2008

Favourite bash command line aliases

My favourite bash aliases currently are

alias hist='history|egrep'

and
alias ös='ls'

The second one for the reason that 'ö' sits next to 'l' on my Swedish keyboard, and when I intended to type 'ls' I type 'ös' more often than not. The one I use the most, however, is alias more='m' (I also have the classic more='mroe' and more='moer' to catch some frequent typos).

The first one, hist, makes it possible to use regular expressions to search the history of earlier shell commands. This is useful when you cannot remember some tricky command line sequence, or are too lazy to type some long command that you know you issued the other day.

For instance

hist 'java|ruby'

will print any previous command (in bash's history) containing any of the two strings.

(Well, I think you can accomplish the same thing using the original history command, but to paraphrase Morrissey, now my head is full, and my brain doesn't have room for more cryptic command line arguments.)

You can put your bash aliases in ~/.bashrc.

(Thanks to Chris for spotting a (now corrected) mistake in the first example. See the comment below.)

Update: Hey, checkout the comment by Anonymous below: Ctrl-r seems useful for searching the Bash history!

2 comments:

Anonymous said...

> alias hist='history|egrep $1'

While this might work for you, it's not the way aliases work.

alias hist='history|egrep

is all you need.

Anonymous said...

Someone pointed out that you can use Ctrl+R to search the history, works at least in the default shell in Ubuntu.

Ctrl+R and a few letters searches for earlier commands starting with those letters, Ctrl+R again moves to the next match.

Sorry if this seems like a noobish observation. I have used Linux on and off for the last ten years, but didn't know about this useful shortcut until recently.