Lately I’ve been using some longer, and not very memorable, git branch names. I love bash completion and was hoping I could add items to this auto-completion dynamically on request. It turns out this is really simple to do. Thanks to this fab tutorial, a quick code snippet like this in my .profile and I’m all set.
_complete_git() {
if [ -d .git ]; then
branches=`git branch -a | cut -c 3-`
tags=`git tag`
cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "${branches} ${tags}" -- ${cur}) )
fi
}
complete -F _complete_git git checkout
Now anytime I’m in a path with a .git directory, I can just use ‘git checkout [tab]‘ and I’ll git tag and branch auto-completion goodness.
2 Responses for "Bash autocompletion + git = super lazy goodness…."
Git comes with a bash autocompletion script which includes autocompletion of commands, branches and tags and more.
See the script for installation instructions, then just tab away =).
http://repo.or.cz/w/git.git?a=tree;f=contrib/completion;hb=2ca880fe54660869bc93a2302efced9ab64511d9
Check out http://git.or.cz/gitwiki/InterfacesFrontendsAndTools#head-47cb6e5713be8f73eb479568ad9d2e8d8f8b7ef8
Hey, that looks really great thanks!
Leave a reply