zsh 中修改当前完成选项的 compopt 等效项是什么?

剑走偏*_*走偏风 5 linux bash shell zsh

我正在为 zsh 编写一个自动完成脚本,我想在某些条件下启用 nospace。bash 等效脚本如下所示

_my_completion(){
  if something; then
    compopt -o nospace
  fi
}

complete -F _my_completion <exec>
Run Code Online (Sandbox Code Playgroud)

如何在 zsh 中实现相同的目标?

Tre*_*iño 2

-S ''可以使用as参数来完成类似的效果compadd,如您所见:

_my_completion(){
  # Here you can access ${words[@]}, $BUFFER, $CURSOR, $NAME, $CURRENT...
  if something; then
    completions_array=(foo bar baz)

    # `-S ''` is the equivalent of `compopt -o nospace`
    compadd -S '' -a completions_array
  fi
}

compdef _my_completion <exec>
Run Code Online (Sandbox Code Playgroud)

如果您熟悉 bash 补全,您可以在这个加载 ZSH 中的 bash 补全的脚本中了解 zsh 补全的比较。