1

I have an alias for git defined as

alias g=git

With this and my zsh and antigen setup, g has the same auto-completion as git.

However, when I replace g with a function to show the git status by default

g() {
  if [ "$#" -eq 0 ]; then
    git status --short --branch
  else
    git "$@"
  fi
}

this, naturally, doesn't work anymore. The same goes for a git-all function to run the same git command in all repositories in sub-directories

git-all() { 
  if [ "$#" -eq 0 ]; then
    find . -maxdepth 3 -type d -name .git -execdir echo \; -execdir pwd \; -execdir git status --short --branch \;
  else
    find . -maxdepth 3 -type d -name .git -execdir echo \; -execdir pwd \; -execdir git "$@" \;
  fi
}
alias ga='git-all'

I would like to have completion for all these, g, git-all, and ga, just like for git.

The docs say

If you want a command, say cmd1, to have the same completions as another, say cmd2, which has already had completions defined for it, you can do this:

compdef cmd1=cmd2

So, I type

compdef git-all=git

in my current zsh session, and it works! Nice.

So, I put the compdefs in my .zshrc after my antigen setup, which has zsh-users/zsh-completions to init the completions, and after my alias and function definitions

if [ -f ~/.antigenrc ]; then
  source ~/.antigenrc
fi

if [ -f ~/.sh_aliases ]; then
  source ~/.sh_aliases
fi

compdef g=git
compdef ga=git
compdef git-all=git

antigen apply

And my antigenrc looks like this:

source /usr/share/zsh-antigen/antigen.zsh

antigen use oh-my-zsh

antigen bundle gradle/gradle-completion
antigen bundle command-not-found
antigen bundle MikeDacre/cdbk
antigen bundle zsh-users/zsh-completions
antigen bundle zsh-users/zsh-syntax-highlighting

antigen theme ys

and I start a new zsh shell. Now the completions don't work.

How can that be? An interactive zsh shell reads the .zshrc (if I put an echo in there, I see the output). If I put the compdef before the antigen setup, I get errors about compdef not being defined, but when they are at the end they don't show errors, they just don't work. Maybe antigen is doing something strange, but even then, the completions are defined after the antigen setup, so antigen shouldn't mess them up?

I also tried adding _git 2>/dev/null to my .zshrc as suggested here, or using compdef '_dispatch git git' g as suggested here, to no avail.

My zsh version is 5.8.

3
  • 1
    Please post your complete .zshrc so that people can reproduce your setup. Also tell us what zsh version you're using in case it matters. What's the output of echo $_comps[git] $_comps[ga] $_comps[g] $_comps[git-all]? Run set -x then type g <space> <tab>; what's the output (it might be very long)? Sep 26 '21 at 19:57
  • Thanks for the response. My complete .zshrc is very long, I understand that somewhere in there might be the source of the problem, but I'm pretty sure I posted the relevant parts. It's probably something to do with antigen. I'll add my antigenrc. echo $_comps[git] $_comps[ga] $_comps[g] $_comps[git-all] return just _git once. Sep 28 '21 at 6:52
  • I'm onto something now: When I type type compdef in my zsh, it says compdef is a shell function from /usr/share/zsh/functions/Completion/compinit, whereas when I include it in my .zshrc it prints compdef is a shell function from /home/dario/.antigen/init.zsh. Sep 28 '21 at 6:56
1

I finally figured it out. It seems to be a bug with antigen. There is also an issue on the antigen Github, which contains a workaround:

For me the way of fixing it was to comment out in the .antigen/init.zsh the following lines:

# autoload -Uz add-zsh-hook; add-zsh-hook precmd _antigen_compinit
# compdef () {}

I know it's not the proper way of fixing it and that it can cause some other errors, but I'm heavily using kubectl so I need autocompletion so much

The function compdef () {} obviously doesn't do anything and is what is called in my .zshrc. The quoted workaround didn't work for me, because the .antigen/init.zsh is (re-)generated by antigen, but what works for me is to put autoload -U +X compinit && compinit before my compdefs in my .zshrc:

#type compdef #uncomment this to see the problem
autoload -U +X compinit && compinit
#type compdef #uncomment this to see the solution
compdef g=git
compdef ga=git
compdef git-all=git
1

As suggested in the Github Issue Comment, a workaround (unknown consequences) you would need to comment the following line in antigen.zsh not .antigen/init.zsh (since init.zsh is generated every time antigen loads)

# autoload -Uz add-zsh-hook; add-zsh-hook precmd _antigen_compinit
# compdef () {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.