JAyenGreen
JAyenGreen

Reputation: 1435

Cygwin git tab completion

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.

Upvotes: 39

Views: 16557

Answers (9)

Daniel Kaplan
Daniel Kaplan

Reputation: 67360

Update from 2024: Don't solve this via the cygwin package manager. Manually install the official git-completion.bash file instead.

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.

Original answer

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.

What if I want to use 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. Here's why:

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, but they won't be created until I attempt to autocomplete a git command first. This is a non-starter if you're trying to automate something (e.g., do something in your .bashrc file), because it requires a human to autocomplete first.

Upvotes: 0

superbadcodemonkey
superbadcodemonkey

Reputation: 917

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.

Upvotes: 0

Alfredo Carrillo
Alfredo Carrillo

Reputation: 316

I had to install the following Cygwin packages:

  • bash-completion
  • bash-completion-devel

Upvotes: 3

vito
vito

Reputation: 121

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

Upvotes: 5

sesm
sesm

Reputation: 1228

Updated for 2016: Install package bash-completion.

Upvotes: 58

bblues
bblues

Reputation: 21

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.

Upvotes: 2

Evmorov
Evmorov

Reputation: 1219

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

cyg-get git-completion

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

Upvotes: 5

Johann
Johann

Reputation: 4373

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.

Upvotes: 10

user2765647
user2765647

Reputation:

In my .bashrc

 for file in /etc/bash_completion.d/* ; do
    source "$file"
 done

Upvotes: 5

Related Questions