jozxyqk
jozxyqk

Reputation: 17266

How can I read partially entered text from bash?

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.

Upvotes: 4

Views: 89

Answers (1)

jozxyqk
jozxyqk

Reputation: 17266

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.

Upvotes: 3

Related Questions