繁体   English   中英

bash别名中的Git自动完成?

[英]Git autocomplete in bash aliases?

我使用go作为git checkout branchname的简单 bash 别名。 我想念的是与完整的git checkout branchna...命令一起使用的自动完成功能,但不在别名中。

有没有办法指示 Bash “继承”另一个命令的自动完成“驱动程序”?

使用complete -F

complete -F _git_checkout go

go后跳格可能会导致:

bash: [: 1: unary operator expected

而不是complete ,使用__git_complete

这是 git bash 补全为此目的的内置函数。

声明别名后,将正确的自动完成功能绑定到它:

# 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

如果您可以找出原始命令使用的完成功能,则可以使用complete -F将其分配给别名。

例如,在我的 ubuntu 机器上, git checkout使用的完成函数是_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/ 

后:

[me@home]$$ complete -F _git_checkout go

[me@home]$$ go <TAB><TAB>
HEAD            master          origin/HEAD     origin/master 

在 Ubuntu 18.04(仿生)上,以下工作。 将类似此代码段的内容(使用您的别名)添加到您首选的 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

通常__git_complete指令的格式如下:

__git_complete <YOUR ALIAS> _git_<GIT COMMAND NAME>

这将现有答案中的智慧结合在一个最新的答案中,谢谢大家。

在 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

添加到其他出色的答案:通常您有很多 Git 别名,手动转发所有别名的完成可能很乏味。 这是一个自动执行此操作的小技巧:

if [ -f "/usr/share/bash-completion/completions/git" ]; then
  # Enable Git completions for aliases
  . /usr/share/bash-completion/completions/git
  for a in $(alias | sed -n 's/^alias \(g[^=]*\)=.git .*/\1/p'); do
    c=$(alias $a | sed 's/^[^=]*=.git \([a-z0-9\-]\+\).*/\1/' | tr '-' '_')
    if set | grep -q "^_git_$c *()"; then
      eval "__git_complete $a _git_$c"
    fi
  done
fi

正如其他人回答的那样,您应该使用__git_complete ,否则脚本将失败。

alias g="git"
__git_complete g __git_main

alias g="gl"
__git_complete gl _git_log

但是你不应该使用_git作为主命令,它是__git_main

不幸的是,很多关于完成的信息都被隐藏了,但你可以在我的 fork 的 README 中找到更多信息: git-completion

在 Linux Mint 上,接受的答案对我不起作用。 我得到了bash: [: 1: unary operator expected

我发现以下链接的响应效果很好 - 用户提供的故障排除部分非常有帮助。 https://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases

我知道您是在专门询问 bash 别名,但对于那些来这里寻找bash中复杂 git 别名的自动完成功能的人,请参阅此处

尤其是:

# 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 ; ... '".

对于macOS ,运行以下命令来安装 bash 补全

 brew install bash-completion

然后添加以下内容

[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh" 

到您的 .bashrc 或 .bash_profile

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM