Fewer keystrokes in bash with Ctrl-R

The bash shell has a powerful history mechanism, which in its default state isn't too useful. Luckily the modifications are quite easy to make.

Hit Ctrl-R and then type the first few letters of a previously executed command. Bash will search through the search history to find the command that matches this string. Continue to hit Ctrl-R to see the alternatives. When you find the command that you want, hit return and it'll be executed. Fewer keystrokes, less effort, fewer typos.

The bash(1) manpage details a number of variables that dictate how bash handles command history.

Two of these variables are:

HISTFILESIZE

HISTSIZE

Both have a default of 500, which if live on the command line is far too small. Increase the size of both of these variables by adding the following lines to your $HOME/.bashrc file:

export HISTFILESIZE=20000

export HISTSIZE=$HISTFILESIZE

A pool of 20,000 previously executed commands (or more if you fancy) increases the utility of the command history search dramatically.

Advanced shell users might like to take note of what the manpage says about the $HISTCONTROL variable, especially the sentence:

If the list of values includes ignorespace, lines which begin with a space character are not saved  in the history list.

Arrow