815

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?

Neuron
  • 121
Village
  • 5,035
  • 15
  • 50
  • 84

4 Answers4

1086

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

E.g.:

  • 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.)

Runium
  • 28,811
  • 29
    +1 -- FYI -- you can also search forward as per this StackOverflow answer. – Jordan Arsenault Apr 24 '13 at 21:14
  • 17
    And use Ctrl+Shift+r for reverse scrolling if you happen to pass over. – wiswit Nov 01 '15 at 13:41
  • 55
    @wiswit CTRL+SHIFT+r doesn't work for me. – Maxim Suslov Apr 05 '16 at 07:48
  • 16
    @MaximSuslov See this question: http://stackoverflow.com/questions/791765/unable-to-forward-search-bash-history-similarly-as-with-ctrl-r/791800#791800 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
  • 16
    @JordanArseno I took "search forward" to mean "search for commands I have not yet typed" – Josh Johnson Jul 28 '16 at 22:10
  • 5
    I just tried STRL+s and it froze my terminal. Worth mentioning that you can unfreeze it with CTRL+q as per https://unix.stackexchange.com/a/12108/265674 – user2740 Feb 05 '19 at 13:13
  • 1
    Why did I not take the 30 seconds to look this up until now? I've been grepping my command history for years. Thank you so much. – Nick Woodhams Apr 12 '19 at 02:18
  • 2
    #ThingsThatIShouldProbablyHaveLookedUp20YearsAgo – DrMcCleod Feb 11 '20 at 13:57
  • 2
    stty -ixon was the missing piece I've needed for 25 years to search forward. Can't believe it annoyed me for so long before I Googled it! :D – mhvelplund Feb 18 '21 at 07:09
  • btw: you have to type the ctrl-R first! if you type grep ctrl-R then it only shows the most recent command, and repeated presses of ctrl-R do nothing. there's probably a really good reason for this, but i consider it a bug. – Spongman Sep 23 '21 at 16:57
354

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

command #useful

Then

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.

Neuron
  • 121
Sathyam
  • 3,678
75

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.

l0b0
  • 51,350
  • 5
    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 07:11
  • You could also look at this post for more details on this answer: http://codeinthehole.com/writing/the-most-important-command-line-tip-incremental-history-searching-with-inputrc/ – Andrei Dec 04 '16 at 09:48
  • 2
    This wasn't standard on my macOS (10.13). I've always missed this functionality! – forthrin Mar 30 '18 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 Sep 19 '18 at 02:46
29

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:

Example

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).

saaj
  • 2,438