整个问题已讨论完毕here https://github.com/DannyBen/alf/issues/39 and here https://github.com/nvm-sh/nvm/issues/2489。由于参与这些讨论的人都没有 100% 确定这个问题,因此我在此寻求帮助。 (为了完整起见,我将从头开始。)
假设我们有两个脚本(源自~/.zshrc
)为 ZSH 设置一些完成逻辑。现在根据我所了解到的,在脚本中的某个时刻您需要调用compinit
and bashcompinit
像这样(从NVM完成脚本复制)
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
显然,根据ZSH手册,bashcompinit
必须在之后调用compinit
,(不确定是否相关)。现在的问题是,当第二个脚本调用时compinit
,来自第一个脚本的逻辑消失了(即第一个脚本的完成不可用)。重现此问题的一个简单片段是(复制自here https://github.com/DannyBen/alf/issues/39):
complete -W "hello world" one
one <tab> # to see autocomplete working
compinit
one <tab> # to see autocomplete NOT working
有人提出(here https://github.com/DannyBen/alf/issues/39)类似于下面的内容来解决问题(通过检查是否compinit
在调用之前已经被调用):
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
另一个想法可能是打电话compinit
and bashcompinit
不在自定义完成脚本中,而是在~/.zshrc
(这会损害 NVM 等工具的自动化安装过程)。
我想知道一般情况下设置完成的正确方法是什么(或者特别是关于调用compinit
).
Thanks.