Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I use compgen when $COMP_WORD equals 1, now I want to restore default completion when otherwise,

Right now I have,

_rr()
{
    case $COMP_CWORD in
        1)
            COMPREPLY=($(compgen -W "A B" "${COMP_WORDS[$COMP_CWORD]}"))
            ;;
    esac
}

complete -F _rr rr

Now when $COMP_CWORD is not 1, I got no completion for files / directories, that's the default completion I needed.

Also I need to enable completion only when ${COMP_WORDS[$COMP_CWORD - 1]} is A in this minimal case.

share|improve this question

1 Answer 1

up vote 2 down vote accepted
+100

I'll assume that in your real-world case you want to do other things too, and so continue to use case constructs (although in this minimal case, if constructs would have been sufficient):

_rr()
{
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD - 1]}"

    case "${COMP_CWORD}" in
        1)
            COMPREPLY=($(compgen -W "A B" "${cur}"))
            ;;
    esac

    case "${prev}" in
        A)
            COMPREPLY=($(compgen -f "${cur}"))
            ;;
    esac
}

complete -F _rr rr

Documentation of the -f option to compgen is available in the Bash Reference Manual (scroll down to file, under the -A action subsection of the description of the complete builtin).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.