Git completion not working in zsh on OS X Yosemite with Homebrew
Asked Answered
C

5

59

I cannot get git completion to work on my freshly installed OS X Yosemite in the zsh shell. I've installed both git and zsh using homebrew:

brew install zsh git

When installing git via homebrew, it should setup tab-completion for you, but when I git <tab>, it just tries to tab-complete the directories, and not the git-commands. I.e. it's not giving any errors - it's just not working - like it's not set up.

This is what I know so far

Apparently zsh will look for ways to autocomplete by looking for _* files in one of the directories in $fpath. This is mine:

% echo $fpath
/usr/local/share/zsh/site-functions /usr/local/Cellar/zsh/5.0.6/share/zsh/functions

Looking at the first path we see:

% ls -l /usr/local/share/zsh/site-functions
lrwxr-xr-x  1 watson  admin   55 Oct 20 12:08 _git -> ../../../Cellar/git/2.1.2/share/zsh/site-functions/_git
lrwxr-xr-x  1 watson  admin   70 Oct 20 12:08 git-completion.bash -> ../../../Cellar/git/2.1.2/share/zsh/site-functions/git-completion.bash

So it seems to be setup to work out of the box - but it doesn't.

This is what I've also tried

git-completion.bash

  1. Downloading git-completion.bash
  2. Running it using source git-completion.bash

This one kind of works (i.e. it enables auto-completion), but it prints a warning:

WARNING: this script is deprecated, please see git-completion.zsh

git-completion.zsh

So because of the above warning, I obviously also tried to download git-completion.zsh and followed the guide in the top of the file, but it basically tells you to source the .bash file first, which is of cause still giving an error.

Search StackOverflow and Google

I've tried just about everything I could find by searching here and on Google, but nothing seems to work for me.

Chlamydospore answered 20/10, 2014 at 9:45 Comment(1)
The git-completion.zsh script doesn't tell you to source the .bash file, the script does it by itself, it only needs to know the location. Since you have both files in the same directory that's all you need.Offish
C
104

I just stumbled upon the answer!

In my case I was missing a few important pieces in my .zshrc file. But first a little background:

What I'm trying to do is setup the "zsh Completion System". It comes with a lot of commands all named something with comp*. I tried to run these a few times but in many cases zsh would just tell me it didn't know them. So aparently you have to autoload them, among other things.

This is what I did:

I added the following lines to my .zshrc file:

autoload -U compinit && compinit
zmodload -i zsh/complist

Then I opened a new terminal and ran:

rm -f ~/.zcompdump; compinit

Then I opened a new terminal and now git <tab> worked as expected :)

If you are setting up a custom $fpath in your .zshrc file I would recommend adding these lines after you've modified the $fpath (though I don't know if it makes a difference).

Chlamydospore answered 21/10, 2014 at 5:24 Comment(2)
Thanks. I found that I didn't need zmodload -i zsh/complist, though. FYI.Harr
Thanks it is very helpful autoload -U compinit && compinit was enough for me.Azygous
C
35

Since macOS Catalina,...
Apple has switched from bash to zsh as their default shell.

Now the only brew command you need to activate zsh-completions (which includes git completion) is this...

brew install zsh-completions

Then add this block to your ~/.zshrc file (which you may have to create first):

if type brew &>/dev/null; then
  FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

  autoload -Uz compinit
  compinit
fi

If compinit reports insecure directories, you may need to fix permissions on some directories. You can use compaudit to find which directories need to be fixed.

See my answer on AskDifferent for more details:
https://apple.stackexchange.com/a/392382/77452

Cypriot answered 28/5, 2020 at 8:52 Comment(4)
Thanks for your answer. It worked for me after I ran chmod go-w '/usr/local/share' and then rm -f ~/.zcompdump; compinit as suggested by brew install zsh-completionBolinger
I had to also do a: chmod go-w '/usr/local/share/zsh' and chmod go-w '/usr/local/share/zsh/site-functions'Rabaul
I had to run compaudit and run chmod go-w on every dir listed in the outputEspresso
Ditto; the stuff listed by the advice at the end of the brew install was insufficient, I had to run compaudit to get the list of problematic directories.Ticktock
R
1

My problem was much simpler; I forgot to change my default shell.

echo $SHELL and if it's /bin/bash then just type chsh -s /bin/zsh

Replacement answered 22/10, 2018 at 21:7 Comment(0)
S
0

I removed the .zcompdump-(...) file I had in my home directory and plugins started working again.

Sonny answered 16/12, 2019 at 13:12 Comment(0)
H
0

On MacOS Ventura Deleting ~/.zcompdump directory worked for me

$ rm -r ~/.cache/zsh
$ exec $SHELL

directory structure

$ tree ~/.cache
~/.cache
└── zsh
    ├── compcache
    │   ├── brew_all_commands
    │   ├── brew_casks
    │   └── brew_formulae
    └── compdump
Hidrosis answered 15/7, 2023 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.