In the terminal, I can type CTRL-R to search for a matching command previously typed in BASH. E.g., if I type CTRL-R then grep is lists my last grep command, and I can hit enter to use it. This only gives one suggestion though. Is there any way to cycle through other previously typed matching commands?

up vote 448 down vote accepted

If I understand the question correctly you should be able to cycle through alternatives by repeatedly hitting Ctrl+R.


Ctrl+R grep Ctrl+R Ctrl+R ...

That searches backwards through your history. To search forward instead, use Ctrl+S. (More details here.)

  • 20
    +1 -- FYI -- you can also search forward as per this StackOverflow answer. – Jordan Arseno Apr 24 '13 at 21:14
  • 6
    And use Ctrl+Shift+r for reverse scrolling if you happen to pass over. – wiswit Nov 1 '15 at 13:41
  • 16
    @wiswit CTRL+SHIFT+r doesn't work for me. – Maxim Suslov Apr 5 '16 at 7:48
  • 6
    @MaximSuslov See this question:… You can add [[ $- == *i* ]] && stty -ixon to your .bashrc and then CTRL+s will work as the reverse of CTRL+r – gla3dr Apr 21 '16 at 17:03
  • 8
    @JordanArseno I took "search forward" to mean "search for commands I have not yet typed" – Josh Johnson Jul 28 '16 at 22:10

If you feel the command will be used frequently, you could add a tag

command #useful


ctrl+r #useful

This works because # is a comment delimiter, i.e. everything that comes after the symbol is not interpreted as a command. However, it will be recorded in the history and is thus searchable.

  • 2
    Usually I do like this kind of tagging. #trg_bld #open_log – Makesh Jul 31 '15 at 5:50
  • 1
    where to add this tag? – Sudip Bhandari Nov 3 '16 at 13:46
  • 3
    @SudipBhandari just after your command. # starts a comment. E.g. ls -lah #useful – Andrei Dec 4 '16 at 9:27
  • this one I learn it from Quora – elsadek Nov 13 '17 at 8:17

You can also set up the up and down arrows to do a slightly different search by adding these lines to ~/.inputrc:

"\e[A": history-search-backward
"\e[B": history-search-forward

Instead of searching for a substring anywhere in the command (like Ctrl-r) it will search for a command starting with the text to the left of the cursor. For example, if I run these commands:

$ ls bart
$ ls fools

then type ls and press Up twice, it will show ls bart and the cursor in the same place. Compare with Ctrl-r, where it would find the ls twice in the last line, so you'd have to press it once again to find the previous line.

These approaches both have their strengths, and both of them can save a lot of time.

  • 2
    This is also standard on OS X, so you don't need to create ~/.inputrc and add those two lines. – DASKAjA Aug 12 '16 at 14:36
  • 1
    As falconepi have written in the comments of this answer, on Ubuntu you just need to uncomment in ~/.inputrc the two lines including history-search-* – Arpad Horvath Aug 16 '16 at 7:11
  • You could also look at this post for more details on this answer:… – Andrei Dec 4 '16 at 9:48
  • This wasn't standard on my macOS (10.13). I've always missed this functionality! – forthrin Mar 30 at 11:02

protected by Anthon Apr 11 '16 at 22:31

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.