zsh: use completions for command X when I type command Y

白昼怎懂夜的黑 提交于 2019-12-03 09:36:17

问题


In zsh, I have a function called g which acts like this:

  • with no arguments, call git status
  • with one or more arguments, delegate to git with all given arguments - i.e. call git $@

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).

How can I delegate to the completion for 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.

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.


回答1:


The documentation for compdef says this:

The function compdef can be used to associate existing completion functions with new commands. For example,

compdef _pids foo

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

I was able to get it to work via _dispatch though:

compdef '_dispatch git git' g


来源:https://stackoverflow.com/questions/4221239/zsh-use-completions-for-command-x-when-i-type-command-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!