Bash provides facilities to modify an entire line and change the position of the cursor with commands bound to bind -x through READLINE_LINE and READLINE_POINT variables:
$ cat test.sh
#!/bin/bash
autocompletefunc()
{
echo $READLINE_LINE >> test.out
READLINE_LINE='completely rewritten line & cursor is right here ->X<-'
READLINE_POINT=51
}
$ source test.sh
$ bind -x '"\t" : autocompletefunc'
$ smth<TAB> <== gets transformed into the below:
$ completely rewritten line & cursor is right here ->|X<-
(with | denoting the position of the cursor)
I did not find a way to modify the entire line (rather than just the currently-entered word) in an autocompletion function (registered with complete -F). Escape codes seem to just be printed (if only a single element is provided to COMPREPLY):
$ smth word<TAB>
$ smth ^[[2Kotherwisetheargumentcanentirelybereplaced
All the autocompletion variables are available "only in shell functions invoked by the programmable completion facilities". Is it possible to somehow combine the two (callback registered through complete -F and the one through bind -X) to simultaneously modify the line and list some suggestions, preferrably on <TAB><TAB>? Like this:
$ smth<TAB>
$ modified line=| further modified line
possiblevalue1
possiblevalue2
possiblevalue3