unhammer
unhammer

Reputation: 4681

pre-fill bash/readline incremental search query?

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").

Upvotes: 0

Views: 77

Answers (1)

Leon
Leon

Reputation: 32474

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

Upvotes: 1

Related Questions