问题描述
我已经阅读了一些有关zsh完成的指南,但是我仍然感到困惑.在我们的开发环境中,我们有一个名为git new-branch
的自定义Git命令.我希望zsh在输入git ne
和后为我自动完成它.我该怎么办?
I've read a few guides on zsh completion, but I am still confused. In our development environment we have a custom Git command called git new-branch
. I'd like zsh to auto-complete it for me after typing just git ne
and a . How can I do that?
推荐答案
默认git完成是可扩展的:
% zstyle ':completion:*:*:git:*' user-commands foo:'description for foo'
`user-commands'是一种列表样式,因此您可以在其中添加任意数量的程序. :description部分是可选的,因此您可以从中添加所有git- *程序 您的$ path像这样:
`user-commands' is a list style, so you can add any number of programs there. The :description part is optional, so you could add all git-* programs from your $path like this:
% zstyle ':completion:*:*:git:*' user-commands ${${(M)${(k)commands}:#git-*}/git-/}
也就是说,添加就足够了
That is, it suffices to add
zstyle ':completion:*:*:git:*' user-commands new-branch:'custom new branch function'
到您的zshrc
.
如果您也想处理自定义命令的参数,那么使用自定义compdef文件是更好的解决方案.上面引用的文件还包含一些详细信息:只需创建定义git-<yourcommand>
函数的标准定义文件,默认的gitcomplete就会在需要时自动使用它.
If you would like to handle parameters to your custom command as well, then it is a better solution to use a custom compdef file. The file referenced above has some details on that as well: Just create a standard definition file defining a git-<yourcommand>
function, the default git completion will use it automatically when needed.
这篇关于如何添加自定义git命令到zsh完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!