If I run a regular git command such as git checkout
I get helpful autocompletion of branch names when hitting the tab key.
I have a few git aliases which take branch names as parameters, and I'm wondering if there's a way of getting the branch name autocompletion to work with them?
Edit:
Just to provide some clarification from the discussion in the comments, aliases with a direct mapping work fine, i.e.:
ci = commit
co = checkout
It's ones that are a bit more involved and use $1
as a parameter that don't, for example:
tagarchive = !f() { git tag archive/$1 origin/$1 && git push origin :$1 && git push origin archive/$1 && git branch -d $1; }; f
For git aliases, the autocomplete function for the git command (
__git()
) uses a call togit config --get "alias.$1"
to determine that equivalent autocomplete function. This works for simple mappings but will choke on more complex aliases.To get around this, define an autocomplete function with a name that matches your alias, i.e.
_git_tagarchive()
. The autocomplete function for git should pick that up and use it for autocompletion.For example:
For a more permanent solution simply add the function definition to your
bashrc
file. Eg:Note that I've simply reused the autocomplete function for
git branch
; you may wish to change this to something more suitable or write your own.More info
This solution was identified based on an exploration of
/etc/bash_completion.d/git
.Typically, aliased git commands are handled by the
__git_aliased_commands()
function which parses the output ofgit config --get "alias.$1"
to decide on the autocomplete function to use. Using a more complex shell command as the alias target would understandably foil this approach.Looking further, it appears the autocomplete function for git (
_git()
) chains in autocomplete function for subcommands by simple prepending the function with_git_
(with dashes (-
) in the command replaced by underscores). This is done before__git_aliased_command()
is checked so this is something we could use.The approach I've gone for is therefore to ensure that a function that matches your alias exists, i.e.
_git_tagarchive()
.Update July 2015 (Git 2.5): getting the git aliases is easier in
contrib/completion/git-completion.bash
.See commit 12bdc88, commit e8f9e42 (10 May 2015) by SZEDER Gábor (
szeder
).(Merged by Junio C Hamano --
gitster
-- in commit 935d937, 22 May 2015)~~~~~~~~~~~~~~
Git completion should work better with complex aliases in Git 2.1 (August 2014).
See commit 56f24e8 by Steffen Prohaska (
sprohaska
)completion: handle '
!f() { ... }; f
' and "!sh -c '...' -
" aliases