How should I use argcomplete in zsh?
Asked Answered
Y

4

10

I'm using argcomplete to have Tab completion in Bash.

argcomplete offers global completion for bash, but doesn't for zsh.

I would like to create a file ~/.zsh_completion, to contain the to be completed files. This file should generate autocompletion for those files when it's sourced from ~/.zshrc.

How do I do that?

Yonne answered 4/6, 2015 at 7:41 Comment(3)
man zshcompsys or Completion System online doc. Zsh has a much more sophisticated completion system, and that's one reason why Zsh is loved by millions. It's usually advised to start with some configuration framework like Prezto, so that you get collective wisdom out of the box. And I don't know what you mean by "I would like to create a file ... to contain the to be completed files." Zsh completion is command based, and it can complete to different things for different commands.Homologize
And if you want to complete just to a predefined list, that's of course possible (Zsh is Turing complete) and pretty easy, but I won't say that's advised.Homologize
The question isn't about generic path-completion, it's about adding completion for arguments of python scripts. That's what the argcomplete lib is about.Disfeature
F
9

Argcomplete author here. I just released argcomplete v3, which supports zsh natively, without the bashcompinit compatibility layer, and with full official support for completion descriptions and global completion (which gets installed by activate-global-python-argcomplete into /usr/local/share/zsh/site-functions by default, but that behavior is configurable). Please give it a try.

Flyback answered 20/3, 2023 at 14:55 Comment(2)
That's nice to hear!Yonne
It doesn't appear to be working on MacOS 14 (Sonoma). I've followed the instructions and activate-global-python-argcomplete runs, installing to /opt/homebrew/etc/bash_completion.d/python-argcomplete and /opt/homebrew/share/zsh/site-functions/_python-argcomplete but even after a reboot there is no completing going on.Antananarivo
Y
8

Alright there is a way to do it, but it's not the way I really wanted it to be.

Anyway, here goes:

  1. Install argcomplete:

    $ pip install argcomplete
    
  2. Activate argcompolete:

    $ activate-global-python-argcomplete --user
    
  3. Add this to ~/.zshrc:

    autoload bashcompinit
    bashcompinit
    source ~/.bash_completion.d/python-argcomplete.sh
    
    eval "$(register-python-argcomplete /path/to/the/to/be/completed/file1)"
    eval "$(register-python-argcomplete /path/to/the/to/be/completed/file2)"
    eval "$(register-python-argcomplete /path/to/the/to/be/completed/file3)"
    

    There's probably a solution to read out the to be completed files from another file, but I don't know how to do that.

Yonne answered 14/1, 2016 at 19:17 Comment(5)
The docs say that global autocomplete is not supported for zsh which is too bad.Smutch
As far as I can see are step 2 and source ~/.bash_completion.d/python-argcomplete.sh unnecessary, they are just doing dynamically (which doesn't seem to work under zsh) what "$(register-python-argcomplete /path/to/the/to/be/completed/file1)" does explicitly.Ballottement
It's strange, when I run the eval "$(register-python-argcomplete /path/to/the/to/be/completed/file1)" command manually in a already spawned zsh, I get the desired autocompletion. But adding the line to .zshrc does not give the autocompletion.Damning
@Damning Had the same issue. Looks like running eval from within the script's root path seems to work. So cd into the root path first.Crysta
@Smutch so you remember where you read that information? wondering if it's still the caseSnapdragon
G
3

I've just released a new project by the name of pyzshcomplete which can be found here. It is meant to provide the same functionality as argcomplete but for zsh.

Glaydsglaze answered 20/5, 2020 at 18:2 Comment(0)
S
2

Same as Exeleration-G but in your ~/.zshrc you only put

eval "$(cd path/to/script; register-python-argcomplete script.name)"
Straiten answered 2/4, 2019 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.