Improve Command Line History Search with These .inputrc Modifications
If you’re a heavy command line user, you’re probably well aware that the arrow keys can be used to flip through previously executed commands and the tab key can complete them. But both of these functions can be significantly improved upon for searching through past command history by adding a few modifications to your .inputrc file.
The first two lines allow you to use the arrow keys to flip through command history for either a specific command, or even just the beginning of a specific command. For example, if you recall a command started with “c” but you’re not sure what else, you could simply type ‘c’ and then hit the up arrow to start searching through command history for anything that begins with the letter c. This also works to search through full commands, so you can search all history for the ‘curl’ command by typing curl and then following it up with the up or down arrow. The second batch of three lines are shared from a past tip and greatly improves the Tab completion abilities in the terminal, thereby bringing tab completion to the history, removing case sensitivity in completion, and the ability to see everything if the attempt is completely ambiguous. Combined, your history searches and tab completion will be dramatically improved.
Launch Terminal, and open your .inputrc file in a preferred text editor. We’ll use nano because it’s very user friendly:
nano .inputrc
Paste the following five lines into the (presumably blank) .inputrc file:
"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on
TAB: menu-complete
That should look like this:
Hit “Control+O” to save the file, then Control+X to exit nano.
Refresh the terminal or create a new one and you’ll be able to see the difference immediately when using the arrows and tab keys when flipping through command history.
The length of history stored still depends on on what HISTFILESIZE is set to, so don’t forget to adjust that to accommodate your needs. Also note that if you clear command history with any regularity, the usefulness of these features will degrade considerably.
We’ve discussed other ways of printing and searching through past command history before but this is perhaps the best option if you’re looking to execute a past command again rather than simply locate the syntax of a long-executed string. If your primary interaction with terminal is for defaults commands, don’t forget the excellent trick to automatically keep track of them by storing any ‘defaults’ string into an individual text file, though these inputrc adjustments will still make your job easier if you’re looking to toggle a setting in the future.
Heads up to Lifehacker for the arrow key history search tricks, though you may recall the other .inputrc lines from a prior trick that we shared a while back.