Git tab completion not working in zsh on mac
Asked Answered
B

8

201

No matter what I try and do I can't seem to make git tab/auto completion work in my zsh shell. I've downloaded the bash-completion script and the zsh-completion one and followed the instructions, but I can't make it work.

I've reinstalled oh-my-zsh but that didn't seem to help or make any difference.

Can anyone who's got it working describe to me their setup so I can try an emulate it to get it working for me?

To be specific, what I've done so far is:

No luck.

Blackthorn answered 1/7, 2014 at 15:20 Comment(6)
Have you added git to the plugins list?Emancipation
Yep, my plugins list only contains the git plugin.Blackthorn
And fpath=(~/.zsh $fpath) is somewhere in your config (possibly provided by Oh My Zsh)?Emancipation
Pretty much. Inside oh-my-zsh.sh I have fpath=($ZSH/functions $ZSH/completions $fpath) and for plugin ($plugins); do if is_plugin $ZSH_CUSTOM $plugin; then fpath=($ZSH_CUSTOM/plugins/$plugin $fpath) elif is_plugin $ZSH $plugin; then fpath=($ZSH/plugins/$plugin $fpath) fi doneBlackthorn
Let us continue this discussion in chat.Blackthorn
Here is a fix for git completion causing problems from homebrew: https://mcmap.net/q/129568/-just-autocomplete-and-git-alias-autocomplete-aren-39-t-working-with-zsh-and-homebrewAmbrotype
B
22

The answer was that I had alias git=hub in my .zshrc file. See https://github.com/github/hub for info on hub (it's awesome).

Here's a link to info about the problem I was having with hub and git completion: https://github.com/github/hub/issues/586#issuecomment-47727226

Blackthorn answered 2/7, 2014 at 9:20 Comment(1)
For me, the actual solution was to add FPATH="$(brew --prefix)/share/zsh/site-functions:${FPATH}" to my .zshrc above source $ZSH/oh-my-zsh.sh Per github.com/mislav/hub/issues/231#issuecomment-145174024 and docs.brew.sh/Shell-Completion#configuring-completions-in-zshKursk
F
481

TL;DR one-liner

echo 'autoload -Uz compinit && compinit' >> ~/.zshrc && . ~/.zshrc

this will enable completion in .zshrc and apply the setting to your current terminal session.

Explanation:

Actually, ZSH does know how to do git completion out of the box, but you need to turn on the completion feature itself (which from the steps you described I guess you haven't done)

Adding this to your .zshrc should be enough:

autoload -Uz compinit && compinit

After you put the line .zshrc file, don't forget to restart the shell for ZSH to pick up the new config (alternatively, you can execute the line in your current session, that'll enable autocompletion for that one session)

the zsh compinit: insecure directories warning

Thanks to @FranMorzoa for suggesting to use compinit -u to skip the security checks for completion scripts

While this will get rid of the warning/confirmation, the warning is there for a reason and it shouldn't happen normally.

It is a sign that something is wrong with ownership of the completion scripts, and it can (and should) be fixed with one of these:

  • brew.sh version:

    chmod -R go-w "$(brew --prefix)/share"

  • another one, will probably work for non-brew zsh, credits to pvinis on GitHub:

    compaudit | xargs chmod g-w

More info

PS Another answer here suggests installing the hub tool instead: although the tool is handy, it's merely a 3rd party (github community) wrapper around git. Hence, it has nothing to do with the topic of "Git completion in ZSH"

Final answered 23/10, 2019 at 7:30 Comment(8)
It's good but after restart I should run this command again... OS catalinaExtern
@Extern If you put this line in your .zshrc -- it works like configuration, i.e. ZSH executes everything there on each launch, so no need to run the command after restarting the mac. However, if you only need to turn the completion on just this one time, you can just directly execute the command and you'll get autocompletion, but it'll only remain active in that ZSH session.Final
% source ~/.zshrc zsh compinit: insecure directories, run compaudit for list. Ignore insecure directories and continue [y] or abort compinit [n]? Adding -u right after "&& compinit" solved the issue.Gaspar
This enables auto completion for me, but there is still one issue. Ambiguous tab-completions do not work as described in the git-scm book link. If I type git che<tab> that directly converts into git checkout instead of giving/listing the options of all the git commands starting with "che"Physostomous
The answer by @FranMarzoa was spot on – "autoload -Uz compinit && compinit -u" makes the feature work as expected without any insecure directory warnings when launching a new terminal. Thanks!Bandylegged
Some things work like pull, checkout, branch, etc. However, it does not recognize my branch names or my repo names. So not really a big help. Is it suppose to autocomplete my branches and repos too?Amphetamine
This causes Ignore insecure files and continue [y] or abort compinit [n]? each time my shell starts up now.Ics
@JJDuPlessis interesting enough I then removed the line "autoload -Uz compinit && compinit'" from my ~/.zshrc and it turns out it always work now. Seems some broken things now bridged by running this script once.Dit
F
25

For the 2019 viewer:

If you use ZSH:

brew install hub

mkdir ~/.zsh and mkdir ~/.zsh/completions

Once you got your directory created and hub installed, you have to cp the hub.bash_completion.sh file to your local zsh/completion folder.
(Mine was cp /usr/local/etc/bash_completion.d/hub.bash_completion.sh ~/.zsh/completions/_hub)

Then you add the following line to your ~/.zshrc file :

fpath=(~/.zsh/completions $fpath) 
autoload -U compinit && compinit

then source ~/.zshrc and voilà ! You should have the git completion available

source : https://github.com/github/hub/tree/master/etc#zsh

Freya answered 11/2, 2019 at 11:6 Comment(2)
Although zsh is the new "default" shell on OS X, you have to enable it: chsh -s /bin/zsh. Yes it tells you this in the splash message, but I was ignoring it. Afterwards this works like a charm.Bilestone
Not sure of the exact cause of this issue but for me I just needed to brew install hub and then I could tab to autocomplete branch names etc.Counterinsurgency
B
22

The answer was that I had alias git=hub in my .zshrc file. See https://github.com/github/hub for info on hub (it's awesome).

Here's a link to info about the problem I was having with hub and git completion: https://github.com/github/hub/issues/586#issuecomment-47727226

Blackthorn answered 2/7, 2014 at 9:20 Comment(1)
For me, the actual solution was to add FPATH="$(brew --prefix)/share/zsh/site-functions:${FPATH}" to my .zshrc above source $ZSH/oh-my-zsh.sh Per github.com/mislav/hub/issues/231#issuecomment-145174024 and docs.brew.sh/Shell-Completion#configuring-completions-in-zshKursk
C
4

Turns out the problem for me wass that when installing git via homebrew, git installs its own zsh shell extension which is considerably less complete/capable than the default that oh-my-szh installs. Find out what versions your git install is and then remove the zsh autocompletions. Mine were here and deleted thusly:

rm -rf /usr/local/Cellar/git/2.28.0/share/zsh/

This is not to say that the problem could not be any one of the other answers or a combination of.

Cuisse answered 15/9, 2020 at 3:12 Comment(1)
I wonder how we can avoid this, and if this should be filed as a bug for the completion provided by oh-my-zsh?Maibach
I
4

If nothing helps, try to remove symlink /usr/local/share/zsh/site-functions/_git that appears after installation git with Homebrew brew install git

Inquire answered 17/7, 2021 at 15:38 Comment(1)
I didn't realise that this isn't the default zsh completion, removing the link fixed my problem. I was getting _git_branch:[:16: unknown condition: -lt on tab after configuring completions for my wrapper function gb like this compdef _git gb=git-branchTerm
A
2
mkdir -p ~/.zsh/completions
curl "https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh" > ~/.zsh/completions/_git
chmod a+x ~/.zsh/completions/_git
cat ~/.zshrc | grep 'fpath=(~/.zsh/completions $fpath)' > /dev/null || echo 'fpath=(~/.zsh/completions $fpath)' >> ~/.zshrc
cat ~/.zshrc | grep 'autoload -Uz compinit && compinit' > /dev/null || echo 'autoload -Uz compinit && compinit' >> ~/.zshrc
source ~/.zshrc

Or just run brew install zsh-completions and follow the instructions.

Aciculate answered 3/9, 2021 at 10:23 Comment(2)
brew install zsh-completions throws an error, but running all the mentioned steps one by one, worked for me until the shell was open, after I closed terminal and reopened, it didn't worked.Airing
@ParthMansata for me it also opened after I reopenedOller
J
0

The same issue bothers me today. Inspired by the answers, I looked around the .zshrc and found this:

Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
Example format: plugins=(rails git textmate ruby lighthouse)
Add wisely, as too many plugins slow down shell startup.
plugins=(git)

Just comment the above line to enable the git plugin, the problem is solved. More details about the plugin can be found here: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/git

Jinx answered 4/9, 2018 at 13:50 Comment(0)
A
0

Jan 2024 Update (ZSH + Git + Brew)

I wrote a more thorough answer here. But if you have git installed via brew and are using zsh, this should fix auto completions (and give you dope autocompletions for every other brew function)

# Init Homebrew to get `HOMEBREW_PREFIX`
eval "$(brew shellenv)"

# Delete brew's objectively worse git completion
remove_conflicting_git_completions() {
    local git_completion_bash="$HOMEBREW_PREFIX/share/zsh/site-functions/git-completion.bash"
    local git_completion_zsh="$HOMEBREW_PREFIX/share/zsh/site-functions/_git"

    [ -e "$git_completion_bash" ] && rm "$git_completion_bash"
    [ -e "$git_completion_zsh" ] && rm "$git_completion_zsh"
}

# This needs to run every time since brew sometimes brings those files back
remove_conflicting_git_completions

# Add Homebrew's site functions to fpath (minus git, because that causes conflicts)
# This will give you autocomplete for _other_ things you installed
# from brew (like `just`, or `exa`, or `k6`)
fpath=($HOMEBREW_PREFIX/share/zsh/site-functions $fpath)
Ambrotype answered 7/1 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.