问题
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
- The first one performs a forward search history operation with a fixed query string.
- 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