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.


39 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:

    Second thought : I’ve posted your tip in French, linking to your blog page. I hope it’s okay with you 😉

  3. 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.

  4. 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
    it will even cycle through all previous find commands, etc.

    (Combine this program with numpy and you also have a basic matlab replacement 🙂

  5. Wei says:

    This ipython is quite nice! thanks

  6. 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.

  7. Alessandro says:

    Thanks! Great advice!

  8. Shane says:


    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!

  9. Ray says:

    Many thanks!

  10. Victor says:

    Thanks a lot!!!

  11. 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.

  12. cms says:

    Thanks a lot!!!

  13. Rakesh says:

    When i use up arrow to see or use the earlier commands, it does’nt show or list out.

  14. Spencer says:

    Who is one person who has been influential in your life, for better or worse?

  15. Muthiah says:

    Awesome, this is sweet

  16. Abbie says:

    Thanks! I have been looking for a solution to this problem for a long time!!

  17. Jawaica says:

    This is very useful, I’ve been searching for this for so long. Thanks

  18. Mabuhay says:

    Thank you, I’m glad I came across this site.

  19. x0r says:

    thanks, that helped!

  20. Matt says:

    The most obvious technique is missing from the comments above. If one uses VI Editing Mode (I assume most of the readers are), searching through history with / ? n and N is simple.
    If you don’t add one line, “set -o vi”, to your ~/.bashrc file
    See for example “Readline VI Editing Mode Cheat Sheet” at

  21. DSH says:

    Thanks helped a lot

  22. How to search partial matching commands with history?

  23. giordi says:

    if you type ctrl+r and write, for example “cd”, you can search among every “cd” command saved in history!

  24. Buminda says:

    ctrl + r , saves ot of time

  25. nicodeem says:

    This is the guide to previous commands search and edit:

  26. shan says:

    use this

    then all you need is to type x and hit upward arrow until you see your command

  27. shan says:

    or summarized

    in terminal enter:

    sudo gedit ~/.inputrc

    then copy paste and save

    “\e[A”: history-search-backward
    “\e[B”: history-search-forward
    “\e[C”: forward-char
    “\e[D”: backward-char

    FROM now on [and many agree this is the most useful tool]

    all you need to do to find a previous command is to enter say the first 2 or 3 letters and upward arrow will take you there quickly
    all i need to do is enter ff and hit upward arrow command will soon appear

  28. Achari says:

    This is exactly what I wanted.
    Thanks a million.

  29. MMG says:

    Thanks a lot!

  30. says:

    Thanks..Helps a lot

  31. Niraj K says:

    Thank you….very helpful!!!!!

  32. johndoge says:

    YES! I would’ve shot you some Dogecoin if you had a donation address somewhere. Thanks.

  33. Kornél says:

    Love it! Thanks!

  34. When your kitty kicks litter throughout the flooring, simply use a larger pot. A huge, roomy tote rich in sides creates a good litter box. A large spherical bathtub also will make a very good cat litter box. A cafe or restaurant sizing tour bus tub is a capacious cat litter box. Providing higher ends and a lot more space will solve your cat’s litter kicking problem.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: