Bash completion without spaces.

I am working on completion for a command that takes an argument of type one: two: three.

In simple terms, I want ":" to be treated as the default space character. Is there an easy way to do this that I am missing?

I found that ':' is in COMP_WORDBREAKS, but that the character in COMP_WORDBREAKS is also treated as words.

So, if on the command line:

cmd one:tw[TAB] 

COMP_CWORD will be 3, and COMP_WORDS [COMP_CWORD-1] will be ::

For comparison, if on the command line:

 cmd one tw[TAB] 

COMP_CWORD will be 2, and COMP_WORDS [COMP_CWORD-1] will be "one"

The worst part is that if you press [TAB] immediately after the separator ':', it acts basically like a space:

 cmd one:[TAB] 

Now COMP_CWORD will be 2, and COMP_WORDS [COMP_CWORD-1] will be "one."

I can easily enough analyze the command line from COMP_LINE, but it’s better to find a way to just make ":" act like "" in my custom add-on. Is it possible?

+4
source share
1 answer

First take your own solution to analyze. Love to know if there is a better way:

 parms=$(echo "$COMP_LINE" | cut -d ' ' -f 2) vals="${parms}XYZZY" IFS=$":" words=( $vals ) unset IFS count=${#words[@]} cur="${words[$count-1]%%XYZZY}" 
0
source

All Articles