无需在 bash 中按 ctrl+r 即可触发反向搜索的替代方法
bash 中的反向搜索功能很有用,但它与大多数其他 bash 命令不同,它似乎绑定到键绑定 (Ctrl + R)。 用户如何使用别名或键入的命令来触发此功能?
The reverse-i-search facility in bash is useful, but it is unlike most other bash commands in that it seems to be bound to a keybinding (Ctrl + R). How can a user trigger this facility using an alias or typed-in command instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看“历史扩展>事件指示符”下的 bash 或历史记录 (3readline) 的手册页,您会看到执行此类操作的命令。
将调出包含“prof”的最新命令并立即执行。 如果要打印命令而不执行它,可以附加“:p”。
为了在命令行编辑命令,使用上面的示例,键入:(
不要按 Enter)并按 M-^(Meta -Caret - Alt+^ 或 Esc ^)
这些使用 bash 的扩展功能。 他们只获得最近的比赛。
如果您想编写脚本或别名,请查看 bash 手册页中的内置命令“fc”和“history”(-p 或 -s)。 还有“shopt -s histreedit”。
这是一个别名示例:(
这些是单引号内的反引号)。
您也可以使用其中一些命令执行一些很酷的“s/old/new”和其他操作。
If you look at the man pages for bash or history (3readline) under "History Expansion > Event Designators" you see commands for doing this type of thing.
would bring up the most recent command that contained "prof" and execute it immediately. If you want to print the command without executing it, you can append ":p"
In order to edit the command at the command line, using the example above, type:
(don't press enter) and press M-^ (Meta-Caret - Alt+^ or Esc ^)
These use the expansion facility of bash. They only get the most recent match.
If you'd like to write scripts or aliases, take a look in the bash man page at the builtin commands "fc" and "history" (-p or -s). Also "shopt -s histreedit".
Here's an example alias:
(those are backquotes just inside the single quotes).
You can do some cool "s/old/new" and other stuff with some of these commands, too.
相反,i-search 函数实际上是一个 readline 函数(reverse-search-history),而不是 bash 内置函数(man 内置或参见 bash 参考手册中的内置命令)。 据我所知,除了将 readline 函数绑定到键之外,没有其他方法可以调用它。
您可以使用“
bind -l -p
”查看与这些函数绑定的所有 readline 键。 可绑定 readline 命令 的列表可以是在参考手册中也可以找到。您始终可以使用
history
命令获取历史记录列表,并使用grep
查找您要查找的内容。 我自己发现这很有用:当我运行
hists some
时,我会得到与something
匹配的所有命令的列表。 我所要做的就是执行 !# 来运行命令。 例如:这并不完全是您正在寻找的东西,但它是我能做到的最接近的东西。
The reverse-i-search function is actually a readline function (reverse-search-history) and not a bash builtin function (man builtin or see builtin commands in the bash reference manual). To my knowledge there is no way to call a readline function outside of binding it to a key.
You can see all the readline key binding to these function using "
bind -l -p
". The list of bindable readline commands can be found in the reference manual as well.You can always use the
history
command to get a list of the history and usegrep
to find what you are looking for. Myself I find this to be useful:When I run
hists something
I get a list of all the commands that matchessomething
. All I have to do is then do !# to run the command. For example:It's not exactly what you are looking for but it's as close as I can make it.