I'm trying to figure out how autocompletion works. I read this and the whole bash reference on complete, compgen and compopt, but this doesn't tell me how they really work together.
I'm trying to create an auto-completion for a command that takes a verb (from a rather small set) followed by the name of an existing file. So far I have this:
verbs=(upload download delete)
function ac_complete {
printf "COMP_WORDS[%d/%d]: " ${COMP_CWORD} ${#COMP_WORDS[@]}
printf "[%s] " "${COMP_WORDS[@]}"
printf "\n"
if [[ COMP_CWORD -eq 1 ]]
then
COMPREPLY=( "${verbs[@]}" )
else
compopt -o default
fi
}
complete -F ac_complete testit
but it doesn't work, I never see bash completing the verb.
- How are possible completions returned?
- What should be returned? Only those still valid at that point (ie, after a "d", should the set be "upload"/"download"/"delete" or only "download"/"delete" (if so there is a quick way to apply that restriction?)?
The completion for the file works, but I'd like to restrict it to few filetypes...
bash