The best command line trick

The best command line trick is using Ctrl-r to do a reverse-incremental search on your command history.

If you know this trick, you’ll think, “Well, duh”. But if you don’t know this trick, it is pretty freaking amazing.

Programmers use a terminal (or shell, or command-line) to run software, install code, debug problems. Often, we run the same command repeatedly with slight modifications. Or we run a command one day, then need to run the same command three weeks later, when we can’t remember exactly what we did the first time. Almost everyone who uses the command line has figured out that you can get the previous command by typing the up arrow, and the next command by typing the down arrow. But that only gets you the most recent couple of commands – you get bored typing up arrow over and over again.

Instead, you can use Ctrl-r, and start typing some part of the command you previously used. At your prompt, the most recent command that includes that string will show up, and you can keep typing Ctrl-r to step back to the earlier ones.

Ctrl-r is one of those things you stumble across that vastly improves your life. Like finding out that Command-t will open a new tab in Terminal in Mac. Or that Command-2 will move to the 2nd tab in Chrome.

I learned about Ctrl-r when I was sitting elbow-to-elbow with a colleague, trying to debug some tricky issue. I was switching back and forth between a terminal window and an editor. I had been copying useful commands that I thought I might need later to a text file. When I needed one of those lines, I would find it in the text file, modify it, and copy it into my terminal. My friend said, “What on earth are you doing?” And showed me how to use Ctrl-r instead.

How is command-line history stored?

Many programs that a user enters commands into (shells like bash, or interpreters like ipython) maintain a history of those commands for later use.

Here is how bash stores and accesses its command history:

  • Each time you enter a command in Bash, it stores that command in memory. It keeps a certain number of previous commands in memory. The number is set by the environment variable HISTSIZE. The default is 500.

  • When Bash exits normally (by the user typing exit at the command prompt), the history commands are appended to a file. The default file is ~/.bash_history, but you can specify a different file with the environment variable HISTFILE. The number of entries saved into the history file is set by the environment variable HISTFILESIZE. The default for this is 500 too.

  • If Bash does not exit normally – say the session timed out, or the user closed the window before exiting Bash – the command history is not saved to the history file.

  • When Bash starts up, it reads the history file, and loads those commands into memory as a starting point for that session.

Useful tweaks to bash history

  • 500 commands? Are you kidding? This may have been enough in 1971, but our computers have big memories and bigger file systems, and it is not enough today. In your ~/.bashrc file, add the following lines:
export HISTSIZE=10000000
export HISTFILESIZE=10000000

If you want the size of the history file to be unlimited, set HISTFILESIZE to 0. See the Bash documentation for details.

  • My Bash sessions do not always exit gracefully. It is a total drag to lose all my command line history (and all the learning it implies) because I forgot to exit bash before shutting a window. To fix this problem, add the following line to your ~/.bashrc file:
export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

This will append any new history lines to the history file each time Bash displays a prompt. So your history file will always have all your history. Though if Bash dies while in the middle of executing a particular command, that command will not make it into the history file.

Note that if you use history -a with multiple terminals, the commands from them will be interleaved in the history file. This doesn’t bother me, because I almost exclusively use Ctrl-R to access my previous history. But if you expect groups of commands to be together, this may annoy you.

More information

For more details, see the History section of the bash man page.

Leave a Reply

Your email address will not be published. Required fields are marked *