Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: _git Completion and custom commands



Benjamin R. Haskell <zsh@xxxxxxxxxx>:
[...]
>> The one issue I've found is that the _git completion function (as of  
>> 4.3.10 shipped with Debian testing) does not include custom commands  
>> (though it does include aliases).
[...]
> zstyle ':completion:*:git:*' user-commands ${${(k)commands[(I)git-*]}#git-}

Yes, this is explained at the top of the _git completion file.

Here's a little more:

The basic idea is that you can list your own git commands in the
`user-commands' style. And generally you're allowed to provide a
description for each command, too. So, say you've got a program
`git-foo', you can do this:

% zstyle ':completion:*:*:git:*' user-commands \
                     foo:'solves the question about the universe'

Now completing `git fo<tab>' results in a menu such as this (depending
on how compsys is configured, of course):

% % git fo<tab>
- git command -
foo           -- solves the question about the universe
for-each-ref  -- output information on each ref
format-patch  -- prepare patches for e-mail submission

Now, if you want smartness, when you try `git foo <tab>' you may
write a function `_git-foo()' and _git will pick it up. So, if
your `git-foo' program supports a few options (-f, -v and -q), a
completion function may look like this:

function _git-foo() {
    _arguments \
        '-f[force stuff]' \
        '-v[be verbose]'  \
        '-q[be quiet]'    && ret=0;
}

With this, typing `git foo -<tab>' results in this menu:

% git foo -<tab>
- option -
-f            -- force stuff
-q            -- be quiet
-v            -- be verbose


This can become as complicated as you like. :)

The `style' line Benjamin gave sets the `user-commands' style to a
list of all `git-*' commands zsh finds in $PATH. It's useful if you
got a *lot* of own scripts. Since zsh cannot guess a description for
the programs this way, commands added like that will be missing
descriptions.

Regards, Frank

PS: If the OP didn't configure compsys yet, then
    <http://zshwiki.org/home/examples/compquickstart> may serve him
    as a quickstart. I'll certainly help to get menus such as the
    ones I mentioned above.



Messages sorted by: Reverse Date, Date, Thread, Author