38

I installed Cygwin on windows and one of the default packages is git tab completion, but I don't have tab completion.

I did some searching and found a note saying it isn't enabled by default. I copied the skel .bashrc and .bash_profile to ~ and in .bashrc I uncommented the lines for completion and then relaunched the console. Still nothing.

I then noticed that the file .bash_completion was not present in /etc, so I found a copy of that online and put it in place, but I still have no completion in git.

It seems there should also be a /etc/bash_completion.d directory with git/ in it, but I don't have that either.

Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
JAyenGreen
  • 1,385
  • 2
  • 12
  • 23
  • 3
    There is thread on stackoverflow which is similar to this. http://stackoverflow.com/questions/11173447/git-bash-completion-on-debian – shakthydoss Sep 10 '13 at 15:48

8 Answers8

57

Updated for 2016: Install package bash-completion.

Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
sesm
  • 1,218
  • 12
  • 21
  • From the installation try: The git-completion package is obsolete. Selecting this package for installation will cause the git package, which replaces this one, to be installed instead. – kasi May 11 '16 at 09:49
  • 1
    This is the easiest way to install it as of May 2016! – Theodore R. Smith May 21 '16 at 07:23
  • A little note, the package are adding `bash_completion.sh` in `/etc/profile.d`, so no need to call `/usr/share/bash-completion/bash_completion` in `.bashrc`. The mentioned script also does some checking, including testing if it already had run. – 244an Mar 18 '17 at 23:56
  • 6
    I installed cygwin on 2017/6/8, version is 2.8.2. It is 64 bit version. I installed `git` and `bash-completion`, but then I found that `/etc/bash_completion.d/git` is still missing which makes the git completion still not working. How to fix it? I also try to search for `git-completion` but I cannot found it. – sflee Aug 06 '17 at 00:27
  • 2
    this is a dumb followup, but make sure you've installed both the `git` and `bash-completion` packages in cygwin. it's not enough to have bash-completion plus windows git. – starwarswii Jan 28 '21 at 15:24
  • it's 2021 and this still seems to work. I'm confused because `https://cygwin.com/cgi-bin2/package-cat.cgi?file=x86_64%2Fgit%2Fgit-2.32.0-1&grep=git` shows that it installs `usr/share/bash-completion/completions/git` and completion didn't work with it alone. After installing bash-completion, my `/etc/bash_completion.d` directory still doesn't have a git file in it. I'm not sure how installing bash-completion magically got completion to work, but it did. – Daniel Kaplan Jun 21 '21 at 01:56
10

The answer above prints out bash: have: command not found for nearly every script in bash_completion.d/. This answer from the linked post solves it for me:

if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi

The script/program /etc/bash_completion already includes the scripts in /etc/bash_completion.d and also defines some functions needed by the included scripts.

However, beware that this will slow down the loading of your shell, especially in Cygwin (slowed to about 1 second load for me). To mitigate this, you can rename everything in /etc/bash_completion.d/ to *.bak, and then remove the .bak selectively from the commands you actually care about (thanks to Slow load time of bash in cygwin).

Update: The file /etc/bash_completion magically disappeared (looks like it was due to upgrading the bash-completion package to 2.1-1). As a quick solution, I just created a symlink in /etc to /usr/share/bash-completion/bash_completion.

Update 2: Re-reading the original question, the issue with not having an /etc/bash_completion.d directory might be related to not installing the "bash-completion" package in Cygwin.

Community
  • 1
  • 1
Johann
  • 4,107
  • 3
  • 40
  • 39
  • Currently there is no `/etc/bash_completion` file that comes with `bash-completion` package. There is file `/etc/profile.d/bash_completion.sh` instead. This file in turn is being executed by `/etc/profile`. I was checking if `git --ver` will expand to `git --version`. After sourcing `/etc/profile` bash completions started to work. Looks like after some update `/etc/profile` is not being executed. So now the last question left for me is: what would be the best place to call `/etc/profile`? – Victor Yarema Oct 08 '18 at 18:34
5

In my .bashrc

 for file in /etc/bash_completion.d/* ; do
    source "$file"
 done
  • 3
    Try source /etc/bash_completion.d/git it should enable it in the current shell. If it does, check your bash init scripts –  Sep 10 '13 at 15:45
  • Again, I don't have a /etc/bash_completion.d I followed the link given above and did what comment 11 said Put the following lines in your ~/.bashrc if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi The script/program /etc/bash_completion already includes the scripts in /etc/bash_completion.d and also defines some functions needed by the included scripts. Still no autocompleteion in git. I'm still also perplexed how cygwin can show it's including git autocomplete yet after installing there's no /etc/bash_completion file nor a /etc/bashcompletion.d directory – JAyenGreen Sep 10 '13 at 22:19
  • Sesm's answer is the only one that works in 2015. This particular answer just screwed up my terminal. – Theodore R. Smith Jan 13 '15 at 16:48
5

To install "git-completion" package run from cmd.exe:

cyg-get git-completion

You don't need to modify your .bashrc after that.

Evmorov
  • 1,134
  • 22
  • 28
4

I stumbled on the same issue few days ago and installing bash-completion was not enough.

The issue was solved in my case downloading https://github.com/git/git/blob/master/contrib/completion/git-completion.bash and copying it into /etc/bash_completion.d/

See also: https://git-scm.com/book/en/v1/Git-Basics-Tips-and-Tricks

vito
  • 111
  • 1
  • 1
  • It worked for me (`cygwin` with Git for Windows instead of cygwin's native). However, the performance is unacceptable and every "miss" completion takes >30sec. – Greg Witczak Aug 21 '17 at 14:47
3

I had to install the following Cygwin packages:

  • bash-completion
  • bash-completion-devel
2

As of May 2016, I was able to get git & svn tab completion working by simply installing the bash-completion package using the Cygwin setup utility (v2.874) found here: https://www.cygwin.com/install.html. Make sure to select the 'Install from Internet' option. The bash-completion package is listed under the Shells category.

bblues
  • 21
  • 2
0

You can also source git-completion.bash directly from your Git Bash installation in .bashrc:

source /cygdrive/c/Program\ Files/Git/mingw64/share/git/completion/git-completion.bash

This works for git-prompt.sh as well.

This avoids having to install git through Cygwin separately, which could potentially be a different version than your Windows installation. Counterintuitively, the bash-completion package is not required.