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

1

10

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?

Feoff answered 18/1, 2012 at 21:12 Comment(0)
W
12

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.
Wages answered 18/1, 2012 at 22:1 Comment(2)
Perfect solution. This reduces TAB pressings to only one.Feoff
if not obvious: add set show-all-if-ambiguous on to ~/.inputrc to do this automatically.Puerility

© 2022 - 2024 — McMap. All rights reserved.