Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

search

People also ask

What does Ctrl R do in bash?

Ctrl+R – starts a reverse search, through the bash history, simply type characters that should be unique to the command you want to find in the history.

How do I undo Control R?

To reset Ctrl + R , the usual Emacs key Ctrl + G can do. If you want to reverse Ctrl + R by one step, instead of working your way up from the bottom again, you can use Ctrl + S . The trick is Ctrl + S is also used to pause the terminal. So you would need assign that to another key.

How do I scroll through bash history?

Up Arrow or Ctrl+P: Go to the previous command in your history. Press the key multiple times to walk backwards through the commands you've used. Down Arrow or Ctrl+N: Go to the next command in your history. Press the key multiple times to walk forwards through the commands you've used.

Why does bash history disappear?

This happens if the computer is "slow" while reading/writing parts of the history. Basically how it works is that your currently history is read, the file is deleted/moved away, and a new file is created, where all the history lines are written to.


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

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


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


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


Another solution is to use:

history | grep <searched expression>


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