预填充bash/readline增量搜索查询? [英] pre-fill bash/readline incremental search query?

查看:39
本文介绍了预填充bash/readline增量搜索查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用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

  1. 第一个使用固定的查询字符串执行正向搜索历史操作.
  2. 第二个通过将所述固定查询字符串设置为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆