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, it 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?


4 Answers 4


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, but you may need to have set: stty -ixon (either by .bash_profile or manually) prior to that to disable the XON/XOFF feature which takes over Ctrl + S. If it happens anyway, use Ctrl + Q to re-enable screen output (More details here.)

  • 29
    +1 -- FYI -- you can also search forward as per this StackOverflow answer. Commented Apr 24, 2013 at 21:14
  • 17
    And use Ctrl+Shift+r for reverse scrolling if you happen to pass over.
    – wiswit
    Commented Nov 1, 2015 at 13:41
  • 59
    @wiswit CTRL+SHIFT+r doesn't work for me. Commented Apr 5, 2016 at 7:48
  • 16
    @MaximSuslov See this question: stackoverflow.com/questions/791765/… You can add [[ $- == *i* ]] && stty -ixon to your .bashrc and then CTRL+s will work as the reverse of CTRL+r
    – gla3dr
    Commented Apr 21, 2016 at 17:03
  • 17
    @JordanArseno I took "search forward" to mean "search for commands I have not yet typed" Commented Jul 28, 2016 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.

  • 11
    Usually I do like this kind of tagging. #trg_bld #open_log
    – Makesh
    Commented Jul 31, 2015 at 5:50
  • 2
    where to add this tag? Commented Nov 3, 2016 at 13:46
  • 8
    @SudipBhandari just after your command. # starts a comment. E.g. ls -lah #useful
    – Andrei
    Commented Dec 4, 2016 at 9:27
  • 39
    Unix had hashtags before it was cool :)))
    – Jencel
    Commented Oct 11, 2019 at 11:44
  • 6
    That's more a hack, and .bash_history gets flushed after a while. You'd be more comfortable using bash aliases
    – Nino Filiu
    Commented Dec 11, 2020 at 13:56

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.

  • 5
    This is also standard on OS X, so you don't need to create ~/.inputrc and add those two lines.
    – DASKAjA
    Commented Aug 12, 2016 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-* Commented Aug 16, 2016 at 7:11
  • You could also look at this post for more details on this answer: codeinthehole.com/writing/…
    – Andrei
    Commented Dec 4, 2016 at 9:48
  • 2
    This wasn't standard on my macOS (10.13). I've always missed this functionality!
    – forthrin
    Commented Mar 30, 2018 at 11:02
  • 1
    a good thing about this is that you can still use ctrl-p/ctrl-n for regular skimming through history
    – elig
    Commented Sep 19, 2018 at 2:46

There's a replacement for built-in Ctrl + R called hstr. It allows to search command history matching all search tokens at the same time (among other things), and cycle through result using arrow keys:


Here's is a demo screencast.

It can be installed on a Debian-family OS like:

add-apt-repository ppa:ultradvorka/ppa
apt-get update
apt-get install hstr

hstr --show-configuration >> ~/.bashrc

And then use Ctrl + R (after reopening the terminal).

  • 1
    This MUST be the accepted answer in my view. Thank you!
    – Nam G VU
    Commented Oct 27, 2020 at 0:13
  • Awesome, working!!! Commented Mar 1, 2022 at 12:14
  • 1
    Newer and a bit fancier: github.com/ellie/atuin
    – Caleb
    Commented Feb 13, 2023 at 22:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .