Custom git command autocompletion
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.
How can I make tab completion for my Git alias behave like an existing command?
Using an alias
Let's assume a minimal version of your script, something like
#!/usr/bin/env bash
git diff "$@"
This is executable and somewhere in your $PATH
. Let's call it gitreview
.
To get an alias for it, you add it like this in your .gitconfig
:
[alias]
review = "!f() { gitreview \"$@\"; }; f"
This gets you completion for review
when you enter git
. Now, to make it behave the same as git checkout
, you can use a null command, like this:
review = "!f() { : git checkout ; gitreview \"$@\"; }; f"
and it'll complete the same as git checkout
! Notice that the blank between checkout
and ;
is required.
This is mentioned in the comments of git-completion.bash
:
# If you use complex aliases of form '!f() { ... }; f', you can use the null
# command ':' as the first command in the function body to declare the desired
# completion style. For example '!f() { : git commit ; ... }; f' will
# tell the completion to use commit completion. This also works with aliases
# of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
Using an external command
Alternatively, you can make your script a proper external command by naming it git-review
and leave it somewhere in your $PATH
. To get completion for it (assuming you have both Bash and Git completion), add this to your ~/.bash_completion
file (just create it if it doesn't exist):
_git_review() { _git_checkout; }
Git completion (for Git 2.18 or newer) checks for completion functions that are named _git_$command
, and by doing this, you say "just call _git_checkout
instead".
How can I set up autocompletion for Git commands?
You need to source /etc/bash_completion.d/git
to enable git auto-completion.
In my .bashrc
it's done with:
for file in /etc/bash_completion.d/* ; do
source "$file"
done
How to add custom git command to zsh completion?
The default git completion is extendible:
Say you got your own git sub-commands (git will run a program `git-foo'
when you run "git foo") and you want "git f" to complete that sub
commands name for you. You can make that sub-command known to the completion
via the user-command style:% zstyle ':completion:*:*:git:*' user-commands foo:'description for foo'
`user-commands' is a list style, so you can add any number of programs there.
The :description part is optional, so you could add all git-* programs from
your $path like this:% zstyle ':completion:*:*:git:*' user-commands ${${(M)${(k)commands}:#git-*}/git-/}
That is, it suffices to add
zstyle ':completion:*:*:git:*' user-commands new-branch:'custom new branch function'
to your zshrc
.
If you would like to handle parameters to your custom command as well, then it is a better solution to use a custom compdef file. The file referenced above has some details on that as well: Just create a standard definition file defining a git-<yourcommand>
function, the default git completion will use it automatically when needed.
Git autocomplete for custom bash functions
Manually-crafted bash completion is as simple as this:
# our handler that returns choices by populating Bash array COMPREPLY
# (filtered by the currently entered word ($2) via compgen builtin)
_gitpull_complete() {
branches=$(git branch -l | cut -c3-)
COMPREPLY=($(compgen -W "$branches" -- "$2"))
}
# we now register our handler to provide completion hints for the "gitpull" command
complete -F _gitpull_complete gitpull
After sourcing the above commands:
$ gitpull <TAB>
asd master qwe zxc
$ gitpull m<TAB>
$ gitpull master
The ultimate reference on bash completion is (of course) the section on Programmable Completion in the bash manual, but a nice introduction is given on "Debian Administration" page (part 1 and a more important part 2).
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
Git auto completion behaving strangely
Check first if the issue persists with Git 2.24, considering 2.23 and 2.24 have worked on the completion script.
Check also if your Git bash completion is correctly installed.
I added in the comments:
There a possibility for your .bashrc/.profile
to have somehow an error which would manifest when running that completion bash script.
You need to test it with a minimal (almost empty) content for your .bashrc/.profile
("empty", beside defining at least the $PATH
)
The OP alamoot confirms:
Great call!
In my~/.bash_profile
I was calling a script which was setting a custom$BASH_COMPLETION
and$BASH_COMPLETION_DIR
.
This custom script was part of and old "system setup" I don't need no more.
So taking it out I have got git completion working again.
Related Topics
What Is File Hole and How Can It Be Used
Linux Udp Max Size of Receive Buffer
Differencebetween Module_Init and Init_Module in a Linux Kernel Module
Ubuntu Equivalent of Yum's Whatprovides, to Find Which Package Provides a File
Writing Memory of the Traced Process
Git - Windows and Linux Line-Endings
Strange Return Value "134" to Call Gawk in a Bash Script
Can an Interrupt Handler Be Preempted
How to Trace Per-File Io Operations in Linux
The Address Where Filename Has Been Loaded Is Missing [Gdb]
Trying to Find All the Kernel Modules Needed for My MAChine Using Shell Script
Debugging in Pycharm with Sudo Privileges
Nasm Linux Assembly Printing Integers
Iterating Over Lists in Makefiles
Shell Script to Copy and Prepend Folder Name to Files from Multiple Subdirectories
Calculating Rounded Percentage in Shell Script Without Using "Bc"