Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Keep hitting Ctrl+R. – Runium Apr 24 '13 at 2:10
@Sukminder You don't consider that an answer? – Hauke Laging Apr 24 '13 at 2:25
@HaukeLaging: Yes, perhaps ;) if I read the Q correctly. – Runium Apr 24 '13 at 2:32
@Sukminder Then you should copy it into an answer, enabling yourself to get the bravely earned upvotes and this question to get closed (as solved). – Hauke Laging Apr 24 '13 at 2:33
up vote 151 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 ...

share|improve this answer
+1 -- FYI -- you can also search forward as per this StackOverflow answer. – Jordan Arseno Apr 24 '13 at 21:14
And use Ctrl+Shift+r for reverse scrolling if you happen to pass over. – wiswit Nov 1 '15 at 13:41
@wiswit CTRL+SHIFT+r doesn't work for me. – Maxim Suslov Apr 5 at 7:48
@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 Apr 21 at 17:03

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

command #useful


ctrl+r #useful

share|improve this answer
This is effing genius! – akuhn Jul 25 '15 at 7:20
Usually I do like this kind of tagging. #trg_bld #open_log – Makesh Jul 31 '15 at 5:50
+1 this is a really really neat trick. – hochl Aug 7 '15 at 8:59
The original hashtag. The bashtag, if you will. – musicin3d Feb 1 at 22:10

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.

share|improve this answer
This is brilliant and very handy. Thanks :) – Sachin Mar 8 at 11:22

protected by Anthon Apr 11 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.