argcomplete icon indicating copy to clipboard operation
argcomplete copied to clipboard

zsh support, including option descriptions

Open nickl- opened this issue 11 years ago • 21 comments

Let me start by saying well done with all the improvements since v2.9 the README looks awesome I must make a point to look at the docs... but who reads manuals anyway? =)

Being the devils advocate again, even though I am well aware that the project clearly states BASH completion I know there are many who use zsh for whatever reasons unbeknown to myself, your friendly devil and scope-boundary-shifter extraordinaire.

From what I can source the differences are not that huge but I am not well versed on things that make you go Oh My at all, maybe someone with such astonishment afflictions would care to step up and explain yourselves or just enlighten us on the peculiarities of zsh completion as both would be fascinating to hear.

What I can add: There is something called bashcompinit as explained in man zshcompsys and reads as follows:

The function bashcompinit compatibility with bash's programmable completion system. When run it will define the functions, compgen and complete which correspond to the bash builtins with the same names. It will then be possible to use completion specifications and functions written for bash.

Would that suggest our work is done here or do we need something other (zstyle, compctl, worshiping of false devils perhaps)? None of which would make me jump with joy, being a jealous devil and all, just thought I'd stir the pot and see what's cooking.

nJoy!

nickl- avatar Dec 25 '12 16:12 nickl-

I don't really care about zsh, but it is within the scope of this package and it's certainly #2 on the list of shells to support.

Does bashcompinit support complete -D?

kislyuk avatar Dec 25 '12 19:12 kislyuk

I do not have the foggiest - merely mentioned what I found on google as a starting point but I won't be much more use I am afraid. Had zsh support in aero before I yanked the custom implementation in favour of supporting argcomplete instead, for the greater good and all that. Would be nice to get it back but I won't be loosing any sleep over it just yet.

Anyone we can pass this ball to?

nickl- avatar Dec 26 '12 01:12 nickl-

I tested bashcompinit out of curiousity. It works.

$ autoload bashcompinit
$ bashcompinit
$ autoload compinit
$ compinit
$ eval "$(register-python-argcomplete docformatter)"
$ docformatter -<tab>
--help                 --recursive            -h
--in-place             --version              -i
--no-blank             --wrap-descriptions    -r
--pre-summary-newline  --wrap-summaries

myint avatar Jun 13 '13 03:06 myint

Glad to hear that! If you want, you can write a test suite and add it to tests (please configure it to only run if zsh and bashcompinit are found).

kislyuk avatar Jun 13 '13 15:06 kislyuk

While argcomplete does indeed run fine on zsh, zsh's auto-complete is far more advanced. argcomplete doesn't take advantage of any of this. Here are two example features that I'd love to see argcomplete handle:

  1. mini-documentation: zsh is capable of listing each possible argument with a mini documentation about it. When I type "git /tab/", I get:
add                 -- add paths to the index
am                  -- apply patches from a mailbox (cooler than applymbox)
annotate            -- annotate file lines with commit info
  1. aliases: zsh is aware of aliases. For example, it can be told that '-h' is the same as '--help', and will display both of them on the same line, and won't show the --help if -h is already typed (and vice versa). In addition, for the mini-doc display, it will display them on the same line. For example, if I type "p4 /tab/", I get:
changelist    change   -- Create or edit a changelist description                                                                                                                                                        

They're shown together because "change" is an alias of the "changelist" subcommand.

Since argcomplete has all the necessary info to implement both these features, I'd strongly vote for doing this as a feature request. If I get around to doing it, I'll submit a patch. Meanwhile, thanks for argcomplete, it's awesome!

redstreet avatar Aug 18 '13 23:08 redstreet

Sounds great, I'm not a big zsh user and don't have a lot of time for this, but I'd love to see this.

kislyuk avatar Aug 22 '13 16:08 kislyuk

I found the commit that yanked out the custom autocomplete which worked for zsh as well, and got replaced by argcomplete. Aeronautics/aero@06de51e6c5d and this was the eval code Aeronautics/aero@5ec10de5057 which did the magic.

Along the lines of:

function _aero_completion {
  local words cword
  read -Ac words
  read -cn cword
  reply=( $( COMP_WORDS="$words[*]" \\
             COMP_CWORD=$(( cword-1 )) \\
             AERO_AUTO_COMPLETE=1 $words[1] ) )
}
compctl -K _aero_completion aero

Does that shed any lights in the otherwise dark tunnel?

nickl- avatar Sep 18 '13 20:09 nickl-

The current bash autocomplete works fine in zsh, since zsh can accept bash autocomplete scripts. However, this doesn't use the much more advanced features for autocomplete that zsh has. Taking advantage of these features would likely require a bunch of new code (i.e., it's not a simple fix, it requires some design and development).

redstreet avatar Sep 18 '13 20:09 redstreet

@redstreet @nickl- @kislyuk

Another example from aws-cli, which uses a wrapper similar to the one mentioned by @nickl- in Aero.

aws-cli approach to wrapping zsh: https://github.com/aws/aws-cli/blob/develop/bin/aws_zsh_completer.sh

$ source bin/aws_zsh_completer.sh

Related: tcsh on #49

tony avatar Oct 26 '13 23:10 tony

@tony, thanks for the pointer to https://github.com/aws/aws-cli/blob/develop/bin/aws_zsh_completer.sh, there's some interesting/crazy code in there.

Does zsh have a convention for installing completion hooks globally or per user? And is anyone interested in finding out what should be done to make use of the advanced features that @redstreet is referring to?

kislyuk avatar Oct 27 '13 22:10 kislyuk

As I mentioned earlier, the current bash autocomplete that argcomplete does works fine in zsh, since zsh can accept bash autocomplete scripts. The code at https://github.com/aws/aws-cli/blob/develop/bin/aws_zsh_completer.sh seems to help with getting this to work better, but doesn't use the advanced zsh autocompletion features themselves.

Here's an example of one of the features: showing brief descriptions next to parameters or subcommands: http://joshparnham.com/2012/10/nanoc-plus-zsh-equals-awesomeness/

I've written small zsh autcomplete scripts, and I'd be happy to work on this, but won't have the time in the near future. If anyone is interested, here's a very basic tutorial to get you started: http://askql.wordpress.com/2011/01/11/zsh-writing-own-completion/

redstreet avatar Oct 28 '13 02:10 redstreet

Does argcomplete currently support global installation with zsh? I haven't managed to get it to work (while it works fine with zsh), and I don't think this is clearly specified in the docs.

anntzer avatar May 28 '14 10:05 anntzer

I don't use zsh, so I don't have a clear idea of what level of compatibility its bash completion compatibility layer provides. From the zsh documentation, it seems like this should work:

autoload bashcompinit
bashcompinit
source argcomplete/bash_completion.d/python-argcomplete.sh

But cursory testing shows that it's not working.

kislyuk avatar May 30 '14 23:05 kislyuk

Yes, that is what I tried. I understand fixing this is not a priority for you but can you please document this limitation?

anntzer avatar May 31 '14 01:05 anntzer

In https://github.com/kislyuk/argcomplete/commit/b38c1aaec131871238a68ca9291db572fa7138c7.

kislyuk avatar Jun 07 '14 18:06 kislyuk

:+1: for zsh support.

sotte avatar Nov 10 '14 13:11 sotte

Pull requests are welcome. I would love to add it, but I don't use zsh on a daily basis and don't currently have the time to write this. Need a zsh expert to take a look.

kislyuk avatar Nov 10 '14 17:11 kislyuk

FYI: I tested with zsh's bashcompinit and it indeed seems to work fine.

As already stated, bash completion is overly simple (read: crap) and using zsh native completion would yield much better results... However, supporting multiple output formats in a code that was written with only one in mind sounds like more work than I'm willing to do. But a man can dream, right? :)

yac avatar May 13 '15 16:05 yac

If completion doesn't work for you using bashcompinit, note https://github.com/zsh-users/zsh/commit/e2f793e7df7214cc3d80e2fcfe961ed087c860ab which I had to apply to my system's bashcompinit to get this to work.

segevfiner avatar Jan 17 '18 23:01 segevfiner

Will global support work in zsh the same way it does in bash? I.e. not having to individually register files? The portion of the README addressing zsh only describes the individual file method.

atyshka avatar Dec 23 '19 01:12 atyshka

When switching to zsh and finding out that argcomplete is not fully compatible (via this issue), I decided to try my hand at writing my own solution to this problem. I have just released my first version of it, and thought to share for all of the people who'll reach this thread like I did. The project is called pyzshcomplete and can be found here. Any feedback would be highly appreciated.

dan1994 avatar May 20 '20 17:05 dan1994

argcomplete now supports zsh directly (without the bashcompinit compatibility layer) in the develop branch.

The practical impact of this support is currently limited to emitting description strings in zsh, but by directly emitting completions to zsh instead of using the bash compatibility layer, we have unlocked the potential for using other, fancier zsh features as well.

Global completion is not supported yet (I am still looking into how to support it in zsh), so commands have to be individually registered with register-python-argcomplete.

I will be releasing this functionality in a new release shortly.

kislyuk avatar Mar 19 '23 09:03 kislyuk

OK, I was able to activate global completion for zsh using compdef _python_argcomplete_global -P '*', so zsh is now fully and officially supported by argcomplete.

kislyuk avatar Mar 19 '23 21:03 kislyuk

Released in v3.0.0.

kislyuk avatar Mar 19 '23 23:03 kislyuk