簡體   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