Git autocomplete in bash aliases?
After using complete -F
:
complete -F _git_checkout go
Tabbing after go
may result in:
bash: [: 1: unary operator expected
Instead of complete
, use __git_complete
This is git bash completion's built-in function for this purpose.
After declaring your alias, bind the correct auto-complete function to it:
# Main git completions (prior to git 2.30, you an use _git instead of __git_main)
alias g="git"
__git_complete g __git_main
alias go="git checkout"
__git_complete go _git_checkout
alias gp="git push"
__git_complete gp _git_push
If you can find out the completion function used by the original command, you can assign it to the alias using complete -F
.
For example, on my ubuntu box, the completion function used by git checkout
is _git_checkout
(found in /etc/bash_complete.d/git
).
Example
Before running complete -F
:
[me@home]$ git checkout <TAB><TAB>
HEAD master origin/HEAD origin/master
[me@home]$ alias go="git checkout"
[me@home]$$ go <TAB><TAB>
.git/ precommit_config.py README.md SvnSentinel/
.gitignore precommit.py startcommit.py tests/
After:
[me@home]$$ complete -F _git_checkout go
[me@home]$$ go <TAB><TAB>
HEAD master origin/HEAD origin/master