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

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?

Upvotes: 282

Views: 87083

Answers (7)

Eyad Ebrahim
Eyad Ebrahim

Reputation: 991

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

Upvotes: 18

Lyubomir
Lyubomir

Reputation: 301

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'"

Upvotes: 0

User
User

Reputation: 65931

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

Upvotes: 6

liar666
liar666

Reputation: 91

Another solution is to use:

history | grep <searched expression>

Upvotes: 9

Martin Dvorak
Martin Dvorak

Reputation: 812

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

Upvotes: 30

Eric Burghard
Eric Burghard

Reputation: 469

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

Upvotes: 46

hlovdal
hlovdal

Reputation: 28180

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

Upvotes: 441

Related Questions