我使用的go
是一个简单的bash别名git checkout branchname
.我想念的是自动完成功能,它与完整git checkout branchna...
命令一起使用,但不在别名中.
有没有办法指示Bash"继承"另一个命令的自动完成"驱动程序"?
小智 76
使用后complete -F
:
complete -F _git_checkout go
Run Code Online (Sandbox Code Playgroud)
之后的标签go
可能会导致:
bash: [: 1: unary operator expected
Run Code Online (Sandbox Code Playgroud)
complete
,使用__git_complete
这是git bash completion的内置函数.
声明别名后,将正确的自动完成功能绑定到它:
alias g="git"
__git_complete g _git
alias go="git checkout"
__git_complete go _git_checkout
alias gp="git push"
__git_complete gp _git_push
Run Code Online (Sandbox Code Playgroud)
Sha*_*hin 39
如果您可以找到原始命令使用的完成功能,则可以使用它将其分配给别名complete -F
.
例如,在我的ubuntu框中,git checkout
is 使用的完成函数是_git_checkout
(找到/etc/bash_complete.d/git
).
在运行之前complete -F
:
[me@home]$ git checkout <TAB><TAB>
HEAD master origin/HEAD origin/master
[me@home]$ alias go="git checkout"
[me@home]$$ go <TAB><TAB>
.git/ precommit_config.py README.md SvnSentinel/
.gitignore precommit.py startcommit.py tests/
Run Code Online (Sandbox Code Playgroud)
后:
[me@home]$$ complete -F _git_checkout go
[me@home]$$ go <TAB><TAB>
HEAD master origin/HEAD origin/master
Run Code Online (Sandbox Code Playgroud)
Sea*_*usJ 12
在Ubuntu 16.04.3 LTS中,我需要提供的文件是/usr/share/bash-completion/completions/git
.所以在.bash_custom
(或.bashrc,无论如何):
[ -f /usr/share/bash-completion/completions/git ] && . /usr/share/bash-completion/completions/git
__git_complete g __git_main
Run Code Online (Sandbox Code Playgroud)
(这个答案属于对程序员的回复jangosteve的评论,但由于我需要50个代表来创建评论,我会将其作为自己的答案进行垃圾邮件.)
ada*_*ish 10
在Ubuntu 18.04(Bionic)上,以下工作正常。加入这样的片段(与你的别名)为您的首选bash的配置文件,例如.bashrc
,.bash_aliases
.bash_profile
。
# define aliases
alias gc='git checkout'
alias gp='git pull'
# setup autocompletion
if [ -f "/usr/share/bash-completion/completions/git" ]; then
source /usr/share/bash-completion/completions/git
__git_complete gc _git_checkout
__git_complete gp _git_pull
else
echo "Error loading git completions"
fi
Run Code Online (Sandbox Code Playgroud)
通常,__git_complete
伪指令的格式如下:
__git_complete <YOUR ALIAS> _git_<GIT COMMAND NAME>
Run Code Online (Sandbox Code Playgroud)
这将现有答案中的智慧整合到一个最新的答案中,谢谢大家。
正如其他人回答的那样,您应该使用__git_complete
,否则脚本将失败。
alias g="git"
__git_complete g __git_main
alias g="gl"
__git_complete gl _git_log
Run Code Online (Sandbox Code Playgroud)
但你不应该使用_git
主命令,它是__git_main
.
不幸的是,隐藏了很多有关完成的信息,但您可以在我的分支的自述文件中找到更多信息:git-completion。