HOWTO: quick search terminal history commands

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.

13 Responses to “HOWTO: quick search terminal history commands”

  1. kmandla Says:

    Excellent! I’ve been looking for something like this for a while. Thanks!

  2. bapoumba Says:

    Neat, thanks

  3. bapoumba Says:

    Second thought : I’ve posted your tip in French, linking to your blog page. I hope it’s okay with you ;)
    http://bapoumba.free.fr/?p=78

  4. Wei Says:

    To bapoumba: That’s totally ok, I am pleased to share with others. In addition, I did not invent this, I just read this from somewhere else :) . Thanks for sharing this.

  5. Gabriel Says:

    Well if you will not miss job control, you can use ipython, which is an awesome python shell, that has a shell like environment (to start it use ipython -p pysh). You can then have the exact matlab feature you want.

    Suppose I have done a search like,
    fine . -name ‘*.tex’

    Then the next time I want to use it I simply type
    find
    it will even cycle through all previous find commands, etc.

    (Combine this program with numpy and you also have a basic matlab replacement :-)

  6. Wei Says:

    This ipython is quite nice! thanks

  7. Tobias Says:

    Something even better (and actually what you want).
    Go to /etc/inputrc and find the line with history search and page-up/down, and uncomment it.
    Voila history search with Page-Up/Down from the letter in front of the cursor.

  8. Alessandro Says:

    Thanks! Great advice!

  9. Shane Says:

    Hey,

    The way to do this “like MATLAB” is by adding the following lines in your .bash_profile or .bashrc file (and subsequently running source .bashrc, etc):

    bind ‘”\M-[A”: history-search-backward’
    bind ‘”\M-[B”: history-search-forward’

    These are the keybindings I use for bash 3.2 on Mac OS X, so you might have to find other keybindings specific to your machine, but the general syntax is there! Enjoy!

  10. Ray Says:

    Many thanks!

  11. Victor Says:

    Thanks a lot!!!

  12. Chee How Says:

    Another tip:

    Pressing Ctl+R again while searching will skip the latest command that is found and go on to the next one.

  13. cms Says:

    Thanks a lot!!!

Leave a Reply