For anyone coming to the conclusion that startup times for new shells on MacOS are too slow for them, this is the solution.
I just discovered that there are in fact two packages that can be installed via brew
. I've been installing the bash-completion
package for years, and never bothered to question it, even though in that time I've gone from Bash 3, to 4, to now 5. From time to time, though, I'd revisit the problem, often stumbling upon this very StackOverflow discussion.
There's another package, bash-completion@2
!
What's the difference? bash-completion
is for Bash version 3.2. bash-completion@2
is for Bash version 4.1+, and 5.
In removing the old bash-completion
package, and installing bash-completion@2
, my shell startup times have dropped from 605ms to 244ms. That's a huge speed improvement.
I suspect many of us are making this same mistake, as the brew info
stats show that the former has tonnes of installs, while the latter has so few:
It should be noted that the current chosen answer mentions commenting out some lines, which provides only a slight improvement to startup times (if using the old bash-completion
package, which many probably are), but has no impact whatsoever on the new bash-completion@2
package: this new package is fast no matter what. That means no hacks required.
TL;DR:
brew uninstall bash-completion && brew install bash-completion@2
Remember to update the source path to the completion file in your .bashrc
or .bash_profile
file.
Sources:
As a topic somewhat related, I use the rclone
utility a lot, so it's installed. It also happens to have the largest completion file I've ever seen. Removing it reduces my shell startup times to ~120ms, which is very fast.
Edit:
For anyone who wants the technical details that explain this problem, I've written about it at length on the Homebrew forums. To summarize, the reason that bash-completion@2
is so much faster is because it was written so that it no longer eagerly loads all the completion files; instead it loads a completion file on-demand, or as the author describes it, it loads them in a non-eager manner.