Integrating Homebrew bash completion on Linux with system completions

I like to use Homebrew on my Linux development machine as well, instead of random apt packages which may or may not be up to date for common tools.

One annoyance I found a solution for, is getting the bash completion for Homebrew commands to work on Linux. The problem is that Ubuntu (and other Linux distributions) have their own bash completion scripts for system commands. But the way most bash completion scripts work is that they have a check to see if completions are already loaded.

So if I have a line in my bashrc to load homebrew completions, it won’t load, because it detects the completions from the system already loaded. Specifically, it looks for the environment variable BASH_COMPLETION_VERSINFO which is set by any bash completion script that is already loaded.

Solution

The only satisfactory solution I found is to disable the bash completion for system commands.

mv /etc/profile.d/bash_completion.sh /etc/profile.d/bash_completion.sh.disabled
Bash

My only concern was that I might lose completions for system commands. However, it seems the Homebrew completion loader also checks for system completion scripts.

You can verify that it works here:

$ # A homebrew command

$ which helm
/home/linuxbrew/.linuxbrew/bin/helm

$ complete -p helm
complete -o default -F __start_helm helm

$ # A system command

$ which apt
/usr/bin/apt

$ complete -p apt
complete -F _apt apt
Shell session
Completions are lazy loaded. complete -p for a command will only show the completion function if you have exercised the completion for the command once using COMMAND<TAB><TAB>.
tech
System Design - Tap Compare Ubuntu Wi-Fi 5GHz Issue on Raspberry Pi 5