2

For an alias to git itself, eg. alias g=git, you'll find your answer more easily here.

For git subcommand aliases, eg. alias gco = "git checkout", look here.


I type git far too often, so I've aliased it:

alias g='git'

However, now I can't do:

g comtab

and have the completion present me with:

g commit

How do I get completion running again?


Duplicate differentiation

This equal-first meta answer suggests keeping the specific case with a with a link to the more generic question. I've added the link to the more generic case in this edit.

The asserted duplicate question is more generic - but mostly about aliases to git subcommands, rather than an alias to git itself.

This question is about the specific case where the alias is to git itself.

In the marked duplicate, the git-itself case is only answered in the 4th shown answer, and in a comment to the first listed answer. Reading another comment is required for the second piece of the puzzle (source /usr/share/bash-completion/completions/git) to get it working via that method.

Addressing the links in the comments below:

  • @ewcz's link is the marked duplicate and has already been addressed
  • Remy J's links
    1. This is question as asked (and answered) is also about git subcommands
    2. This is an even more generic question about non-git aliases, which doesn't isn't near to the best solution in this case
    3. This is the same as @ewcz's link (already addressed)
Community
  • 1
  • 1
Tom Hale
  • 25,410
  • 16
  • 132
  • 172
  • I'm running `GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)` – Tom Hale Sep 15 '16 at 08:58
  • 5
    http://stackoverflow.com/questions/342969/how-do-i-get-bash-completion-to-work-with-aliases ? – ewcz Sep 15 '16 at 09:00
  • Maybe these few links will help you out 1. [Git autocomplete in bash aliases?](http://stackoverflow.com/a/9869478/6683985) 2. [How do I get bash completion for command aliases?](http://unix.stackexchange.com/a/4220/184872) 3. [How do I get bash completion to work with aliases?](http://stackoverflow.com/a/15009611/6683985) 4. ... – Remy J Sep 15 '16 at 09:14

2 Answers2

9

At the end of your .bashrc, add:

_xfunc git __git_complete g _git

If you see bash: _xfunc: command not found, use the longer form:

source /usr/share/bash-completion/completions/git
__git_complete g _git

Reload bash

exec bash

(This answer was inspired by the kub1x's answer and the comments in a question about completion of git subcommand aliases)

Community
  • 1
  • 1
Tom Hale
  • 25,410
  • 16
  • 132
  • 172
4

Add the following in your .bashrc file:

source /usr/share/bash-completion/completions/git
complete -o default -o nospace -F _git g