265

I am trying to search my bash history similarly as with CTRL-r, but to forward direction.

It has been a pain for me, when I just hit once too often CTRL-r, to find the previous command again.

How can you forward search your Bash history similarly as in reverse searching?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • Anyone have a solution for Git Bash in Windows? – Garret Wilson Mar 26 '23 at 16:59
  • 1
    @GarretWilson: The accepted answer works for me in Git Bash (setting `stty -ixon`). – marco Apr 03 '23 at 12:00
  • Thanks for mentioning this, @marco . I just now tried it again, and it does seem to work. Maybe the first time I tried it I had put something in the wrong state by using `Ctrl+S` before trying the command, or by some other key combination, or who knows what. – Garret Wilson Apr 05 '23 at 13:12

7 Answers7

409

You can search forward as well. From the bash info manual, "8.2.5 Searching for Commands in the History":

To search backward in the history for a particular string, type C-r. Typing C-s searches forward through the history.

The problem with Ctrl-S however is that sometimes collides with XON/XOFF flow control (in Konsole for instance). The searching is a readline feature however, and you should be able to bind it to some other key. Update: Simpler and better is just to disable XON/XOFF by running

stty -ixon
idbrii
  • 10,975
  • 5
  • 66
  • 107
hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • 1
    It seems to collide with XON/XOFF all the time for me, since I cannot get Ctrl-S working. How can you bind the search to other key? – Léo Léopold Hertz 준영 Apr 26 '09 at 23:24
  • 4
    This is brilliant, but I add stty -ixon to my .profile and it doesn't seem to work for new tabs. Any ideas on how to make this work? I'd love XON/XOFF to be disabled by default. – John Gallagher Sep 20 '11 at 09:10
  • 14
    In case anyone else has the same issue I did - if you add this to .profile it doesn't take effect. It's only when you add it to the .bash_profile that the magic happens! Thanks for a great tip - this had been driving me mad for years. – John Gallagher Sep 20 '11 at 09:24
  • 13
    @JohnGallagher Important to note is that `.bash_profile` and `.profile` are only sourced for log-in shells. I would put this in `.bashrc` (which I source from `.profile`). – Victor Zamanian Jan 26 '12 at 11:51
  • 21
    `[[ $- == *i* ]] && stty -ixon` can be used to avoid the problem [described here](http://stackoverflow.com/questions/24623021/) – mMontu Aug 19 '14 at 19:53
  • 3
    In case you're using PuTTY and can't/don't want to maintain `.bash_profile` on every machine you connect to, [this answer on superuser](http://superuser.com/a/376881/164453) works a treat. – fazy Oct 08 '14 at 15:44
  • 1
    Just to be sure. Does anyone have a scenario where you would need flow control? – BakaKuna Jul 23 '15 at 19:39
  • 1
    @BakaKuna If you communicate with some several decades old museum computer hardware like a terminal screen or printer, then using xon/xoff might be useful. For everything else, you want it disabled. – hlovdal Jul 24 '15 at 00:19
  • Still doesn't work for me on lxterminal even after running `stty -ixon`? – xji Apr 02 '18 at 12:54
  • I had to type two times `C-s` to move forward could it be done with single `C-s`? – alper Jun 28 '22 at 18:08
46

The best trick IMHO is enabling with pgup and pgdown. just put that in your ~/.inputrc

"\e[5~": history-search-forward
"\e[6~": history-search-backward

logout/login, type the first letters and then pgup or pgdown to search throughout history

ctrl-R search all lines containing words, whereas history-search-forward search lines beginning with words

Eric Burghard
  • 469
  • 4
  • 2
  • 2
    You can also uncomment these 2 lines in `/etc/inputrc` (e.g. in Ubuntu). – falconepl Jun 28 '14 at 11:47
  • 2
    I prefer to bind this to up and down arrow: "\e[A": history-search-backward and "\e[B": history-search-forward – shmup Oct 03 '17 at 13:39
  • What if this has no effect in bash? – Soerendip May 14 '20 at 23:18
  • @Sören This works in bash (tested version 4.4.20). Please ensure you put this in either `~/.inputrc` or `/etc/inputrc`. This will not work if placed in any of the `bashrc` files because it's not a command, it's a configuration option. – aggregate1166877 Jul 05 '21 at 05:24
29

You may want to try https://github.com/dvorka/hstr which allows for "suggest box style" filtering of Bash history with (optional) metrics based ordering i.e. it is much more efficient and faster in both forward and backward directions:

enter image description here

It can be easily bound to Ctrl-r and/or Ctrl-s

Martin Dvorak
  • 790
  • 8
  • 15
  • 6
    I'm in love. Quick instructions to install on Ubuntu: `sudo add-apt-repository ppa:ultradvorka/ppa; sudo apt-get update; sudo apt-get install hh; hh --show-configuration >> ~/.bashrc;` – CivFan Aug 26 '15 at 23:04
17

I usually press ESC in terminal, and then the >. It resets at least and then you could try press less too often CTRL+R.

Eyad Ebrahim
  • 971
  • 1
  • 8
  • 21
7

Another solution is to use:

history | grep <searched expression>

liar666
  • 71
  • 1
  • 1
4

As many have experienced, ctrl+s freezes (and ctrl+q unfreezes) the terminal because of software flow control (XON/XOFF flow control) and you can disable it as mentioned in the accepted answer.

Although I can't say I've really intentionally used the feature, I do want the option to be able to pause a fast moving stream of terminal text, so I didn't want to completely disable it.

So instead of turning it off, I rebound the xoff function by placing the following in my .bashrc

stty stop '^P'

Which binds xoff to ctrl+p (and ctrl+q still unfreezes). I used "p" for "pause" and this does obscure the bash previous command function previous-history. Personally I always use the up arrow key for that so it doesn't matter to me, but you could choose a different key.

This automatically frees up ctrl+s for forward-search-history

User
  • 62,498
  • 72
  • 186
  • 247
0

For KDE's terminal app (Konsole), you can disable flow control with XON/XOFF from settings according to THIS answer:

"in Konsole you can disable this feature, by going to Settings -> Configure Profile -> Choose current profile -> Edit Profile -> Advanced Tab and disable 'Enable flow control using Ctrl+S and Ctrl+Q'"

Lyubomir
  • 140
  • 5
  • 13