How do I get bash completion to work with aliases?

后端 未结 12 1068
温柔的废话
温柔的废话 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:06

    There are a lot of answers to this question and like myself I bet a lot of confused readers. For my case I had also had the requirement to have my dotfiles work on multiple platforms with different versions of Git. I also don't alias g=git but instead have g defined as a function.

    To accomplish this I had to slap together different answers here into one solution. Although this reiterates the answers already I thought someone in my boat might find this compilation useful as I would have when I first came to this question.

    This assumes older and newer Git completion, Ubuntu defaults, and brew install git on MacOS. In the later case the brew installed completions were not being processed by bash (something I will diagnose later).

    # 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
    

提交回复
热议问题