210

一个例子:

我是使用 bash v3.2.17 的 mac,我使用的是通过 macports 安装的带有 bash_completion 变体的 git。

当我键入git checkout m<tab>. 例如,我把它完成到master.

但是,我有 , 的git checkout别名gco。当我输入时gco m<tab>,我没有自动完成分支名称。

理想情况下,我希望自动完成功能可以神奇地适用于我的所有别名。是否可以?如果做不到这一点,我想为每个别名手动自定义它。那么,我该怎么做呢?

4

14 回答 14

192

正如上面评论中所说,

complete -o default -o nospace -F _git_checkout gco

将不再工作。但是,__git_completegit-completion.bash 中有一个函数可用于为别名设置完成,如下所示:

__git_complete gco _git_checkout
于 2013-02-21T18:25:48.963 回答
56

我也遇到了这个问题,并想出了这个代码片段。这将自动为您完成所有别名。在声明所有(或任何)别名后运行它。

# 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
于 2009-11-24T21:52:45.397 回答
19

git-completion.bash里面有一行:

complete -o default -o nospace -F _git git

查看该行(和 _git 函数),您可以将此行添加到您的.bash_profile

complete -o default -o nospace -F _git_checkout gco
于 2008-12-05T05:55:56.090 回答
19

理想情况下,我希望自动完成功能可以神奇地适用于我的所有别名。是否可以?

是的,完全别名项目(在 Linux 上)是可能的。对 Mac 的支持是实验性的,但用户报告成功。

于 2016-12-24T07:15:13.630 回答
15

我给 g='git' 起了别名,并结合我的 git 别名,我输入了类似的东西

$ g co <branchname>

对于我的特定用例,更简单的解决方法是在 git-completion 中添加一行。

在这条线的正下方:

__git_complete git _git

我添加了这一行来处理我的单个“g”别名:

__git_complete g _git
于 2012-05-22T18:07:22.673 回答
7

另一种选择是使用~/.bash_completion文件。要创建gco别名,git checkout只需将其放在那里:

_xfunc git __git_complete gco _git_checkout

然后~/.bashrc你必须把别名本身:

alias gco='git checkout'

两条线。就是这样。

解释:

~/bash_completion获取源在主 bash_completion 脚本的末尾。在 gentoo 中,我在/usr/share/bash-completion/bash_completion.

_xfunc git位负责git-completion为您获取文件,因此您无需在~/.bashrc.

接受的答案要求您从我觉得蹩脚的文件中复制.git-completion.sh和获取它。~/.bashrc


PS:我仍在试图弄清楚如何不将整个git-completion脚本源到我的 bash 环境中。如果您找到方法,请发表评论或编辑。

于 2016-02-26T18:52:52.810 回答
6

您也可以尝试使用 Git 别名。例如,在我的~/.gitconfig文件中,我有一个如下所示的部分:

[alias]
        co = checkout

因此,您可以键入git co m<TAB>,并且应该扩展为git co master,这是git checkout命令。

于 2008-12-05T15:39:26.363 回答
5

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

将这些行放入您的.bashrcor中.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 的脚本,但只有这个对我有用。(balshetzer 的脚本对我的一些别名有问题。)

于 2010-01-16T18:19:33.010 回答
3

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

我有alias d-m="docker-machine"。换句话说,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
于 2016-11-07T11:07:37.803 回答
2

首先,查找原始完成命令。例子:

$ complete | grep git

complete -o bashdefault -o default -o nospace -F __git_wrap__git_main git

现在将这些添加到您的启动脚本中(例如 ~/.bashrc):

# copy the original statement, but replace the last command (git) with your alias (g)
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main g

# load dynamically loaded completion functions (may not be required)
_completion_loader git

_completion_loader行可能不是必需的。但在某些情况下,补全功能只有在你输入命令并TAB第一次按下后才会动态加载。因此,如果您没有使用原始命令,并尝试使用别名 + TAB,您可能会收到类似“bash: completion: function '_docker' not found”的错误。

于 2018-07-26T22:50:04.973 回答
1

这个问题有很多答案,像我自己一样,我敢打赌很多困惑的读者。对于我的情况,我还需要让我的点文件在具有不同版本 Git 的多个平台上工作。我也没有alias g=git,而是g定义为一个函数。

为了做到这一点,我不得不在这里将不同的答案整合到一个解决方案中。尽管这已经重申了答案,但我认为我船上的某个人可能会发现这个汇编很有用,就像我第一次遇到这个问题时一样。

这假设较旧和较新的 Git 完成、Ubuntu 默认设置和brew install gitMacOS 上。在后一种情况下,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
于 2019-03-19T12:35:14.523 回答
1

您可以绑定Tabalias-expand-linecomplete(其默认操作)在~/.inputrc. 为此,您首先需要将每个操作绑定到一个键,然后将它们链接在一起:

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

您可以使用任何您喜欢的组合键,我使用 Meta 组合键,因为它是免费的。有关man 3 readline更多信息,请参阅。

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

gco m<TAB>

该行将转换为

git checkout master

当然,Tab即使不涉及别名,它仍然会照常工作。

于 2021-03-10T12:38:04.993 回答
0

如果您使用alias g='git',我将这行代码添加到.bash_aliases

complete -o default -o nospace -F _git g
于 2019-04-17T18:55:20.727 回答
0

Felipe Contreras已经非常活跃于 Git 补全功能(请参阅Git 2.30 中的 Zsh 补全)提出(可能为 Git 2.31,2021 年第一季度)一个有助于别名自动补全的公共功能。

他的提议:

早在 2012 年,我就主张引入一个帮助程序,允许用户指定别名,例如:

git_complete gf git_fetch

那时因为公共功能(git_completevs _git_completevs _GIT_complete)没有明确的指导方针而遭到抵制,而且一些别名实际上并没有起作用。

快进到 2020 年,公共功能仍然没有指南,而且这些别名仍然不起作用(即使我发送了修复程序)。

这并没有阻止人们使用显然需要设置自定义别名(此页面)的此功能,实际上这是推荐的方式。

但是用户必须键入很麻烦:

__git_complete gf _git_fetch

或者更糟:

__git_complete gk __gitk_main

8年的时间,足以让你不再等待完美的到来;让我们定义一个实际上对用户友好的公共函数(同名):

__git_complete gf git_fetch
__git_complete gk gitk

同时还保持向后兼容性。

逻辑是:

  1. 如果$2存在,直接使用
  2. 如果不存在,请检查是否__$2_main存在
  3. 如果不存在,请检查是否_$2存在
  4. 如果没有,失败
于 2020-12-29T01:13:42.920 回答