预填充bash/readline增量搜索查询? [英] pre-fill bash/readline incremental search query?
问题描述
我可以使用bash bind
传递某个函数提供的字符串来预填充\C-s
查询吗?我希望我能做
Can I, with bash bind
, hand a string given by some function to pre-fill a \C-s
query? I was hoping I could do
bind '"\e\C-i": "\C-s$(echo "$FOO")\C-j"'
但这只是搜索确切的(未展开的)$(echo "$FOO")
.
but that just searches for the exact (unexpanded) $(echo "$FOO")
.
推荐答案
readline
不支持评估宏中的shell表达式,但是有一种解决方法.引入两个辅助键序列绑定,使得
readline
doesn't support evaluating shell expressions in macros, but there is a workaround. Introduce two auxiliary key sequence bindings such that
- 第一个使用固定的查询字符串执行正向搜索历史操作.
- 第二个通过将所述固定查询字符串设置为bash变量的值或某些bash函数的输出来配置第一个.
您想要的键序列必须先呼叫2,再呼叫1.
Your desired key sequence must call 2 followed by 1.
以下是使用辅助键序列"\e\C-o"
和"\e\C-p"
的上述想法的实际实现(如果您将其用于其他目的,请不要忘记将它们替换为未使用的键序列):>
The following is an actual implementation of the above idea, using auxiliary key sequences "\e\C-o"
and "\e\C-p"
(if you are using those for other purposes, don't forget to replace them with unused key sequences):
$ setup_fixedfwdsearchhistory_binding() { bind '"\e\C-o":"\C-s'"$FOO"'\C-j"'; }
$ bind -x '"\e\C-p": setup_fixedfwdsearchhistory_binding'
$ bind '"\e\C-i": "\e\C-p\e\C-o"'
这篇关于预填充bash/readline增量搜索查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!