Cygwin git tab completion
Asked Answered
O

9

39

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.

Onionskin answered 10/9, 2013 at 15:20 Comment(1)
There is thread on stackoverflow which is similar to this. #11173947Jovanjove
G
58

Updated for 2016: Install package bash-completion.

Gipps answered 24/11, 2014 at 11:29 Comment(6)
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.Triform
This is the easiest way to install it as of May 2016!Butlery
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.Stowe
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.Mcmillian
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.Practise
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.Inane
I
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.

Inchoation answered 7/8, 2014 at 16:48 Comment(1)
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?Shantishantung
H
5

In my .bashrc

 for file in /etc/bash_completion.d/* ; do
    source "$file"
 done
Hackathorn answered 10/9, 2013 at 15:45 Comment(3)
Try source /etc/bash_completion.d/git it should enable it in the current shell. If it does, check your bash init scriptsHackathorn
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 directoryOnionskin
Sesm's answer is the only one that works in 2015. This particular answer just screwed up my terminal.Butlery
O
5

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

cyg-get git-completion

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

Outandout answered 7/3, 2015 at 14:32 Comment(0)
H
5

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

Horotelic answered 19/5, 2017 at 14:48 Comment(2)
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.Taught
While this works, git-completion.bash is now obsolete and there are newer versions of that file.Inane
R
3

I had to install the following Cygwin packages:

  • bash-completion
  • bash-completion-devel
Rudimentary answered 10/2, 2018 at 4:56 Comment(0)
L
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.

Laryngology answered 10/5, 2016 at 16:8 Comment(2)
In cygwin simply type: pact install bash-completionTriform
@Triform I think that pact comes with Babun, not plain CygwinPericlean
M
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.

Minelayer answered 30/5, 2023 at 19:25 Comment(0)
I
0

TL;DR from 2024: Don't do this through the cygwin package manager. Manually install the official git-completion.bash file instead.


Why don't the other answers work for everyone?

"bash-completion" installs the infrastructure needed for bash autocompletion, but if you look at the files it creates, git is not one of them. Installing it alone will not give you git autocompletion.

"git" installs /usr/share/bash-completion/completions/git and this gives you git autocompletion, but only when it's loaded. It won't be loaded unless you have "bash-completion" installed, too.

Depending on which of these are already installed, some answers will work for you and others will be incomplete.

What if I don't want to install the cygwin "git" package?

Someone mentioned that they use the Windows git command, and for that reason, they don't want to install cygwin's git command. In that case you can skip installing either package and manually install the official git-completion.bash file instead.

If I want the cygwin "git" command?

As mentioned, you can install both packages and probably move on with your life. This probably applies to 99% of the people.

On the other hand, you may not want to use the autocompletion that comes with the "git" package.

To be specific, the autocompletion script won't run until you interact with it first. That matters to me because I need to refer to some of the functions the script creates, and they don't always exist.

Manual install

  1. Download the file.
  2. Edit your .bashrc or .bash_profile, etc. to add this line: source /path/to/git-completion.bash
  3. If you have installed the cygwin "git" package and want to continue using it, rm /usr/share/bash-completion/completions/git so it doesn't interfere with your manual install.
Inane answered 14/7 at 23:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.