Git tab completion not working in zsh on mac
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"
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