Navigating Bash History with Ctrl+R

I spend a lot of time at command line, and bash records the history of all the commands I’ve typed and puts it in .bash_history (there’s a limit on how many it stores but for me it goes back a few days). I find it really useful to look back at the history to use the same commands again or edit them slightly. You can press the up arrow to go through your history but it can take a really long time to find what you’re looking for. So instead, try Ctrl + r.

To do this: first press Ctrl + r, then start typing the command or any part of the command that you are looking for. You’ll see an autocomplete of a past command at your prompt. If you keep typing, you’ll get more specific options appear. You can also press Ctrl + r again as many times as you want to, this goes back in your history to the previous matching command each time.

Once you see a command you like, you can either run it by pressing return, or start editing it by pressing arrows or other movement keys. I find this a really useful trick for going back to a command I know I used recently, but which I can’t remember or don’t want to look up again. I hope this is a useful trick for you too!

26 thoughts on “Navigating Bash History with Ctrl+R

  1. Thanks Lorna, i tried it and it works great. I normally use the command history | grep “” but this is faster i think.

  2. I can never get my head around CTRL-R history enough to use it usefully … I guess I was used to the 4NT style history with cursor keys in the old days + find it hard to navigate in CTRL-R

  3. Thanks for sharing this. After reading your post I realized that I can edit and run really long command. This definitely saves few keyboard hits and comes handy.
    Regarding running last executed commands there are few more ticks. You can using bang command “!”. This doesn’t provide you luxury to edit and run. it simply run the command.
    If you type “!find” it will try to run last “find” command. You can write the very first character of command it will find the last executed one and run it again.
    If you want to run just last command executed, run “!!”

  4. Thanks, I was missing that multiple [ctrl]-r could bring you farther back in history. To contribute back some tidbits, a handy command is alias:
    [code]alias l=’ls -al'[/code]
    So when you type “l” in console, it automatically does an “ls-al”.

  5. nice CTRL-R CTRL-R trick. It keeps searching backwords but once you use movement keys and pick the command how to further do search replace on it ?

  6. I’ve always used history command with grep to filter results. Its kinda useful too, cuz u can look for all matching registers.

  7. I use this all the time, you don’t even have to type the first part of the command. E.g whenever I want to restart apache I do ctrl+r then start typing “restart”.

    Also if you are cycling with ctrl+r and accidentally pass the command you wanted, just use ctrl+shift+r to reverse the reverse search :)

  8. Pingback: Mark Llobrera · Professional Amateurs: Memory Management « site design

  9. Pingback: Mark Llobrera · Professional Amateurs: Memory Management | WEB DESIGN

  10. Pingback: A Unix Guy looks at Powershell | Object Partners

  11. Pingback: Chimo ([email protected])'s status on Thursday, 13-Jul-2017 09:50:08 EDT - Micromic

  12. This is great. It’s changed my life! Ok, not my whole life, but I have lots of long, hard-to-remember commands that can’t be tab-completed, and this is exactly what I needed.

  13. I use Ctlr-R every day since i learnt it over the shoulder of a colleague freelance developer.

    After the 1st Ctrl-R, you can also
    1. Return to execute it
    2. left, right arrow and backspace to edit the commands before execution
    3. Ctrl-R again to see previous matches
    4. Arrows up and down to see previous or next commands
    5. Alt-Arrow left and right to navigate by word instead of characters
    6. ?? and ?? to move to the beginning and end of the command line == THIS IS THE MISSING ONE I COULD NOT FIND, IF ANYONE KNOWS IT PLEASE REPLY THANK YOU !!

    Hope this helped.

  14. If I search for a command and don’t find it (failed reverse-i-search), I want to carry on typing my new command and execute it. How can I do that?

  15. Pingback: Memory Management – Scrappy

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.