2

I have a few aliased functions such as gb for git branch and gco for git checkout in my .zshrc. This works great when I remember the full branch name I'm creating, deleting, checking out, etc. However, I noticed that completions no longer seem to work. Previously, I could do

$ git checkout m<TAB>

and it would autocomplete master if that was the name of a branch. Now, however, I get the following error when I use:

$ gco m<TAB>
_git:15: parse error: condition expected: 1

I'm not sure why this is occurring. It appears there's possibly a missing argument, but I'm not sure why.

Edit:

I'm setting an alias for git branch and git checkout in my .zshrc file like this:

alias gco='git checkout'
alias gb='git branch'
Sandro
  • 4,761
  • 1
  • 34
  • 41

2 Answers2

4

After a little more digging around I found a solution to the same problem for bash that looks to work just as well for zsh. After defining the two functions __define_git_completion and __git_shortcut you would call __git_shortcut to set up your alias and the completion in one call.

__define_git_completion () {
eval "
    _git_$2_shortcut () {
        COMP_LINE=\"git $2\${COMP_LINE#$1}\"
        let COMP_POINT+=$((4+${#2}-${#1}))
        COMP_WORDS=(git $2 \"\${COMP_WORDS[@]:1}\")
        let COMP_CWORD+=1

        local cur words cword prev
        _get_comp_words_by_ref -n =: cur words cword prev
        _git_$2
    }
"
}

__git_shortcut () {
    type _git_$2_shortcut &>/dev/null || __define_git_completion $1 $2
    alias $1="git $2 $3"
    complete -o default -o nospace -F _git_$2_shortcut $1
}

__git_shortcut  ga    add
__git_shortcut  gb    branch            # I use this one.
__git_shortcut  gba   branch -a
__git_shortcut  gco   checkout          # and I use this one, too.
__git_shortcut  gci   commit -v
__git_shortcut  gcia  commit '-a -v'
__git_shortcut  gd    diff
__git_shortcut  gdc   diff --cached
__git_shortcut  gds   diff --stat
__git_shortcut  gf    fetch
__git_shortcut  gl    log
__git_shortcut  glp   log -p
__git_shortcut  gls   log --stat
Sandro
  • 4,761
  • 1
  • 34
  • 41
-1

I believe you should create the aliases in the .gitconfig if you want autocomplete to work as well. However you will have to spell the git command out.

[alias]
gb = branch
gco = checkout

etc...

You can also add aliases like this:

git config –-global alias.gb branch

They will be appended to your .gitconfig

cheers

peshkira
  • 6,069
  • 1
  • 33
  • 46
  • 2
    Thanks, but I believe this adds an alias to the `git` command so the alias `gb` here would add `git gb m` completions, but I'm trying to use just `gb m` – Sandro Jun 04 '12 at 15:08
  • 2
    `git gco foo` isn't any shorter/more-useful than e.g. `git checkout foo`. it's not defining `gco foo`. – AD7six Jun 04 '12 at 21:35