25

After struggling to get tab completion for git setup on osx, I've gotten some odd errors that I can't find the source too.

zsh:12: command not found: ___main
_default:compcall:12: can only be called from completion function

I'm not sure what is causing the error as everything is setup correctly.

zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
autoload -U compinit && compinit
zmodload -i zsh/complist
source ~/.git-completion.zsh

Any ideas?

Braiam
  • 1
  • 11
  • 47
  • 78
mhartington
  • 6,905
  • 4
  • 40
  • 75

2 Answers2

52

It seems that the git-completion.zsh is not designed to be sourceed. You could copy the git-completion.zsh file to somewhere in the $fpath and rename it to _git instead.

For example: (if you decide to have ~/.zsh/functions/_git.)

First, you could copy the git-completion.zsh to there and rename it to _git.

% mkdir -p ~/.zsh/functions && cp git-completion.zsh ~/.zsh/functions/_git

Then you could have your ~/.zshrc like this:

zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
# `compinit` scans $fpath, so do this before calling it.
fpath=(~/.zsh/functions $fpath)
autoload -Uz compinit && compinit

If I'm not sure, I do rm ~/.zcompdump to make sure that compinit discards its cache.

hchbaw
  • 4,894
  • 18
  • 16
  • 1
    You are my hero :) Thanks for the explanation. – mhartington Jan 20 '15 at 01:16
  • You don't need the zstyle part; by default it will use `/usr/share/bash-completion/completions/git`. – FelipeC Jun 06 '19 at 17:45
  • 2
    You literally just completed my 1hr search. Thanks! - The `zstyle ':completion:*:*:git:*' script ~/.git-completion.bash` is no longer needed – sramzan Apr 10 '20 at 03:38
  • This results in `__git_zsh_bash_func:8: command not found: __git_aliased_command` for me – Qwertie Nov 25 '20 at 00:04
  • 3
    got the same issue `__git_zsh_bash_func:8: command not found: __git_aliased_command` but the issue disappeared after restarting the terminal. also make sure the `gitfast` plugin disabled. – androidkc Jan 15 '21 at 23:31
  • Im a little confused by the extensions. You mentioned git-completion.zsh and then in the .zshrc you have a dot bash file. – Hernan Pintos Mar 12 '21 at 12:18
  • 3
    @HernanPintos Both two files are necessary to use the completion scripts in the Git distribution: https://git.kernel.org/pub/scm/git/git.git/tree/contrib/completion?h=v2.30.2 (We can see it by looking at `git-completion.zsh`, btw) – hchbaw Mar 13 '21 at 00:40
  • One thing I had to add: `compaudit | xargs chmod g-w` to avoid the warning `zsh compinit: insecure directories, run compaudit for list.` https://stackoverflow.com/a/66432105/2893090 – cs01 Jun 11 '21 at 20:27
2

pre

mkdir -p ~/.zsh
cd ~/.zsh

get completion scripts

curl -o git-completion.bash https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
curl -o _git https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh

add to ~/.zshrc

# git completion
zstyle ':completion:*:*:git:*' script ~/.zsh/git-completion.bash
fpath=(~/.zsh $fpath)
autoload -Uz compinit && compinit

restart shell session

diogo
  • 525
  • 1
  • 7
  • 12