如何使用别名来完成bash完成?

时间:2008-12-05 05:17:10

标签: git bash shell unix autocomplete

案例:

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

当我输入git checkout m<tab>时。例如,我将其完成到master

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

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

14 个答案:

答案 0 :(得分:165)

如上述评论所述,

complete -o default -o nospace -F _git_checkout gco

将不再有效。但是,git-completion.bash中有一个__git_complete函数,可用于设置别名的完成,如下所示:

__git_complete gco _git_checkout

答案 1 :(得分: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

答案 2 :(得分:18)

git-completion.bash中有一行:

complete -o default -o nospace -F _git git

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

complete -o default -o nospace -F _git_checkout gco

答案 3 :(得分:15)

我有别名的g ='git',并结合我的git别名我输入的内容如

$ g co <branchname>

我的特定用例的更简单的修复是向git-completion添加一行。

在这一行的正下方:

__git_complete git _git

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

__git_complete g _git

答案 4 :(得分:12)

  

理想情况下,我喜欢自动完成,只是神奇地为我的所有别名工作。有可能吗?

是的,可以使用complete-alias项目(在Linux上)。对Mac的支持是实验性的,但用户已经报告成功。

答案 5 :(得分:5)

This forum page显示了解决方案。

将这些内容放入.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's script,但只有这一个实际上适合我。 (balshetzer的剧本在我的一些别名上有问题。)

答案 6 :(得分:5)

另一个选择是使用~/.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环境。如果找到方法,请评论或编辑。

答案 7 :(得分:4)

您也可以尝试使用Git别名。例如,在我的~/.gitconfig文件中,我有一个看起来像这样的部分:

[alias]
        co = checkout

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

答案 8 :(得分:2)

您只需找到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

答案 9 :(得分:0)

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

$ 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:完成:未找到功能'_docker'的错误”之类的错误。

答案 10 :(得分:0)

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

要做到这一点,我不得不将不同的答案拼凑成一个解决方案。尽管这已经重申了答案,但我认为船上的某人可能会像我初次提出该问题时那样会发现该汇编很有用。

这假设已完成旧版本和较新的Git,Ubuntu默认设置以及MacOS上的brew install 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

答案 11 :(得分:0)

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

complete -o default -o nospace -F _git g

答案 12 :(得分:0)

Felipe Contreras 已经非常热衷于 Git 补全功能(请参阅 Zsh completion in Git 2.30),他提议(对于 -- 可能 -- Git 2.31,2021 年第一季度)一个有助于别名自动补全的公共函数。

他的提议:

<块引用>

Back in 2012 I argued 用于引入帮助程序 允许用户指定别名,如:

git_complete gf git_fetch

当时因为公共函数没有明确的指导方针(git_complete vs _git_complete vs _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. 如果没有,则失败

答案 13 :(得分:0)

您可以在 alias-expand-line 中将 Tab 绑定到 complete~/.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 仍会照常工作。

相关问题