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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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?
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.
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?
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 gnu.org/software/bash/manual/bash.html#Programmable-Completion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.