举个例子:

我是一个在mac上的bash v3.2.17,我使用的git安装通过macports与bash_completion变体。

当我输入git checkout m<tab>。例如,我把它完成了。

然而,我有一个别名git结帐,gco。当我输入gco m<tab>时,我没有得到自动完成的分支名称。

理想情况下,我想自动完成只是神奇地为我所有的别名工作。这可能吗?如果做不到这一点,我想为每个别名手动定制它。那么,我该怎么做呢?


当前回答

我知道这个问题是关于bash的,但如果你使用zsh,这个改变@hesky-fisher回答将修复它。

From

# 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'\'' /')"

to:

# For each defined alias, extract the necessary elements and use them
# to call wrap_alias.
eval "$(alias | sd '^([^=]+)=(.+)' 'wrap_alias $1=$2')"

你需要sd(这可以用sed代替,但sd在我看来更好)

这样做是因为zsh没有别名-p,并且zsh中的别名输出不输出

alias <something>=<value>

就像在狂欢,但是

<something>=<value>

顺便说一下,我把初始化代码放在。zlogin文件中

其他回答

这个问题有很多答案,就像我自己一样,我打赌很多读者都很困惑。对于我来说,我还需要让我的dotfile在多个平台上使用不同版本的Git。我也没有使用g=git的别名,而是将g定义为一个函数。

为了做到这一点,我必须把不同的答案拼凑成一个解决方案。虽然这重复了之前的答案,但我认为在我的船上的人可能会发现这个汇编很有用,就像我第一次遇到这个问题时一样。

这假设旧的和新的Git完成,Ubuntu默认,并在MacOS上brew安装Git。在后一种情况下,bash没有处理brew安装完成(稍后我将对此进行诊断)。

# Alias g to git

g() {
  if [[ $# > 0 ]]; then
    git "$@"
  else
    git status -sb
  fi
}

# Preload git completion in Ubuntu which is normally lazy loaded but we need
# the __git_wrap__git_main function available for our completion.
if [[ -e /usr/share/bash-completion/completions/git ]]; then
  source /usr/share/bash-completion/completions/git
elif [[ -e /usr/local/etc/bash_completion.d/git-completion.bash ]]; then
  source /usr/local/etc/bash_completion.d/git-completion.bash
fi

if command_exists __git_complete; then
  __git_complete g _git
elif command_exists __git_wrap__git_main; then
  complete -o bashdefault -o default -o nospace -F __git_wrap__git_main g
fi

您可以将Tab绑定到alias-expand-line并在~/.inputrc中完成(它的默认动作)。要做到这一点,你首先需要将每个动作绑定到一个键,然后将它们链接在一起:

"\M-z":alias-expand-line
"\M-x":complete
TAB:"\M-z\M-x"

你可以使用任何你喜欢的组合键,我使用元键,因为它是免费的。更多信息见男子3阅读线。

现在,如果你打开一个新终端,输入别名:

gco m<TAB>

线路将被改造成

git checkout master

当然,即使不涉及别名,Tab也将照常工作。

这个论坛页面展示了一个解决方案。

把这些行放到你的.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

这个解决方案类似于balshetzer的脚本,但只有这个对我有效。(巴尔谢策的脚本对我的一些别名有问题。)

您只需要找到完整的命令并复制具有别名的行即可。

我有别名d-m=“码头机”。d-m是docker-machine的别名。

所以在Mac上(通过brew),补全文件在cd ' brew——prefix ' /etc/bash_completion.d/。 对于我的案例,我编辑了名为docker-machine的文件。 在最下面是:

complete -F _docker_machine docker-machine

所以我添加了另一行,用我的别名:

complete -F _docker_machine docker-machine
complete -F _docker_machine d-m

在git-completion。Bash有一行:

complete -o default -o nospace -F _git git

查看这一行(和_git函数),你可以将这一行添加到你的.bash_profile:

complete -o default -o nospace -F _git_checkout gco