How do I get bash completion to work with aliases?

后端 未结 12 1085
温柔的废话
温柔的废话 2020-11-28 00:35

Case in point:

I\'m a on mac with bash v3.2.17, I\'m using git installed via macports with the bash_completion variant.

When I type git checkout m<

12条回答
  •  孤街浪徒
    2020-11-28 01:05

    This forum page shows a solution.

    Put these lines into your .bashrc or .bash_profile:

    # Author.: Ole J
    # Date...: 23.03.2008
    # License: Whatever
    
    # Wraps a completion function
    # make-completion-wrapper  
    #                          
    # 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
    

    This solution is similar to balshetzer's script, but only this one actually works for me. (balshetzer's script had problems with some of my aliases.)

提交回复
热议问题