Control-r reverse-i-search in Bash: how do you "reset" the search in Cygwin?

BashCygwinReadline

Bash Problem Overview


How do you tell Ctrl + R reverse-i-search to "reset itself" and start searching from the bottom of your history every time?

Background: When using reverse-i-search in Bash, I always get stuck once it is finished searching up through the history and it cannot find any more matches. Sometimes I hit Esc and re-invoke Ctrl + R a second time, expecting it to start a fresh new search from the bottom of my history. However, the "pointer" still seems to be at the previous place it left off in my history.

The problem is, I usually do not want this behavior. If I hit Esc, and then re-invoke Ctrl + R, I would like that to indicate it should restart from the bottom again and work its way back up.

I am using Cygwin on Windows, as none of the so-far mentioned solutions work.


This question was marked as a potential duplicate question. This question is not a duplicate for the following reasons:

  • The alternate question does not deal with Cygwin.
  • The alternate question does not deal with how to reset the search to its initial state (instead it deals with simply going backward in search as well as forward).

Bash Solutions


Solution 1 - Bash

My Bash works as you are expecting. Maybe hitting Ctrl + C instead of Esc can help.

Also, you can search forward using Ctrl + S.

Ctrl + S works if it does not send a "stop" to your terminal, i.e., if "stty -a" gives you "-ixon". You can change it by "stty -ixon".

Thanks to @Phil for reminder.

Solution 2 - Bash

I never tried making this the default when hitting Esc, but Bash uses readline for input, which accepts Emacs-style keybindings by default, so you can go to the bottom using M-> (usually either by combining Meta/Alt and > or by following the Esc key with >).

If M-> does not work because your terminal does not let you enter that, try ^G (Ctrl and G simultaneously). That is the "cancel" stroke in Emacs and usually works with readline too.

Solution 3 - Bash

I got a confirmed answer to this question.

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. For example, the following will set pause to Ctrl + W (and keep "resume" with Ctrl + Q).

$ stty STOP ^w

Alternatively, you can also totally disable XON/XOFF (resume/pause) flow control characters by

$ stty -ixon -ixoff

This will also free up Ctrl + S. To re-enable pause/resume, you can do

$ stty ixon ixoff

Solution 4 - Bash

M-> ... moves to end of history
M-< ... moves to start of history

Your left Alt key is most likely your Meta key.

Use man readline for more readline directives.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondreftymacView Question on Stackoverflow
Solution 1 - BashhayalciView Answer on Stackoverflow
Solution 2 - BashTom AlsbergView Answer on Stackoverflow
Solution 3 - BashDanke XieView Answer on Stackoverflow
Solution 4 - BashPhilip ReynoldsView Answer on Stackoverflow