4

When I enter some text and then Ctrl+R, the partially entered text appears in the reverse search prompt:

> hello[Ctrl+R]
(reverse-i-search)`': hello

I'm looking to write a replacement for reverse search. The shortcut can be re-bound to run a different program, for example:

bind -x '"\C-R":"echo test"'
> hello[Ctrl+R]
test
> hello

How can I access the partially entered command/text hello from a program I bind Ctrl+R to?

Tab completion sets COMP_WORDS, COMP_LINE etc. I'm looking for something similar, ideally directly accessible by a C/C++ executable.

Amir
  • 37
  • 14
jozxyqk
  • 16,424
  • 12
  • 91
  • 180

1 Answers1

3

Found what I'm after here: https://unix.stackexchange.com/a/82716/54030

The environment variable READLINE_LINE can be read and even modified:

bind -x '"\C-R":"echo cmd=$READLINE_LINE"'
> hello[Ctrl+R]
cmd=hello

bind -x '"\C-R":"READLINE_LINE=replaced; READLINE_POINT=8"'
> hello[Ctrl+R]
... becomes
> replaced

READLINE_POINT is used to set the cursor position.

jozxyqk
  • 16,424
  • 12
  • 91
  • 180