ZSH auto completion for git takes significant amount of time, can I turn it off or optimize it?
Asked Answered
H

6

29

Git's tab autocompletion is useful for small projects, but I'm currently working on two big projects that use git and for these it's worse than useless. Whenever I type, say, git add forms<tab>, git takes 20 seconds or more to find the file (in this example, forms.py), and in this timespan I can't do anything else in the terminal. Is there any way to turn off the autocompletion feature, or somehow make it faster?

Hurdle answered 21/3, 2012 at 18:6 Comment(4)
Which shell are you using?Innovate
I'm using zsh and I'd like to use zsh's standard filename autocompletion rather than git's.Hurdle
By the way set -x is enough to see/prove which autocompletion takes too long.Tadio
@Johnsyweb: first line of git-completion.bash is: bash/zsh completion support for core GitTadio
E
57

It's not git auto completing the file names, it's your shell. Do you have the same delay when doing e.g. "cat forms< tab >"?

Check out this post with similar problems:

https://web.archive.org/web/20121013130629/http://talkings.org/post/5236392664/zsh-and-slow-git-completion

This post suggests adding the following to your .zshrc:

__git_files () { 
    _wanted files expl 'local files' _files     
}

EDIT: Here's the original text of that post

I found many posts relating complaints about how painfully slow git auto-completion can be in large repositories. There were various suggested patches and suggestions to load the latest zsh. Maybe one of those things would work, but all I really want is for it to complete the names of branches and files as they are in the file system. I did not find any suggestions on how to get this behavior so I figured it out for myself. I thought I would share this for anyone who might benefit from it. I just added the following to my .zshrc file:

__git_files () { 
    _wanted files expl 'local files' _files  }

Now I can run git commands and get near instant completion while still getting file completion similar to what ls would provide.

Empennage answered 21/3, 2012 at 18:17 Comment(7)
If I add a file to .gitignore, tab completion after a git command doesn't work with that file anymore.Hurdle
Ok. Please check my revised answer. It probably has to do with the communication between your shell and git then.Empennage
Thanks, that worked! I wonder if I should submit a git or zsh bug report, since the solution is very unintuitive.Hurdle
Your solution worked like a charm, but the link is dead. Could you please explain what this does? ThanksLamelliform
For those asking, it seems that __git_files is the function used for autocompleting git, and this new function scans the filesystem instead of communicating with git.Erudite
This doesn't seem to make a difference for me (I should not my repo consists of a lot of untracked, ignored files, and I'm using Cygwin's ZSH). Obviously something is different on my machine. Any suggestions how I can diagnose the discrepancy?Voidable
Make sure you put this command before your zsh plugins, otherwise you might end up with quick autocompletion, but errors once you try to actually run the git command.Graphophone
Q
12

Finally fed up with terribly slow auto-completion in zshell and starting looking a solution. I ended up switching from 'git' to using the 'gitfast' plugin that is already installed w/ oh my zsh and am flying... https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#gitfast

Qulllon answered 24/10, 2013 at 17:24 Comment(0)
B
7

I have no experience with zshell, but I got this answer on another forum. You need to include this line in your .zshrc file:

compdef -d git
Besotted answered 21/3, 2012 at 18:25 Comment(4)
Unfortunately, this makes completion of git commands fail. For example, if you type git ch<TAB>, it won't give a list of valid git commands.Downall
With many git commands the first argument can be either a branch or a filename. Sometimes you want to complete on the filename (which should always be super fast) and then you have to wait a super long time browsing all the branches you didn't care about. Disabling git completion fixes that frequent problem.Tadio
Yes, this turns off autocompletion, which is exactly what the OP asked for.Caskey
Thank goodness I found this answer. Hitting <Tab> in any repo that had a large LFS files would freeze zsh. Now completion of filenames in commands like 'git diff filename' complete instantly because it is just doing file matching.Taxicab
C
6

This is because Zsh comes by default with extremely bloated completion for Git. I wrote a blog post explaining how I fixed this bloatedness, but it had to be outside of the Zsh project.

The easy answer is to install Git's zsh completion, which is different than Zsh's git completion (which comes by default). Download git-completion.zsh, and place it in your ~/.zsh/_git. Then place it on your fpath:

fpath=(~/.zsh $fpath)

You should be flying now.

As another comment here explains; another option is to use oh-my-sh and enable the gitfast plugin, which achieves the same thing.

Why would Zsh developers insist on making their code slow? I don't know, but here you can see a sample of their reasoning: Re: Slowness issue with git completion.

Caskey answered 6/6, 2019 at 19:32 Comment(1)
I enabled gitfast plugin with antidote and it seems to be enabled. but if I do not move the mv /usr/share/zsh/5.9/functions/Completion/Unix/_git /usr/share/zsh/5.9/functions/Completion/Unix/_bugit out of the way, the git checkout feature/<TAB> is still slow as shit... Any solution to this?Surfing
B
-1

One very quick and dirty solution is to delete the following file responsible for the auto-completion.

/usr/local/git/contrib/completion/git-completion.bash
Besotted answered 21/3, 2012 at 18:17 Comment(4)
I can't do that since I'm not the only user on this server, and others may or may not want to use git autocompletion. It seems odd that git would not have an easy fix for something which, unless I'm misunderstanding something, happens to all git projects when they get large.Hurdle
@Aqwis: Don't source that file in your shell startup script.Psittacine
It's not sourced in my shell startup script.Hurdle
Besides that doesn't matter: that's for Bash, his problem is with Zsh. By default Zsh loads its bloated completion stuff.Caskey
T
-4

According to the answer to git bash auto complete slow on windows 7 x64, git 2.13 comes with a faster git-completion.bash

Tadio answered 3/10, 2017 at 17:55 Comment(5)
But this is for Zsh, not Bash.Caskey
@FelipeC, your own blog explains that it's possible (not saying "recommended") to use git-completion.bash in zsh.Tadio
Yes, but you didn't explain how.Caskey
You commented and voted down without even pointing at your own explanation. Weird.Tadio
This is what happens when you source that file in zsh: "WARNING: this script is deprecated, please see git-completion.zsh". I wrote that warning for a reason, and Git developers agreed; you need to use the new method. Yes, you can still do it, but it's not recommended, it has been deprecated for years, and we should probably remove it already.Caskey

© 2022 - 2024 — McMap. All rights reserved.