【问题标题】:Git completions for alias functions [duplicate]别名函数的 Git 完成 [重复]
【发布时间】:2012-06-08 15:25:51
【问题描述】:

我的 .zshrc 中有一些别名函数,例如 gb 代表 git branchgco 代表 git checkout。当我记得我正在创建、删除、签出等的完整分支名称时,这非常有用。但是,我注意到完成似乎不再起作用。以前,我可以做

$ git checkout m<TAB>

如果这是一个分支的名称,它会自动完成 master。但是,现在我在使用时收到以下错误:

$ gco m<TAB>
_git:15: parse error: condition expected: 1

我不确定为什么会发生这种情况。看来可能缺少参数,但我不确定为什么。

编辑:

我在我的 .zshrc 文件中为 git branchgit checkout 设置别名,如下所示:

alias gco='git checkout'
alias gb='git branch'

【问题讨论】:

    标签: macos git shell zsh


    【解决方案1】:

    经过一番深入研究后,我找到了针对 bash 的相同问题的解决方案,它看起来与 zsh 一样有效。在定义了__define_git_completion__git_shortcut 这两个函数之后,您将调用__git_shortcut 来设置您的别名并一次调用完成。

    __define_git_completion () {
    eval "
        _git_$2_shortcut () {
            COMP_LINE=\"git $2\${COMP_LINE#$1}\"
            let COMP_POINT+=$((4+${#2}-${#1}))
            COMP_WORDS=(git $2 \"\${COMP_WORDS[@]:1}\")
            let COMP_CWORD+=1
    
            local cur words cword prev
            _get_comp_words_by_ref -n =: cur words cword prev
            _git_$2
        }
    "
    }
    
    __git_shortcut () {
        type _git_$2_shortcut &>/dev/null || __define_git_completion $1 $2
        alias $1="git $2 $3"
        complete -o default -o nospace -F _git_$2_shortcut $1
    }
    
    __git_shortcut  ga    add
    __git_shortcut  gb    branch            # I use this one.
    __git_shortcut  gba   branch -a
    __git_shortcut  gco   checkout          # and I use this one, too.
    __git_shortcut  gci   commit -v
    __git_shortcut  gcia  commit '-a -v'
    __git_shortcut  gd    diff
    __git_shortcut  gdc   diff --cached
    __git_shortcut  gds   diff --stat
    __git_shortcut  gf    fetch
    __git_shortcut  gl    log
    __git_shortcut  glp   log -p
    __git_shortcut  gls   log --stat
    

    【讨论】:

      【解决方案2】:

      如果您希望自动完成功能也能正常工作,我相信您应该在 .gitconfig 中创建别名。但是你必须拼出 git 命令。

      [alias]
      gb = branch
      gco = checkout
      

      等等……

      你也可以像这样添加别名:

      git config –-global alias.gb branch
      

      它们将被附加到您的 .gitconfig 中

      干杯

      【讨论】:

      • 谢谢,但我相信这会为git 命令添加一个别名,因此这里的别名gb 会添加git gb m&lt;TAB&gt; 完成,但我只是尝试使用gb m&lt;TAB&gt;跨度>
      • git gco foo 并不比例如更短/更有用。 git checkout foo。它没有定义gco foo
      猜你喜欢
      • 2015-07-09
      • 2017-01-23
      • 2020-07-19
      • 2012-04-09
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多