1

I'd like to provide proposals for an argument that has to be quoted just like this:

$ cmdx 1starg 'argtwoa %1 %x argtwob'

The interaction should go like this (expectation):

$ cmdx 1starg '[TAB TAB]
argtwoa argtwoc %1
$ cmdx 1starg 'argtwob[TAB TAB]
argtwoa argtwoc %1
$ cmdx 1starg 'argtwob 

However, if I've typed the first word (one of the proposed) as the second argument, Bash does not give more examples but closes the opened quotation:

$ cmdx 1starg 'argtwob' 

My completion function looks like this so far:

function _complete() {
    local cur prev args
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # echo "## prev=|$prev| cur=|$cur| words=[$COMP_CWORDS[@]] word=|${COMP_CWORD}| ##"

    if [ ${COMP_CWORD} -eq 1 ]; then
        args="1starg"

        COMPREPLY=( $(compgen -W "${args}" -- ${cur}) )
        return 0
    elif [ ${COMP_CWORD} -ge 1 ]; then
        args="argtwoa argtwob argtwoc %1"

        if [[ "${cur}" =~ ^\'[^[:space:]]+[[:space:]]$ ]]; then
            args="%1"
        fi

        COMPREPLY=( $(compgen -W "${args}" -- ${cur}) )

        # echo "####|${COMREPLY}|#|${args}|####"
        return 0
    fi
}

complete -F _complete cmdx

When I uncomment the second debug-echo and press tab after the second argument has been opened with a singlequote and the first word and a space and then press TAB I expect "%" to be printed as a proposal. The debug output confirms, that my code is correct until compgen runs:

$ cmdx 1starg 'argtwoa ####||#|%1|####

I suspect, it's not possible to do what I want as the resulting args value is correct but not the COMPREPLY value (debugged using echo).

The same should work for doubleqoutes too (I know, that I have to extend the code).

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
  • Have you looked at [Properly handling spaces and quotes in bash completion](https://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion)? – Michael Kropat Mar 02 '14 at 16:06
  • To whoever: what's that for a manner voting for close with reason "unclear what the TO is asking" instead of leaving a comment or just moving on. – try-catch-finally Mar 03 '14 at 20:44

0 Answers0