Unable to forward search Bash history similarly as with CTRL-r
Asked Answered
B

7

273

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?

Burbage answered 26/4, 2009 at 22:53 Comment(3)
Anyone have a solution for Git Bash in Windows?Hagy
@GarretWilson: The accepted answer works for me in Git Bash (setting stty -ixon).Tallou
Thanks for mentioning this, @Tallou . 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.Hagy
O
429

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
Odilia answered 26/4, 2009 at 23:9 Comment(13)
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?Icono
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.Gyimah
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.Gyimah
@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).Pourpoint
[[ $- == *i* ]] && stty -ixon can be used to avoid the problem described hereCannice
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 works a treat.Giltzow
Just to be sure. Does anyone have a scenario where you would need flow control?Borax
@Borax 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.Odilia
Still doesn't work for me on lxterminal even after running stty -ixon?Mcglothlin
I had to type two times C-s to move forward could it be done with single C-s?Anchovy
Running stty ixon is not fixing the problem for me in zsh; anyone have any ideas?Manolo
Answering my own comment: for zsh, per man zshle, adding the two lines bindkey '^r' history-incremental-search-backward and bindkey '^s' history-incremental-search-forward to ~/.zshrc worked (and of course running source ~/.zshrc after editing to source changes). It does take 2 invocations of Ctrl+S to start going forward again after going backwards with Ctrl+R.Manolo
I tried to upvote, but it said I had already upvoted 🤦‍♂️ I'll write it on by laptop with permanent marker this time ...Coagulate
K
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

Kermis answered 15/3, 2013 at 11:10 Comment(4)
You can also uncomment these 2 lines in /etc/inputrc (e.g. in Ubuntu).Squarely
I prefer to bind this to up and down arrow: "\e[A": history-search-backward and "\e[B": history-search-forwardZipangu
What if this has no effect in bash?Amontillado
@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.Tatro
M
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

Menam answered 29/5, 2015 at 5:15 Comment(1)
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;Outroar
A
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.

Amianthus answered 7/2, 2013 at 15:1 Comment(2)
s/click/press/ Also, you need to press escape twice (first time to escape from the backwards search).Piscary
This is such underrated advice if one has set HISTSIZE and the like to -1.....Neurath
N
8

Another solution is to use:

history | grep <searched expression>

Nonconductor answered 30/9, 2020 at 8:32 Comment(0)
S
5

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

Socage answered 28/7, 2021 at 22:23 Comment(0)
K
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'"

Karlkarla answered 18/6, 2022 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.