【问题标题】:How do I get bash completion to work with aliases in Windows 10 bash?如何让 bash 完成以在 Windows 10 bash 中使用别名?
【发布时间】:2019-04-21 08:05:35
【问题描述】:

我有一个用

修改的 aliases.sh 文件
alias gc='git checkout'

在检查一个长分支名称时,如果我输入 gc <branchstring> + TAB ,自动完成不起作用,以便显示完整的分支名称。

【问题讨论】:

    标签: git


    【解决方案1】:

    我查看了 this thread 中的所有内容,试图使其适用于并定制 kpsfoo 所说的内容,但适用于 Windows 10 操作系统。

    步骤如下:

    1) 从

    复制 git-completion.bash 文件
    <your git install folder>/etc/git-completion.bash
    

    C:\Users\&lt;YourUserName&gt;\git-completion.bash

    2) 添加这行代码:

    source ~/git-completion.bash 到您的 aliases.sh 文件

    (可以在&lt;your git install folder&gt;\etc\profile.d找到)

    3) 添加alias gc='git checkout' & 在 aliases.sh 文件中 source ~/git-completion.bash 行之后的任意位置添加 __git_complete gco _git_checkout

    4) 重新启动您的 git bash 并享受您的别名自动完成功能!

    示例: 如果我有一个分支 VeryVeryLongBranchName 并且我目前在 dev 分支上,并且想要切换到它,而不是输入 git checkout VeryVeryLongBranchName我只能打字 gc Very +TAB 键 相当于上面的指令。

    我的 aliases.sh 文件中的所有内容的一个示例(它会被扩展,因为我发现需要其他别名)是:

    alias ga="git add"
    alias gb='git branch'
    alias gba="git branch -a"
    alias gc='git checkout'
    alias gcb='git checkout -b'
    alias gcam='git commit -a -m'
    alias gm='git merge --no-ff'
    alias gps='git push wpdev dev'
    alias gpsm='git push wpdev master'
    alias gpl='git pull wpdev dev'
    alias gplm='git pull wpdev master'
    alias st='git status'
    alias l='git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit'
    alias last='log -1 HEAD'
    alias gs='git stash'
    
    # Enable the __git_complete function to autocomplete aliases once you press TAB
    source ~/git-completion.bash
    
    __git_complete ga _git_add
    __git_complete gc _git_checkout
    __git_complete gm _git_merge
    __git_complete gb _git_branch
    __git_complete gba _git_branch
    __git_complete l _git_log
    
    case "$TERM" in
    xterm*)
        # The following programs are known to require a Win32 Console
        # for interactive usage, therefore let's launch them through winpty
        # when run inside `mintty`.
        for name in node ipython php php5 psql python2.7
        do
            case "$(type -p "$name".exe 2>/dev/null)" in
            ''|/usr/bin/*) continue;;
            esac
            alias $name="winpty $name.exe"
        done
        ;;
    esac
    

    -值得注意的是:alias gm='git merge --no-ff'__git_complete gm _git_merge 配合得很好(当输入 gm 加上分支名称中的字符串并按 TAB 时,它将自动完成, 并且,在你运行命令后,合并将考虑 --no-ff 规则)

    【讨论】:

    • 好答案,请注意zsh 无需任何手动调整即可工作。
    • 在我的 Git 版本 2.28.0.windows.1 上,git-completion.bash 位于 &lt;git-installation-folder&gt;/mingw64/share/git/completion/git-completion.bash
    猜你喜欢
    • 2018-03-19
    • 1970-01-01
    • 2012-04-09
    • 2015-08-05
    • 2015-07-09
    • 2014-08-10
    • 2016-09-03
    • 2011-11-08
    • 1970-01-01
    相关资源
    最近更新 更多