is there a bash-completion script that supports filename completion? I use mostly mercurial and there I can type:
hg diff test/test_<tab>
and it will show/complete all modified test files. It works for most subcommands, i.e. hg add <tab><tab>
will only list untracked files. It is really handy.
The bash script from git contrib seams to not support this. Are there any alternatives, or how do you work with git on the command line?
Edit 2015
git-completion.bash
supports full filename completion since ~1.8.2
This solves the
git diff <tab>
problem for me, put the following in .bashrc:And then do
gid <tab>
instead ofgit diff <tab>
. It may be simplified, but seems to work well as a quick fix.So, let's see how the Mercurial bash completion script does this.
This is the important part:
It gets called here:
Thus, it is simply a call of
hg status -nmar
, and using the output as a list of files for completion.I think it would not be too hard to patch something similar into the git completion script - we would have to modify
__git_diff
here to not do a plain filename + branch completion, but callinggit status
instead.The commands
(for
git diff --cached
) and(for
git diff
) seem to output the right thing (if there are no renames).They both are not useful when diffing to anything other than HEAD, though.
A more general way would be to use
where
commit1
andcommit2
(and maybe--cached
) come from the already given diff command line.I implemented the idea outlined above in bash, and patched into
git-completion.bash
. If you don't want to change yourgit-completion.bash
, add these two functions to some bash file and source it after the originalgit-completion.bash
. It should now work with commands likeI submitted this as a patch to the git mailing list, let's see what results from this. (I'll update this answer as I get feedback there.)
Update: Looks like this patch is not wanted in this form, as the current way to complete files is more useful for people which want to check whether there are changes in some subdirectory (e.g. completing when the diff output could be empty). It might be accepted if linked to some configuration variable (with the default being the current behavior). Also, the indenting should be adapted to the standard (see the answer from Junio C Hamano).
I might take another go on it, but can't guarantee this for the near future. If someone else want to do, feel free to take my code, change it and submit it again.
Not really your desired answer but I wanted to let you know that soon fish (friendly interactive shell) will give you git filename completion support out of the box. It is currently in master with a 2.3.0 release coming soon.
https://github.com/fish-shell/fish-shell/issues/901
https://github.com/fish-shell/fish-shell/pull/2364
https://github.com/fish-shell/fish-shell/commit/c5c59d4acb00674bc37198468b5978f69484c628
If you have a status like this:
You can also just type
README
and hit tab and it will insert it for you if it is the only match. Friggin nice!Since 2011, as the OP comments, Git supports full filename completion since ~1.8.2.
But with Git 2.18 (Q2 2018), the shell completion (in
contrib/
) that gives list of paths have been optimized somewhat.See commit 78a2d21 (04 Apr 2018) by Clemens Buchacher (
drizzd
).(Merged by Junio C Hamano --
gitster
-- in commit 3a940e9, 25 Apr 2018)The issue was originally reported Git for Windows issue 1533.