The whole issue is discussed here and here. Since no one involved in those discussions was 100% sure about the issue, I'm asking for help here. (For the sake of completeness, I'll start from the beginning.)
Let's say we have two scripts (sourced in ~/.zshrc) that set up some completion logic for ZSH. Now based on what I learned, at some point in the script you need to call compinit and bashcompinit like this (copied from NVM completion script)
if [[ -n ${ZSH_VERSION-} ]]; then
autoload -U +X compinit && if [[ ${ZSH_DISABLE_COMPFIX-} = true ]]; then
compinit -u
else
compinit
fi
autoload -U +X bashcompinit && bashcompinit
fi
Apparently, according to ZSH manual, bashcompinit must be called after compinit, (not sure if it's relevant). Now the problem is, the moment the second script calls compinit, the logic coming from the first script is gone (i.e. no completion from the first script is available). A simple snippet to reproduce this is (copied from here):
complete -W "hello world" one
one <tab> # to see autocomplete working
compinit
one <tab> # to see autocomplete NOT working
Someone proposed (here) something like below to solve the issue (by checking if compinit is already called before calling it):
if [[ -n ${ZSH_VERSION-} ]]; then
if ! command -v compinit > /dev/null; then
autoload -U +X compinit && if [[ ${ZSH_DISABLE_COMPFIX-} = true ]]; then
compinit -u
else
compinit
fi
fi
autoload -U +X bashcompinit && bashcompinit
fi
Another idea could be to call compinit and bashcompinit not in the custom completion script, but in ~/.zshrc (which hurts the automated installation process for tools like NVM).
I'd like to know what is the correct way to set up completion in general (or specifically with respect to calling compinit).
Thanks.