DEV Community

Jameson
Jameson

Posted on

Bash Completion for Git on Mac OS X Monterey

For years now, I've had a working recipe to enable bash autocompletion for git on my Mac. Recently that broke due to a packaging change with Xcode's CommandLineTools. So, let's take a moment to review how this all works, and how we can get it working (again).

Quick background on bashrc files

bashrc files are environment configuration scripts that Bash includes when it starts. You can use them to customize your CLI experience.

However, the single ~/.bashrc file can get unmanageable quickly. So just like any other piece of software, we'd like to template things and break the problem into smaller chunks.

My dotfiles GitHub repo includes the collection of .bashrc template files I use on a day-to-day basis:

GitHub logo jamesonwilliams / dotfiles

Jameson's . (dot) files for Linux

dotfiles

Jameson's . (dot) files for UNIX and Linux family command-line environments.

One goal of these dot files is to converge on a similar experience across some different UNIX/Linux systems that we encounter in the wild:

  • Ubuntu
  • Debian
  • RHEL
  • Amazon Linux
  • Mac OS X
  • etc.

Setup

To install the dot files for a particular user on your system, do:

git clone https://github.com/jamesonwilliams/dotfiles.git
./dotfiles/install.sh
Enter fullscreen mode Exit fullscreen mode



Basically, they work as follows.

  1. ~/.bash_profile is the hook used by Mac OS X to start loading your Bash customizations. Mine does nothing other than call ~/.bashrc.
  2. My ~/.bashrc itself contains no real logic, it just sources a bunch of template files that handle specific sub-problems.
  3. One of these template files is called ~/.bashrc.darwin, and it includes some fixes for Mac-specific quirks. Stuff that's generally useful on Linux or other UNIX flavors doesn't go into this file.

How it used to work on Mac OS X

One of the quirks of using OS X is that tab completion with git doesn't work out of the box. So for years, I've had a little bit of configuration to handle this case:

# In ~/.bashrc.darwin

cli_tools='/Library/Developer/CommandLineTools'
git_core="$cli_tools/usr/share/git-core"
git_completion="$git_core/git-completion.bash"
[ -x "$(which git)" ] && \
    [ -f "$git_completion" ] && \
    source "$git_completion"
Enter fullscreen mode Exit fullscreen mode

If git is installed and the git completion file can be found, we source it into the environment.

How it works now

Recently, that git-completion.bash file disappeared from the CommandLineTools install, and so my tab completion stopped working.

Turns out it now lives here:

/Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
Enter fullscreen mode Exit fullscreen mode

So all that's needed is to update our pathing for git_core in ~/.bashrc.darwin:

# In ~/.bashrc.darwin

xcode_dev_dir='/Applications/Xcode.app/Contents/Developer'
git_core="$xcode_dev_dir/usr/share/git-core"
git_completion="$git_core/git-completion.bash"
[ -x "$(which git)" ] && \
    [ -f "$git_completion" ] && \
    source "$git_completion"
Enter fullscreen mode Exit fullscreen mode

Save the file, reboot your laptop, and profit. Happy git-ing.

Discussion (0)