How to alias 'git' to 'g' so that bash-completion rules are preserved?
Solution 1
Copying and modifying opportunely from /etc/bash_completion.d/git
, add the following lines to your ~/.bashrc
:
complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
|| complete -o default -o nospace -F _git g
Solution 2
Latest bash-completion upstream moved and renamed things a bit. It's now:
source /usr/share/bash-completion/completions/git
__git_complete g __git_main
Use this in recent versions of Ubuntu (e.g. 14.04, also Fedora 22+) when you encounter:
completion: function `_git' not found
during completing.
Solution 3
In ~/.bashrc
:
alias g='git'
source /usr/share/bash-completion/completions/git
complete -o default -o nospace -F _git g
Via http://29a.ch/2013/8/9/fixing-bash-autocomplete-on-ubuntu-13-04
Solution 4
First, look up the original completion command. Example:
$ complete | grep git
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main git
Now add these to your startup script (e.g. ~/.bashrc):
# copy the original statement, but replace the last command (git) with your alias (g)
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main g
# load dynamically loaded completion functions (may not be required)
_completion_loader git
The _completion_loader
line may not be required. But for some situations, the completion function is only loaded dynamically after you type the command and press TAB
the first time. So if you haven't used the original command, and try the alias + TAB
, you may get an error like "bash: completion: function not found".
Solution 5
The updated way to do this (complete wouldn't work for me):
-
cd
- switch to your home directory wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash
- Add
source ~/git-completion.bash
to your .bashrc file (if you don't have this file make one in your home folder, bash will look for it automatically) - Add
alias g='git'
to your .bashrc file. - Start a new session or source your changes with
source ~/.bashrc
Related videos on Youtube
Paweł Gościcki
Full-stack Software Engineer with 20 years of professional experience. Passionate about creating high quality, reliable software. A believer in flexible and extensible systems. Specialising in back-end Ruby on Rails development, having 14 years of commercial experience building both small and large-scale web applications.
Updated on September 18, 2022Comments
-
Paweł Gościcki over 1 year
If I do this:
alias g='git'
I loose all completion rules (i.e. branches and remotes are no longer being automatically completed when I hit TAB after typing, for example
g push o
).-
Admin over 12 yearsWhile I don't have an answer for you, I'm going to have to take a minute and wonder if not typing those two other letters is really giving you much of an advantage?
-
Admin over 12 yearsSure it does! I'm typing 'g' probably over a 100 times a day.
-
Admin over 11 yearsAliasing often-used short commands to even shorter aliases saves me hundreds of keypresses a day. According to my shell history, I have used the alias
g=git
756 times in the past month, meaning I saved pressing the 'g' and 'i' keys 1512 times total. That, combined with my git aliases, probably saves me tens of thousands of key presses a month. -
Admin about 9 yearsThe main point of shortening keystrokes is helping your hands keep up with your brain.
-
Admin almost 8 yearsThe script at superuser.com/questions/436314/… worked so well I think it deserves a mention.
-
-
Lukas over 9 yearsI also had to add
source /usr/share/bash-completion/completions/git
to my .bashrc -
Paweł Gościcki almost 9 yearsSorry, but that's something completely different. Making such simple alias es for whole commands is trivial.
-
scue almost 9 yearsI just make them work like git plugin of Oh-My-Zsh: github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/…
-
pjvandehaar over 8 yearsYou skipped the part where they actually added completion in the comment you linked to:
__git_complete g _git
-
pjvandehaar over 8 yearsSeems like this is the only correct answer here.
-
Max Wallace over 8 yearsUse
__git_complete gc _git_checkout
(or similar) to set up completions for more specific aliases. Search/usr/.../git
as above to find the right name. -
Max Wallace over 8 yearsNot sure if the
.bashrc
code here works, but this answer is definitely out of date./etc/bash_completion.d/git
doesn't seem to exist on 14.04. See lzap's answer below. -
Piotr Aleksander Chmielowski over 7 years@MaxWallace - when you set the aliases for particular git methods in .gitconfig (e.g. gc = checkout), there is no need to explicitly configure completion for them.
-
Max Wallace over 7 years@PiotrAleksanderChmielowski Thanks! But I don't think that works for top-level bash aliases (e.g.
gc
=>git checkout
) as opposed to aliasingc
tocheckout
withingit
, and sayinggit c
. Correct me if I'm wrong. -
Piotr Aleksander Chmielowski over 7 years@MaxWallace Yes, you are right, my mistake ;)
-
Paul Carlton about 5 yearsWorks great. For me this is the correct answer, the grep command on complete is handy. Did not need the completion loader.