简体   繁体   English

bash别名中的Git自动完成?

[英]Git autocomplete in bash aliases?

I'm using go as a simple bash alias for git checkout branchname .我使用go作为git checkout branchname的简单 bash 别名。 The thing that I miss is the autocomplete feature that works with the full git checkout branchna... command, but not in the alias.我想念的是与完整的git checkout branchna...命令一起使用的自动完成功能,但不在别名中。

Is there a way to instruct Bash to "inherit" the autocomplete "driver" for another command?有没有办法指示 Bash “继承”另一个命令的自动完成“驱动程序”?

After using complete -F :使用complete -F

complete -F _git_checkout go

Tabbing after go may result in: go后跳格可能会导致:

bash: [: 1: unary operator expected

Instead of complete , use __git_complete而不是complete ,使用__git_complete

This is git bash completion's built-in function for this purpose.这是 git bash 补全为此目的的内置函数。

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

If you can find out the completion function used by the original command, you can assign it to the alias using complete -F .如果您可以找出原始命令使用的完成功能,则可以使用complete -F将其分配给别名。

For example, on my ubuntu box, the completion function used by git checkout is _git_checkout (found in /etc/bash_complete.d/git ).例如,在我的 ubuntu 机器上, git checkout使用的完成函数是_git_checkout (在/etc/bash_complete.d/git中找到)。

Example例子

Before running complete -F :在运行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/ 

After:后:

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

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

On Ubuntu 18.04 (Bionic) the following works.在 Ubuntu 18.04(仿生)上,以下工作。 Add something like this snippet (with your aliases) to your preferred bash configuration file eg .bashrc , .bash_aliases .bash_profile .将类似此代码段的内容(使用您的别名)添加到您首选的 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

In general the format of the __git_complete directive is the following:通常__git_complete指令的格式如下:

__git_complete <YOUR ALIAS> _git_<GIT COMMAND NAME>

This combines wisdom from the existing answers in a single up-to-date answer, thank you all.这将现有答案中的智慧结合在一个最新的答案中,谢谢大家。

In Ubuntu 16.04.3 LTS, the file I needed to source was /usr/share/bash-completion/completions/git .在 Ubuntu 16.04.3 LTS 中,我需要获取的文件是/usr/share/bash-completion/completions/git So in .bash_custom (or .bashrc, whatever):所以在.bash_custom (或 .bashrc 等)中:

[ -f /usr/share/bash-completion/completions/git ] && . /usr/share/bash-completion/completions/git
__git_complete g __git_main

To add to other excellent answers: normally you have a lot of Git aliases and it may be tedious to manually forward completions for all of them.添加到其他出色的答案:通常您有很多 Git 别名,手动转发所有别名的完成可能很乏味。 Here's a small trick to do this automatically:这是一个自动执行此操作的小技巧:

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

As somebody else answered you should use __git_complete , otherwise the script will fail.正如其他人回答的那样,您应该使用__git_complete ,否则脚本将失败。

alias g="git"
__git_complete g __git_main

alias g="gl"
__git_complete gl _git_log

But you shouldn't use _git for the main command, it's __git_main .但是你不应该使用_git作为主命令,它是__git_main

Unfortunately a lot of information about the completion is hidden, but you can find more on the README of my fork: git-completion .不幸的是,很多关于完成的信息都被隐藏了,但你可以在我的 fork 的 README 中找到更多信息: git-completion

On Linux Mint, the accepted answer didn't work for me.在 Linux Mint 上,接受的答案对我不起作用。 I was getting bash: [: 1: unary operator expected .我得到了bash: [: 1: unary operator expected

I found the following linked response worked quite well - with the troubleshooting section the user provided to be quite helpful.我发现以下链接的响应效果很好 - 用户提供的故障排除部分非常有帮助。 https://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases https://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases

I realize you're specifically asking about bash aliases, but for those coming here looking for autocomplete in bash for complex git aliases, see here .我知道您是在专门询问 bash 别名,但对于那些来这里寻找bash中复杂 git 别名的自动完成功能的人,请参阅此处

In particular:尤其是:

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

For macOS , run the following to install bash completion对于macOS ,运行以下命令来安装 bash 补全

 brew install bash-completion

Then add the following然后添加以下内容

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

to your .bashrc or .bash_profile到您的 .bashrc 或 .bash_profile

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

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