在bash中使用Control-r反向搜索:如何在Cygwin中“重置”搜索?

时间:2022-03-09 00:17:20

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

问题:如何告诉Ctrl + r reverse-i-search“重置自己”并每次从历史记录的底部开始搜索?

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.

背景:在Bash中使用reverse-i-search时,一旦完成搜索历史记录并且找不到更多匹配项,我总是卡住。有时我点击Esc并再次重新调用Ctrl + r,期望它从我的历史记录底部开始一个全新的搜索。然而,“指针”似乎仍然存在于我历史上的前一个位置。

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.

问题是,我通常不希望这种行为。如果我点击Esc,然后重新调用Ctrl + r,我希望它表明它应该再次从底部重新启动并重新启动。

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

更新:我想我应该提到我在Windows上使用Cygwin,因为到目前为止提到的解决方案都没有。

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.
  • 替代问题不涉及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 个解决方案

#1


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

我在尝试使用Escape时没有尝试将其设置为默认值,但bash使用readline作为输入,默认情况下接受Emacs样式的键绑定,因此您可以使用M->(通常通过组合Meta / Alt和>或者使用>)跟随Escape键。

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.

如果M->不起作用,因为您的终端不允许您输入,请尝试^ G(同时控制和G)。这是Emacs中的“取消”笔划,通常也与readline一起使用。

#2


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

我的bash按照你的期望工作。也许点击“ctrl + C”而不是“esc”可以提供帮助。

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

此外,您可以使用“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.

编辑:如果ctrl + s没有向你的终端发送“停止”,即如果“stty -a”给你“-ixon”,则它可以工作。您可以通过“stty -ixon”进行更改。感谢@Phil提醒。

#3


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

要重置ctrl-r,通常的emacs键ctrl-g可以执行。如果你想将ctrl-r反向一步,而不是再次从底部向上,你可以使用ctrl-s。诀窍是ctrl-s也用于暂停终端。所以你需要将它分配给另一个键。例如,以下内容将暂停设置为ctrl-w(并使用ctrl-q保持“resume”)。

$ stty STOP ^w

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

或者,您也可以完全禁用XON / XOFF(恢复/暂停)流控制字符

$ stty -ixon -ixoff

This will also free-up ctrl-s. To re-enable pause/resume, you can do

这也将释放ctrl-s。要重新启用暂停/恢复,您可以这样做

$ stty ixon ixoff

#4


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

Your left alt key is most likely your Meta key.

你的左Alt键很可能是你的Meta键。

Man readline for more readline directives.

man readline更多readline指令。

#1


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

我在尝试使用Escape时没有尝试将其设置为默认值,但bash使用readline作为输入,默认情况下接受Emacs样式的键绑定,因此您可以使用M->(通常通过组合Meta / Alt和>或者使用>)跟随Escape键。

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.

如果M->不起作用,因为您的终端不允许您输入,请尝试^ G(同时控制和G)。这是Emacs中的“取消”笔划,通常也与readline一起使用。

#2


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

我的bash按照你的期望工作。也许点击“ctrl + C”而不是“esc”可以提供帮助。

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

此外,您可以使用“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.

编辑:如果ctrl + s没有向你的终端发送“停止”,即如果“stty -a”给你“-ixon”,则它可以工作。您可以通过“stty -ixon”进行更改。感谢@Phil提醒。

#3


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

要重置ctrl-r,通常的emacs键ctrl-g可以执行。如果你想将ctrl-r反向一步,而不是再次从底部向上,你可以使用ctrl-s。诀窍是ctrl-s也用于暂停终端。所以你需要将它分配给另一个键。例如,以下内容将暂停设置为ctrl-w(并使用ctrl-q保持“resume”)。

$ stty STOP ^w

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

或者,您也可以完全禁用XON / XOFF(恢复/暂停)流控制字符

$ stty -ixon -ixoff

This will also free-up ctrl-s. To re-enable pause/resume, you can do

这也将释放ctrl-s。要重新启用暂停/恢复,您可以这样做

$ stty ixon ixoff

#4


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

Your left alt key is most likely your Meta key.

你的左Alt键很可能是你的Meta键。

Man readline for more readline directives.

man readline更多readline指令。