bash 中的 Control-r reverse-i-search:如何在 Cygwin 中“重置”搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/549810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Control-r reverse-i-search in bash: how do you "reset" the search in Cygwin?
提问by dreftymac
Question:How do you tell Ctrl+rreverse-i-search to "reset itself" and start searching from the bottom of your history every time?
问题:您如何告诉Ctrl+ rreverse-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 Escand re-invoke Ctrl+ra 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.
- The alternate question does not deal with how to resetthe search to its initial state (instead it deals with simply going backward in search as well as forward).
- 另一个问题不涉及 Cygwin。
- 备用问题不涉及如何将搜索重置为其初始状态(相反,它仅涉及在搜索中向后和向前)。
采纳答案by Tom Alsberg
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 时的默认设置,但 bashreadline
用于输入,默认情况下它接受 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
。
回答by hayalci
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 的提醒。
回答by Danke Xie
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 保持“恢复”)。
$ stty STOP ^w
$ 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
回答by Philip Reynolds
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 指令。