如何添加自定义git命令到zsh完成? [英] How to add custom git command to zsh completion?

查看:264
本文介绍了如何添加自定义git命令到zsh完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些有关zsh完成的指南,但是我仍然感到困惑.在我们的开发环境中,我们有一个名为git new-branch的自定义Git命令.我希望zsh在输入git ne Tab 后为我自动完成它.我该怎么办?

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 Tab. How can I do that?

推荐答案

默认git完成

假设您有自己的git子命令(git将运行程序git-foo 当您运行"git foo"时,您希望"git f"完成该子项 命令名称.您可以将该子命令告知完成 通过用户命令样式:

Say you got your own git sub-commands (git will run a program `git-foo' when you run "git foo") and you want "git f" to complete that sub commands name for you. You can make that sub-command known to the completion via the user-command style:

% 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完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆