Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

In the terminal, I can type CRTL+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. – Sukminder 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. – Sukminder 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

3 Answers 3

up vote 79 down vote accepted

If I understand the question correctly you should be able to cycle trough alternatives by keep 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 at 13:41

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 at 7:20
Usually I do like this kind of tagging. #trg_bld #open_log – Makesh Jul 31 at 5:50
+1 this is a really really neat trick. – hochl Aug 7 at 8:59

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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