1

In my git config, I have a relatively long alias which outputs a log of recent git commits in a pretty format:

lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset'

I also have another alias which builds on the previous one by adding the --all option the the command. I didn't want to repeat myself by specifying the entire command string in both aliases, so I just had my new alias shell out to the previous one:

la = !git lg --all

This works quite well, but there's a problem: while autocompletion of branch names works fine with my regular git lg alias, it doesn't work at all for the one that shells out.

How can I make autocompletion of git branch names work with aliases that shell out to other commands?

Note: This question is distinct from How do I get bash completion to work with aliases?, because that question deals with bash aliases of git commands, and not git's built-in alias system.

Community
  • 1
  • 1
Ajedi32
  • 45,670
  • 22
  • 127
  • 172

1 Answers1

2

In this case typing any branch name would give the same results because --all option shows all refs. However if you add

function _git_la() {
  _git_log
}

to your shell startup file it should autocomplete correctly.

kaman
  • 376
  • 1
  • 8
  • Sorry, I guess I picked a bad example with `la`. I do have similar aliases though where branch name autocompletion is more useful. Thanks for your help. – Ajedi32 Jun 20 '14 at 13:09
  • Also, do you know where this behavior is documented? I've started reading up on bash autocompletion, and all the examples I've seen so far use the bash `complete` builtin. So I'm guessing this is something git-specific? – Ajedi32 Jun 20 '14 at 13:23
  • Autocompletion is a shell feature. It's also fully programmable. You can look for your git completion file in `/etc/bash-completion.d` or `/usr/share/bash-completion` to see how it's done. – kaman Jun 23 '14 at 15:01
  • That file is 2000+ lines long. This behavior (where I can just define a function to make autocomplete work) isn't documented anywhere other than the source code itself? – Ajedi32 Jun 23 '14 at 15:34
  • Oh I thought you want to know how git completion completes commands. Because basically what shell does is just looking for function that is named exactly what you typed (with underscores). So you define a function that is named like your alias and call the completion function for `git log`. For more information about completion you can check http://www.gnu.org/software/bash/manual/bash.html#Programmable-Completion – kaman Jun 23 '14 at 18:33