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.
.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 ofecho $_comps[git] $_comps[ga] $_comps[g] $_comps[git-all]
? Runset -x
then typeg
<space> <tab>; what's the output (it might be very long)?echo $_comps[git] $_comps[ga] $_comps[g] $_comps[git-all]
return just_git
once.type compdef
in my zsh, it sayscompdef is a shell function from /usr/share/zsh/functions/Completion/compinit
, whereas when I include it in my.zshrc
it printscompdef is a shell function from /home/dario/.antigen/init.zsh
.