Use 'g' as an Alias for 'git' without Losing Autocompletion 1

Posted by Joe Yates Sun, 29 Aug 2010 13:19:00 GMT

I've just started using a new kind of abbreviation system for Git:

  • I use 'g' instead of 'git',
  • I've set up bash autocompletion for 'g', just like 'git'.

Previously, I used the following in my ~/.bashrc:

alias gts='git status'
alias gtd='git diff'
The problem is that, by doing this, you lose autocompletion.


Set Up 'g' as an alias for 'git'

In ~/.bashrc:

alias g='git'

2. Locate the Bash Completion File for Git

Locate your bash_completion.d directory:

  • On Linux, /etc/bash_completion.d
  • I'm using OS X with Mac Ports, so mine is at /opt/local/etc/bash_completion.d.
Ensure the directory contains git-completion.bash

3. Add Completion for 'g'

Add the following to the end of git-completion.bash:

complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
        || complete -o default -o nospace -F _git g
Now, if you type:
$ g |<TAB>
you should see autocompletion:
$ g |
add                 commit              init                request-pull 
am                  config              instaweb            reset 
annotate            describe            log                 revert