如何在 zsh 中獲得 bash 風格的自動完成功能(對於 git 命令)

如何在 zsh 中獲得 bash 風格的自動完成功能(對於 git 命令)

我切換到桀騁並發現它比巴什,但我有一個問題哦我的zsh的(以下簡稱“奧姆茲") 自動完成功能。

當我輸入時git chec,OMZ 會將其完成git check;在巴什它完成到git checkout.我也有遠程分支的問題: ori已完成 tooriGorigto origin, in巴什它完成到origin/

我和其他人也有同樣的問題git命令如checkout.

我認為問題是別名由插件添加。我不使用它們,因為我不想習慣它們,但它們破壞了我的自動完成功能。

有什麼辦法可以得到巴什自動完成(我知道 bash 中沒有內建的自動完成功能)桀騁?或是破解 OMZ 插件的方法,使其不會在每次更新時崩潰。

答案1

完成以及它來自哪裡可能會相當令人困惑。以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

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

其中包含文件,例如

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

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

為了讓git co你的新git checkout- oh-my-zsh 插件知道這些別名,並且接下來仍然會完成分支和標籤名稱!

相關內容