Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 761
gfxmonk Avatar asked Nov 19 '10 01:11

gfxmonk


People also ask

How do you use zsh completion?

When you hit the tab key, the system will complete the path to the Documents folder. At this point, you can add a character or two to get to a unique completion, and hit the tab key again. In zsh you can also hit the tab key repeatedly to cycle through the suggested completions.

Can I use bash completion in zsh?

for ZSH usersZsh can handle bash completions functions. The latest development version of zsh has a function bashcompinit, that when run will allow zsh to read bash completion specifications and functions.

Where do I put zsh autocomplete?

zsh-autocomplete adds real-time type-ahead autocompletion to Zsh. Find as you type, then press Tab to insert the top completion, Shift Tab to insert the bottom one, or ↓ / PgDn to select another completion.


1 Answers

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 
like image 128
Chris Johnsen Avatar answered Sep 23 '22 05:09

Chris Johnsen