如何在 zsh 中获得 bash 风格的自动补全,(对于 git 命令)

Hun*_*nsu 5 bash zsh shell git autocomplete

我切换到zsh并发现它比bash更好,但是我对oh-my-zsh(以下称为“ OMZ ”)自动完成功能有问题。

当我输入时git chec,OMZ 将完成输入git check;在bash 中,它完成到git checkout. 我还与远程分支机构的问题: ori完成oriG,并origorigin庆典它被完成origin/

我对其他git命令(如.gitignore )也有同样的问题checkout

我认为问题在于插件添加的别名。我不使用它们,因为我不想习惯它们,但它们会破坏我的自动完成功能。

有没有办法在zsh 中获得bash自动完成功能(我知道bash中没有内置的自动完成功能)?或者一种破解 OMZ 插件的方法,这样它就不会在每次更新时中断。

bar*_*baz 5

完成以及它来自哪里可能会相当令人困惑。以Ubuntu 14.04系统为例:

$ dpkg -L zsh-common | grep git
/usr/share/zsh/functions/Completion/Debian/_git-buildpackage
/usr/share/zsh/functions/Completion/Unix/_stgit
/usr/share/zsh/functions/Completion/Unix/_git
/usr/share/zsh/functions/Completion/Unix/_topgit
/usr/share/zsh/functions/VCS_Info/Backends/VCS_INFO_get_data_git
/usr/share/zsh/functions/VCS_Info/Backends/VCS_INFO_detect_git
/usr/share/zsh/functions/Misc/run-help-git
Run Code Online (Sandbox Code Playgroud)

zsh-common软件包提供了 git 补全功能。另一方面,该git软件包还附带 bash 和 zsh 的完成文件:

$ dpkg -L git | grep compl
/etc/bash_completion.d
/etc/bash_completion.d/git-prompt
/usr/share/bash-completion
/usr/share/bash-completion/completions
/usr/share/bash-completion/completions/git
/usr/share/bash-completion/completions/gitk
Run Code Online (Sandbox Code Playgroud)

其中包含文件,例如

$ head -n 5 /usr/share/bash-completion/completions/gitk
# bash/zsh completion support for core Git.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
Run Code Online (Sandbox Code Playgroud)

git 包甚至提供了一个可以启用的 git 感知提示,所有这些都不需要花哨的插件,例如 oh-my-zsh。

总而言之,git 子命令补全可以来自您的 shell (zsh):

https://github.com/zsh-users/zsh/blob/master/Completion/Unix/Command/_git

来自 git

https://github.com/git/git/tree/master/contrib/completion

或来自 oh-my-zsh 等插件。

回到你的问题:git chec你所描述的完成的旧行为实际上是有问题的。chec仍然不明确,正确的完成脚本不应将其完成checkout,因为有多个以 开头的子命令chec。如果您想要这种行为,请找出您之前使用过的众多完成脚本中的哪一个,禁用 oh-my-zsh git 插件并继续使用旧的完成脚本。

或者,我建议设置一个别名并习惯它。您可以使用

git config --global alias.co checkout
Run Code Online (Sandbox Code Playgroud)

为了让git co你的新git checkout- oh-my-zsh 插件知道这些别名,并且接下来仍然会完成分支和标签名称!