Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom git command autocompletion

Tags:

git

linux

shell

I have implemented a custom git command by writing a shell script located in /usr/local/bin. It works fine, but I would like the script to autocomplete branches in the command line, just like git checkout [TAB][TAB]. How could this be done?

EDIT: Just adding some context: git allows you to very easily add your own commands by creating a script git-$subcommandName. In my case here git-install, the script facilitates checking out a branch, building, packaging, and installing the source code.

like image 455
primitivist Avatar asked Dec 23 '16 20:12

primitivist


People also ask

Why is git autocompletion useful?

It can really save you a lot of typing. If there is more than one possibility, then it will let you know that it needs more information and you can type a little bit more of the word and try again. Git auto-completion is a separate script, and it's included inside the Git source code repository on GitHub.


1 Answers

Figured it out. I needed to download git bash completion (here), create a new file in /etc/bash_completion.d with the following contents:

source ~/.git-completion.bash
_git_install ()
{
  __gitcomp_nl "$(__git_refs)"
}

and then exec bash to reload completion scripts.

like image 93
primitivist Avatar answered Oct 13 '22 03:10

primitivist