241

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
  • 6,866
  • 9
  • 49
  • 75
Léo Léopold Hertz 준영
  • 126,923
  • 172
  • 430
  • 675

6 Answers6

381

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,113
  • 5
  • 57
  • 100
hlovdal
  • 24,540
  • 10
  • 85
  • 154
  • 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
  • @hlovdal: Thank you very much! You saved my day :) – Léo Léopold Hertz 준영 Apr 27 '09 at 04:44
  • 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
  • 19
    `[[ $- == *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
45

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
  • 459
  • 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 – badteeth Oct 03 '17 at 13:39
  • What if this has no effect in bash? – Soren 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
  • 781
  • 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 click less too often CTRL+R.

Misha Akopov
  • 11,071
  • 26
  • 66
  • 81
Eyad Ebrahim
  • 961
  • 1
  • 8
  • 21
6

Another solution is to use:

history | grep <searched expression>

liar666
  • 61
  • 1
  • 1
2

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
  • 56,228
  • 70
  • 174
  • 240