Git shell function (for aliasing) worked in bash with tab complete but not working in Zsh

Viewed 48

The alias I've used with success in bash but no longer works in Zsh looks like this

In .zshrc (previously .bashrc)

# custom git completions 
_git_a ()
{
    _git_add
}
_git_b ()
{
    _git_branch
}
_git_c ()
{
    _git_commit
}
_git_co () 
{
    _git_checkout 
}
.
.
.

# git alias function
g ()
{
    arg1=$1
    shift
    case "$arg1" in
        a)
            eval git add $@;;
        b)
            eval git branch $@;;
        c)
            eval git commit -m \"$1\";;
        co)
            eval git checkout $@;;
        .
        .
        .
        *)
            eval git $arg1 $@;;
    esac
}

zstyle ':completion:*:*:git:*' script ~/.git-completions-mingit.bash
autoload -Uz compinit && compinit -u
source ~/.mingit.sh
compdef g='git'

It used to be the case (in bash) that typing something within the alias such as

g co<TAB><TAB>

would result in the same behavior as

git checkout<TAB><TAB>

but now (in Zsh) the result is that g co<TAB><TAB> cycles through folders and files in the directory. Is there a way to get the tab completion on g co or other g commands to work the same as git ... when it comes to tab completion?

1 Answers

This can be solved by following the instructions in git-completion.zsh (https://github.com/git/git/blob/master/contrib/completion/git-completion.zsh):

# For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
# and then add the following to your ~/.zshrc file:
#
#  fpath=(~/.zsh $fpath)
#
# You need git's bash completion script installed. By default bash-completion's
# location will be used (e.g. pkg-config --variable=completionsdir bash-completion).
Related