210

適例:

私はbash v3.2.17を搭載したMacを使用しています.bash_completionバリアントでmacportsを介してインストールされた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_completeようにエイリアスの補完を設定するために使用できる関数が git-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

理想的には、オートコンプリートがすべてのエイリアスに対して魔法のように機能することを望みます。出来ますか?

はい、complete-aliasプロジェクト (Linux 上) で可能です。Mac のサポートは実験段階ですが、ユーザーは成功を報告しています。

于 2016-12-24T07:15:13.630 に答える
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 に答える
15

g='git' にエイリアスを設定し、git エイリアスと組み合わせて次のように入力します

$ g co <branchname>

私の特定のユースケースのより簡単な修正は、git-completion に 1 行追加することでした。

この行のすぐ下:

__git_complete git _git

単一の「g」エイリアスを処理するために、次の行を追加しました。

__git_complete g _git
于 2012-05-22T18:07:22.673 に答える
7

もう 1 つのオプションは、~/.bash_completionファイルを使用することです。これをそこに入れるだけのgcoエイリアスを作成するには:git checkout

_xfunc git __git_complete gco _git_checkout

次に~/.bashrc、エイリアス自体を配置する必要があります。

alias gco='git checkout'

二行。それでおしまい。

説明:

は、メインの~/bash_completionbash_completion スクリプトの最後に供給されます。gentoo では、メイン スクリプトが/usr/share/bash-completion/bash_completion.

_xfunc gitビットがファイルのソースを処理するgit-completionので、~/.bashrc.

.git-completion.sh受け入れられた回答では、ファイルからコピーして入手する必要がありますが、~/.bashrcこれは不十分だと思います。


git-completionPS:スクリプト全体を bash 環境にソースしない方法をまだ見つけようとしています。方法が見つかった場合は、コメントまたは編集してください。

于 2016-02-26T18:52:52.810 に答える
6

Gitエイリアスを使用することもできます。たとえば、私の~/.gitconfigファイルには、次のようなセクションがあります。

[alias]
        co = checkout

したがって、と入力すると、コマンドであるにgit co m<TAB>展開されます。git co mastergit 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 のスクリプトには、私のエイリアスのいくつかに問題がありました。)

于 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 に答える