Nxt*_*xt3 6 git bash bash-completion
在我看来.bash_profile,我有很多git的功能快捷方式.例如:
function gitpull() {
branch="$1"
if [[ -z $branch ]]; then
current_branch=`git symbolic-ref -q --short HEAD`
git pull origin $current_branch;
elif [[ -n $branch && $branch == "m" ]]; then
git pull origin master;
else
git pull origin $branch;
fi;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在终端中键入它时,我希望它能自动完成git分支.我该怎么做呢?(我已经在使用了.git-completion.bash)
手动制作的bash完成就像这样简单:
# 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
Run Code Online (Sandbox Code Playgroud)
在获取上述命令后:
$ gitpull <TAB>
asd master qwe zxc
$ gitpull m<TAB>
$ gitpull master
Run Code Online (Sandbox Code Playgroud)
关于bash完成的最终参考是(当然)bash手册中关于可编程完成的部分,但是对"Debian管理"页面(第1部分和更重要的第2部分)给出了很好的介绍.