Homebrew’s `git` not using completion

后端 未结 18 1860
盖世英雄少女心
盖世英雄少女心 2020-12-22 16:25

When using OSX’s git, after I modify a file I can simply do git commit , and that’ll auto complete the file’s name to the one that was modified. Howe

相关标签:
18条回答
  • 2020-12-22 16:40

    This get's git tab completion working on OSX without having to restart your terminal:

    curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash && echo "source ~/.git-completion.bash" >> ~/.bash_profile && source ~/.bash_profile
    
    0 讨论(0)
  • 2020-12-22 16:42

    If you have $BASH_VERSION < 4.1, eg 3.2.57(1)-release then go ahead with:

    brew install bash-completion
    # In ~/.bash_profile :
    if [ -f $(brew --prefix)/etc/bash_completion ]; then
        . $(brew --prefix)/etc/bash_completion
    fi
    

    However if you've brew install bash to get version 4.4.12(1)-release you can use the better and more complete completions in:

    brew install bash-completion@2
    # In ~/.bash_profile:
    [ -f "$(brew --prefix)/share/bash-completion/bash_completion" ] \
    && . "$(brew --prefix)/share/bash-completion/bash_completion"
    

    Note that some packages (brew, docker, tmux) will still put some completions into $(brew --prefix)/etc/bash_completion.d/ so you might add:

    for completion in "$(brew --prefix)/etc/bash_completion.d/"*
    do
        . $completion
    done
    

    Finally you should be able to add the git completion script if for some reason the way you installed git did not add it to either of those:

    [[ -f $(brew --prefix)/etc/bash_completion.d/git \
    || -f $(brew --prefix)/share/bash-completion/completions/git ]] \
    || curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash \
        -o $(brew --prefix)/etc/bash_completion.d/git
    

    You can get and add it with the above.

    0 讨论(0)
  • 2020-12-22 16:46

    For those who already have brew bash-completion installed. I did not have the git completion script installed and could not find any tap for that.

    So I added it manually:

    curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o $(brew --prefix)/etc/bash_completion.d/git

    Note that you have to rename the file and remove the extension for it to work.

    If you do not have completion or git installed, install it in the accepted answer.

    brew install git bash-completion

    0 讨论(0)
  • 2020-12-22 16:50

    I solved the problem by figuring out that $(brew --prefix)/etc/bash_completion returned Permission denied when executed. So after a simple:

    chmod +x $(brew --prefix)/etc/bash_completion
    

    Everything is now working fine. I'm wondering why Homebrew doesn't make the bash_completion file executable on installation, though.

    0 讨论(0)
  • 2020-12-22 16:50

    For bash on macOS Catalina, if you want to also use Bash 5 from homebrew, you need to make sure that your login shell is set to homebrew's bash, and not the default.

    To check if you need to do this, run echo ${BASH_VERSION}. If you see a version starting with 3, you are not using Brew's bash for your login shell.

    To change this,

    1. Open System Preferences->Users and Groups.
    2. Right click your user and select "Advanced Options". You may need to unlock this with your password by clicking the lock in the bottom left.
    3. Set the login shell field to the location of your brew's bash, which you can usually find by running which bash in a terminal after you install brew's bash. Mine was /usr/local/bin/bash.

    Restart your terminal, and follow the instructions in this excellent answer

    0 讨论(0)
  • 2020-12-22 16:50

    I know this is an old post, but you don't really need to install any additional packages.

    Homebrew is informing you that there is a directory with all the stuff you need.

    You can simply add the following line to your .bash_profile if you are using Bash:

    source /usr/local/etc/bash_completion.d/git-completion.bash
    
    0 讨论(0)
提交回复
热议问题