pre-fill bash/readline incremental search query?

我怕爱的太早我们不能终老 提交于 2019-12-12 04:33:02

问题


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"'

but that just searches for the exact (unexpanded) $(echo "$FOO").


回答1:


readline doesn't support evaluating shell expressions in macros, but there is a workaround. Introduce two auxiliary key sequence bindings such that

  1. The first one performs a forward search history operation with a fixed query string.
  2. The second one configures the first one, by setting the said fixed query string to the value of a bash variable or the output of some bash function.

Your desired key sequence must call 2 followed by 1.

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"'


来源:https://stackoverflow.com/questions/40149518/pre-fill-bash-readline-incremental-search-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!