zsh:键入命令Y时,对命令X使用补全 [英] zsh: use completions for command X when I type command Y
问题描述
在zsh中,我有一个名为g
的函数,其功能如下:
In zsh, I have a function called g
which acts like this:
- 没有参数,请调用
git status
- 具有一个或多个参数,将所有给定参数委托给git-即调用
git $@
- with no arguments, call
git status
- with one or more arguments, delegate to git with all given arguments - i.e. call
git $@
我希望g
的制表符补全与git
的制表符完全相同.我可以使用alias g=git
实现此功能,但是默认情况下(上面的第一点)不允许我调用status
.
I would like the tab completions for g
to be exactly the same as for git
. I can achieve this with alias g=git
, but that doesn't allow me to call status
by default (the first point above).
如何委派git
的完成?
在bash中,我只是做了complete -F _git g
,它重新使用了git的完成功能.使用zsh,git的完成看起来要复杂得多,而且我无法找到类似的解决方案.
In bash, I simply did complete -F _git g
which re-uses git's completion function. With zsh, git's completion looks much more complex, and I wan't able to find a similar solution.
我猜zsh中有一些功能说假装我键入了命令[x],您要完成什么操作?".如果我知道那是什么,那么使用函数委托它应该足够简单.但是我在手册中没有找到这样的功能.
I'd guess there's some function in zsh to say "pretend I typed command [x], what would you complete it to?". If I knew what that was, it should be simple enough to use a function to delegate to it. But I've found no such function in the manuals.
推荐答案
功能
compdef
可用于将现有完成功能与新命令关联.例如
The function
compdef
can be used to associate existing completion functions with new commands. For example,
compdef _pids foo
但是修改它(_git
是 git 的常用完成函数)并没有为我带来任何工作效果(即使在自动加载_git
之后):
But adapting it (_git
is the usual completion function for git) did not produce a working result for me (even after _git
had been autoloaded):
compdef _git g
尽管如此,我还是可以通过_dispatch
使它工作:
I was able to get it to work via _dispatch
though:
compdef '_dispatch git git' g
这篇关于zsh:键入命令Y时,对命令X使用补全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!