Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git tab completion not working in zsh on mac

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:

  • Switched to using zsh as my default shell
  • Installed oh-my-zsh
  • Downloaded https://github.com/git/git/blob/master/contrib/completion/git-completion.bash and saved it in ~/.completion/git/git-completion.sh
  • Downloaded https://github.com/git/git/blob/master/contrib/completion/git-completion.zsh and saved that in ~/.zsh/_git
  • Added zstyle ':completion:*:*:git:*' script ~/.completion/git/git-completion.sh

No luck.

like image 607
hamchapman Avatar asked Jul 01 '14 15:07

hamchapman


People also ask

How do I use autocomplete in zsh?

zsh-autocomplete adds real-time type-ahead autocompletion to Zsh. Find as you type, then press Tab to insert the top completion, Shift Tab to insert the bottom one, or ↓ / PgDn to select another completion.

Does bash completion work with zsh?

for ZSH users Zsh can handle bash completions functions. The latest development version of zsh has a function bashcompinit, that when run will allow zsh to read bash completion specifications and functions.

What is git-completion zsh?

Configuring the Shell fpath : The git-completion. zsh is a function file, not designed to be sourced like the bash script. This command appends the ~/. zsh directory onto the shell's function lookup list.


5 Answers

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

  • https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Zsh
  • https://docs.brew.sh/Shell-Completion#configuring-completions-in-zsh

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"

like image 52
Ivan Bartsov Avatar answered Oct 27 '22 12:10

Ivan Bartsov


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

like image 38
Djamel Avatar answered Oct 27 '22 12:10

Djamel


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

like image 38
hamchapman Avatar answered Oct 27 '22 11:10

hamchapman


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.

like image 42
toxaq Avatar answered Oct 27 '22 13:10

toxaq


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

like image 23
Nawa Avatar answered Oct 27 '22 12:10

Nawa