zsh:键入命令Y时,对命令X使用补全 [英] zsh: use completions for command X when I type command Y

查看:73
本文介绍了zsh:键入命令Y时,对命令X使用补全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

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