kch*_*kch 185 unix git bash shell autocomplete
例证:
我是一个使用bash v3.2.17的mac,我正在使用通过macports安装的git和bash_completion变体.
当我输入git checkout m<tab>
.例如,我把它完成了master
.
但是,我有一个别名git checkout
,gco
.当我输入时gco m<tab>
,我没有自动完成分支名称.
理想情况下,我希望自动完成只是神奇地为我的所有别名工作.可能吗?如果做不到这一点,我想为每个别名手动定制它.那么,我该怎么做呢?
小智 173
如上面的评论所述,
complete -o default -o nospace -F _git_checkout gco
Run Code Online (Sandbox Code Playgroud)
将不再有效.但是,__git_complete
git-completion.bash中有一个函数可用于设置别名的完成,如下所示:
__git_complete gco _git_checkout
Run Code Online (Sandbox Code Playgroud)
Hes*_*her 53
我也遇到了这个问题,并提出了这段代码.这将自动为您完成所有别名.在声明所有(或任何)别名后运行它.
# wrap_alias takes three arguments:
# $1: The name of the alias
# $2: The command used in the alias
# $3: The arguments in the alias all in one string
# Generate a wrapper completion function (completer) for an alias
# based on the command and the given arguments, if there is a
# completer for the command, and set the wrapper as the completer for
# the alias.
function wrap_alias() {
[[ "$#" == 3 ]] || return 1
local alias_name="$1"
local aliased_command="$2"
local alias_arguments="$3"
local num_alias_arguments=$(echo "$alias_arguments" | wc -w)
# The completion currently being used for the aliased command.
local completion=$(complete -p $aliased_command 2> /dev/null)
# Only a completer based on a function can be wrapped so look for -F
# in the current completion. This check will also catch commands
# with no completer for which $completion will be empty.
echo $completion | grep -q -- -F || return 0
local namespace=alias_completion::
# Extract the name of the completion function from a string that
# looks like: something -F function_name something
# First strip the beginning of the string up to the function name by
# removing "* -F " from the front.
local completion_function=${completion##* -F }
# Then strip " *" from the end, leaving only the function name.
completion_function=${completion_function%% *}
# Try to prevent an infinite loop by not wrapping a function
# generated by this function. This can happen when the user runs
# this twice for an alias like ls='ls --color=auto' or alias l='ls'
# and alias ls='l foo'
[[ "${completion_function#$namespace}" != $completion_function ]] && return 0
local wrapper_name="${namespace}${alias_name}"
eval "
function ${wrapper_name}() {
let COMP_CWORD+=$num_alias_arguments
args=( \"${alias_arguments}\" )
COMP_WORDS=( $aliased_command \${args[@]} \${COMP_WORDS[@]:1} )
$completion_function
}
"
# To create the new completion we use the old one with two
# replacements:
# 1) Replace the function with the wrapper.
local new_completion=${completion/-F * /-F $wrapper_name }
# 2) Replace the command being completed with the alias.
new_completion="${new_completion% *} $alias_name"
eval "$new_completion"
}
# For each defined alias, extract the necessary elements and use them
# to call wrap_alias.
eval "$(alias -p | sed -e 's/alias \([^=][^=]*\)='\''\([^ ][^ ]*\) *\(.*\)'\''/wrap_alias \1 \2 '\''\3'\'' /')"
unset wrap_alias
Run Code Online (Sandbox Code Playgroud)
Chr*_*oyd 18
在git-completion.bash
有一行:
complete -o default -o nospace -F _git git
Run Code Online (Sandbox Code Playgroud)
查看该行(和_git函数),您可以将此行添加到.bash_profile
:
complete -o default -o nospace -F _git_checkout gco
Run Code Online (Sandbox Code Playgroud)
won*_*unk 15
我有别名g ='git',并结合我的git别名我输入类似的东西
$ g co <branchname>
Run Code Online (Sandbox Code Playgroud)
对我的特定用例更简单的修复是向git-completion添加一行.
在这一行的正下方:
__git_complete git _git
Run Code Online (Sandbox Code Playgroud)
我添加了这一行来处理我的单个'g'别名:
__git_complete g _git
Run Code Online (Sandbox Code Playgroud)
Cyk*_*ker 13
理想情况下,我希望自动完成只是神奇地为我的所有别名工作.可能吗?
是的,可以使用complete-alias项目(在Linux上).对Mac的支持是实验性的,但用户已报告成功.
您也可以尝试使用 Git 别名。例如,在我的~/.gitconfig
文件中,我有一个如下所示的部分:
[alias]
co = checkout
Run Code Online (Sandbox Code Playgroud)
所以你可以输入git co m<TAB>
,它应该扩展到git co master
,这是git checkout
命令。
此论坛页面显示了一个解决方案
将这些行放入您的.bashrc
或.bash_profile
:
# Author.: Ole J
# Date...: 23.03.2008
# License: Whatever
# Wraps a completion function
# make-completion-wrapper <actual completion function> <name of new func.>
# <command name> <list supplied arguments>
# eg.
# alias agi='apt-get install'
# make-completion-wrapper _apt_get _apt_get_install apt-get install
# defines a function called _apt_get_install (that's $2) that will complete
# the 'agi' alias. (complete -F _apt_get_install agi)
#
function make-completion-wrapper () {
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$1"
shift 2
local function="
function $function_name {
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" \${COMP_WORDS[@]:1} )
"$comp_function_name"
return 0
}"
eval "$function"
}
# and now the commands that are specific to this SO question
alias gco='git checkout'
# we create a _git_checkout_mine function that will do the completion for "gco"
# using the completion function "_git"
make-completion-wrapper _git _git_checkout_mine git checkout
# we tell bash to actually use _git_checkout_mine to complete "gco"
complete -o bashdefault -o default -o nospace -F _git_checkout_mine gco
Run Code Online (Sandbox Code Playgroud)
这个解决方案类似于balshetzer的脚本,但只有这个实际上适合我.(balshetzer的剧本在我的一些别名上有问题.)
另一种选择是使用~/.bash_completion
文件。要创建gco
别名,git checkout
只需将其放在其中:
_xfunc git __git_complete gco _git_checkout
Run Code Online (Sandbox Code Playgroud)
然后,~/.bashrc
您只需要输入别名本身:
alias gco='git checkout'
Run Code Online (Sandbox Code Playgroud)
两行。而已。
说明:
这些~/bash_completion
获取来自主bash_completion脚本末尾。在gentoo中,我在中找到了主要脚本/usr/share/bash-completion/bash_completion
。
该_xfunc git
位负责git-completion
为您寻找文件,因此您无需放入任何其他内容~/.bashrc
。
接受的答案要求您.git-completion.sh
从~/.bashrc
我认为很and脚的文件中复制并获取它。
PS:我仍在尝试弄清楚如何不将整个git-completion
脚本提供给我的bash环境。如果找到方法,请发表评论或编辑。