builtin completion
The default completion for git clone (reproduced below) gives tab completion for --*
options:
_git_clone ()
{
case "$cur" in
--*)
__gitcomp_builtin clone
return
;;
esac
}
bash-completion 1.x (old bash)
(for a concrete instance, macos high sierra + brew installed bash-completion / git)
In the bash-completion 1.x world, to override this I would (in .bashrc
/ .bash_profile
) define my own _git_clone
completion function:
# https://github.com/scop/bash-completion/blob/d2f14a7/bash_completion#L498
__ltrim_colon_completions() {
if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
# Remove colon-word prefix from COMPREPLY items
local colon_word=${1%"${1##*:}"}
local i=${#COMPREPLY[*]}
while [[ $((--i)) -ge 0 ]]; do
COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
done
fi
}
_git_clone() {
case "$cur" in
--*)
__gitcomp_builtin clone
return
;;
*)
argc=0
for word in "${words[@]}"; do
case "$word" in
git|clone|--*)
continue
;;
*)
argc=$((argc + 1))
;;
esac
done
if [ $argc -le 1 ]; then
__gitcomp "https://github.com/git/git https://github.com/python/cpython"
__ltrim_colon_completions "$cur"
fi
;;
esac
}
This works great:
(The sequence I typed here was git clone h<tab><tab>g<tab>
)
$ git clone https://github.com/
//github.com/git/git //github.com/python/cpython
$ git clone https://github.com/git/git
bash-completion 2.x
(for a concrete instance: stock ubuntu bionic (18.04))
In bash-completion 2.x, the model is flipped to a dynamically loaded configuration. This means that when git
is tab completed, __load_completion
fires, finds the git completion at the path it is installed and sources it.
Defining my own _git_clone
completion function in a .bashrc
/ .bash_profile
is now useless as it gets clobber by the dynamically sourced completion file.
I can define my own git
completion in this directory:
local -a dirs=( ${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions )
(for example ~/.local/share/bash-completion/completions/git.bash
). However this turns off all other git
completion!
How do I make my custom clone
tab completion work under this model (and have the default completion continue to work)?
Unacceptable solution(s):
- Modify system packaged files:
/usr/share/bash-completion/completions/git
. This file is managed byapt
.
__load_completion
yourself, and then override like you used to? – Britisher