Control-r reverse-i-search in bash: how do you “re

2019-01-31 02:33发布

Question: 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 re-start from the bottom again and work its way back up.

Update: I guess I should have mentioned I am using Cygwin on Windows, as none of the so-far mentioned solutions work.

Update: 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).

4条回答
唯我独甜
2楼-- · 2019-01-31 03:08

My bash works as you are expecting. Maybe hitting "ctrl+C" instead of "esc" can help.

Also, you can search forward using "ctrl+s"

edit: 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.

查看更多
等我变得足够好
3楼-- · 2019-01-31 03:13

I never tried making this the default when hitting Escape, 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 Escape key with >).

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

查看更多
可以哭但决不认输i
4楼-- · 2019-01-31 03:33

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
查看更多
仙女界的扛把子
5楼-- · 2019-01-31 03:33
M-> ... moves to end of history
M-< ... moves to start of history

Your left alt key is most likely your Meta key.

Man readline for more readline directives.

查看更多
登录 后发表回答