在 Prezto ZSH 中为 git 函数自定义补全

Customizing completion in Prezto ZSH for git function

我最近从 bash 切换到 zsh 并使用 Prezto 进行基本设置。很长一段时间以来,我一直在使用这个功能:

function g() {
  if [[ $# > 0 ]]; then
    git $@
  else
    git status
  fi
}

这允许我将 g 用于 git statusg <command> 用于任何其他 git 操作。

Prezto git 模块有一个覆盖我函数的别名 alias g='git'

所以我将其注释掉并且我的功能按预期工作。但是,当我需要完成制表符时,问题就来了。

例如:

到目前为止,我无法将我的函数与 git 完成模块结合使用,这样我就可以做 g checkout aw-<tab> 之类的事情并让它显示分支选项。

我在 bash 中运行良好,希望能够在 zsh 中具有类似的功能。

提前致谢!

将此添加到您的 ~/.zshrc

compdef g=git

正如解释的那样here