HOWTO: quick search terminal history commands

January 20, 2007

I like the MATLAB way of input command — it has the same tab-complete feature; in addition, it has a up-arrow-auto-complete-history-command feature. For example, if I have typed a command “x=39;y=23;” before, now I can just type “x” and then type the up arrow button, then this command would auto-complete. It can also switch between all the history commands that has the same starting letters.

Vert unfortunately, I haven’t found any similar feature in the Linux terminal, or say bash. There is a command called “history” that can list all the history commands. I can type “history | grep xxx” to search the command I want to find. The same example, I can type “history | grep x=” try to find that command. There would come up something like “201 x=39;y=23;“. The number in the beginning is the index of in the history. Then I can type “!201″ to repeat this command. But I find this quite time-consuming.

Then I came across a shortcut of bash “Ctrl + R“, it can “search through previously used commands”. Say in the terminal I type “Ctrl + R”, then “(reverse-i-search):” came up, then I can type keyword of my previous command, say “x=” then the most related command would come up. After the right command comes up, then type enter, finish. Although not as convinient as the MATLAB mode, this is way better than using “history” command, in my view.