Search code examples
bashshellbash-completion

Bash completion - how to get rid of unneeded tab presses?


I use

cur="${COMP_WORDS[COMP_CWORD]}" 

opts=`sqlite3 test.db "${QUERY[COMP_CWORD]}"`

SAVEIFS="$IFS"

IFS=$'\n'

COMPREPLY=( $(compgen -S"'" -P"'" -W "${opts}" $cur) )

IFS="$SAVEIFS"

to get possible variants from the database and complete them with TAB. As long as these variants could contain spaces, it is convinient to autoquote them using ' as prefix and suffix, so when I press A, B, TAB and there is only one variant with AB prefix, then I get something like 'ABC DEF'.

But the problem is that if there are many variants then after A, B, TAB I get 'AB, then I press TAB once again and it is NOP, and only at the third TAB press I get possible completions.

Is there a way to reduce TAB pressings to one or at least two?


Solution

  • You can try:

    bind 'set show-all-if-ambiguous on'
    

    From man bash:

       show-all-if-ambiguous (Off)
              This alters the default behavior of the completion functions.  If set 
              to on, words which have more than one possible completion cause the 
              matches to be listed immediately instead of ringing the bell.