zsh-users
 help / color / mirror / code / Atom feed
* _git Completion and custom commands
@ 2010-09-28  0:29 Conrad Irwin
  2010-09-28  4:06 ` Benjamin R. Haskell
  0 siblings, 1 reply; 6+ messages in thread
From: Conrad Irwin @ 2010-09-28  0:29 UTC (permalink / raw)
  To: zsh-users

Hi Zsh,

I finally got round to trying out zsh today, and wow is it cool — I
don't believe I've been stuck on bash for all this time :).

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

(For those of you unfamiliar with git, any executable in your path
with a name like git-* can be called as git * with no hyphen)

My question is then: how would I add my custom commands' names to the
list already completed by _git (without completely overriding or
modifying the system-wide configuration)?

Thanks for your time,

Conrad


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: _git Completion and custom commands
  2010-09-28  0:29 _git Completion and custom commands Conrad Irwin
@ 2010-09-28  4:06 ` Benjamin R. Haskell
  2010-09-28  5:28   ` Sebastian Stark
  2010-09-28  8:03   ` Frank Terbeck
  0 siblings, 2 replies; 6+ messages in thread
From: Benjamin R. Haskell @ 2010-09-28  4:06 UTC (permalink / raw)
  To: Conrad Irwin; +Cc: zsh-users

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1716 bytes --]

On Tue, 28 Sep 2010, Conrad Irwin wrote:

> Hi Zsh,
>
> I finally got round to trying out zsh today, and wow is it cool — I 
> don't believe I've been stuck on bash for all this time :).
>
> 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).
>
> (For those of you unfamiliar with git, any executable in your path 
> with a name like git-* can be called as git * with no hyphen)
>
> My question is then: how would I add my custom commands' names to the 
> list already completed by _git (without completely overriding or 
> modifying the system-wide configuration)?

This has bothered me for a while, too.  I think _git should include 
these by default.  But, it's pretty easy to add:

zstyle ':completion:*:git:*' user-commands ${${(k)commands[(I)git-*]}#git-}

(I've found _git to be very about what it allows you to override via 
zstyles)

Explanation of the '${${(k)commands[(I)git-*]}#git-}' portion:

$commands is a built-in associative array that maps basenames to their 
full paths.

E.g. one of my custom git- commands that it picks up is:
commands[git-build-zsh]=/home/bhaskell/bin/git-build-zsh

So, we're looking for the keys '(k)'.  And we only want entries in the 
hash that have (I)ndexes matching 'git-*'.  Then we want to remove the 
leading 'git-' portion ( ${...#git-} ).

-- 
Best,
Ben

P.S. Played around a bit with (k), and [(MIRK)*], but couldn't seem to 
find a way to not repeat the 'git-' pattern.  I'm sure a veteran could 
do it effortlessly.

P.P.S. I found it a bit odd that:
print -l ${(<Tab>
completes glob qualifiers rather than parameter expansion flags.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: _git Completion and custom commands
  2010-09-28  4:06 ` Benjamin R. Haskell
@ 2010-09-28  5:28   ` Sebastian Stark
  2010-09-28  6:30     ` Benjamin R. Haskell
  2010-09-28  8:03   ` Frank Terbeck
  1 sibling, 1 reply; 6+ messages in thread
From: Sebastian Stark @ 2010-09-28  5:28 UTC (permalink / raw)
  To: Benjamin R. Haskell; +Cc: Conrad Irwin, zsh-users


Am 28.09.2010 um 06:06 schrieb Benjamin R. Haskell:
>> My question is then: how would I add my custom commands' names to the list already completed by _git (without completely overriding or modifying the system-wide configuration)?
> 
> This has bothered me for a while, too.  I think _git should include these by default.  But, it's pretty easy to add:
> 
> zstyle ':completion:*:git:*' user-commands ${${(k)commands[(I)git-*]}#git-}
> 
> (I've found _git to be very about what it allows you to override via zstyles)
> 
> Explanation of the '${${(k)commands[(I)git-*]}#git-}' portion:
> 
> $commands is a built-in associative array that maps basenames to their full paths.
> 
> E.g. one of my custom git- commands that it picks up is:
> commands[git-build-zsh]=/home/bhaskell/bin/git-build-zsh
> 
> So, we're looking for the keys '(k)'.  And we only want entries in the hash that have (I)ndexes matching 'git-*'.  Then we want to remove the leading 'git-' portion ( ${...#git-} ).

All this is just because then you can type "git bu<TAB>" instead of "git-bu<TAB>"? Or is there more to it? Perhaps I do not understand.


Sebastian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: _git Completion and custom commands
  2010-09-28  5:28   ` Sebastian Stark
@ 2010-09-28  6:30     ` Benjamin R. Haskell
  0 siblings, 0 replies; 6+ messages in thread
From: Benjamin R. Haskell @ 2010-09-28  6:30 UTC (permalink / raw)
  To: Sebastian Stark; +Cc: Conrad Irwin, zsh-users

On Tue, 28 Sep 2010, Sebastian Stark wrote:

>
> Am 28.09.2010 um 06:06 schrieb Benjamin R. Haskell:
>>> My question is then: how would I add my custom commands' names to 
>>> the list already completed by _git (without completely overriding or 
>>> modifying the system-wide configuration)?
>>
>> This has bothered me for a while, too.  I think _git should include 
>> these by default.  But, it's pretty easy to add:
>>
>> zstyle ':completion:*:git:*' user-commands ${${(k)commands[(I)git-*]}#git-}
>>
>> (I've found _git to be very about what it allows you to override via zstyles)

                               ^ (very good about)

>>
>> Explanation of the '${${(k)commands[(I)git-*]}#git-}' portion:
>>
>> $commands is a built-in associative array that maps basenames to 
>> their full paths.
>>
>> E.g. one of my custom git- commands that it picks up is:
>> commands[git-build-zsh]=/home/bhaskell/bin/git-build-zsh
>>
>> So, we're looking for the keys '(k)'.  And we only want entries in 
>> the hash that have (I)ndexes matching 'git-*'.  Then we want to 
>> remove the leading 'git-' portion ( ${...#git-} ).
>
> All this is just because then you can type "git bu<TAB>" instead of 
> "git-bu<TAB>"?

Roughly, yes.  But "all this" is really just adding a line to a startup 
file.  The rest was just over-explanation (since the OP mentioned just 
switching from bash).


> Or is there more to it? Perhaps I do not understand.

If you started learning git before all the git-cmd -style commands were 
no longer in path, maybe that git-bu<tab> seems natural, but I don't 
like that style at all.  And personally, typing 'git ' is becoming so 
common I may alias it to 'g '.

The other portion not mentioned is that commands thus added show up in 
the normal list:

git <tab>

produces lots of output, but ends with my custom commands all grouped at 
the end.

-- 
Best,
Ben


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: _git Completion and custom commands
  2010-09-28  4:06 ` Benjamin R. Haskell
  2010-09-28  5:28   ` Sebastian Stark
@ 2010-09-28  8:03   ` Frank Terbeck
  2010-09-28 18:01     ` Conrad Irwin
  1 sibling, 1 reply; 6+ messages in thread
From: Frank Terbeck @ 2010-09-28  8:03 UTC (permalink / raw)
  To: zsh-users

Benjamin R. Haskell <zsh@benizi.com>:
[...]
>> 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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: _git Completion and custom commands
  2010-09-28  8:03   ` Frank Terbeck
@ 2010-09-28 18:01     ` Conrad Irwin
  0 siblings, 0 replies; 6+ messages in thread
From: Conrad Irwin @ 2010-09-28 18:01 UTC (permalink / raw)
  To: zsh-users

On 28 September 2010 09:03, Frank Terbeck <ft@bewatermyfriend.org> wrote:
> Benjamin R. Haskell <zsh@benizi.com>:
> [...]
>>> 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-}
>

This is perfect — thank you!

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

On some version later than the version I used to have. Thank you for
your help (on IRC) in getting the right version of the _git function,
which allowed Ben's command to work.

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

I do have a large number of commands, but luckily I know what they do
:). So I'm going with the automagic rather than the explicit — though
I may consider the additional options for the few commands that take
several options (particularly those that mix the arguments of other
git commands)

> 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.
>
I was just comparing this to the default that Debian gave me — I'll
have to investigate further.

Thanks to everyone.

Conrad


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-09-28 18:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-28  0:29 _git Completion and custom commands Conrad Irwin
2010-09-28  4:06 ` Benjamin R. Haskell
2010-09-28  5:28   ` Sebastian Stark
2010-09-28  6:30     ` Benjamin R. Haskell
2010-09-28  8:03   ` Frank Terbeck
2010-09-28 18:01     ` Conrad Irwin

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).