[SOLVED] Nvm bash completion not working w/o manual sourcing?


brew auto-installs bash completions for many packages in /usr/local/etc/bash_completion.d.

I’m using bash-completion@2 to load them all (by sourcing that /usr/local/share/bash-completion/bash_completion) without having to source them individually…not entirely sure what bash-completion is for beyond that?

Everything works great except for nvm. That doesn’t work unless I manually source the brew installed bash completions? What am I missing?


(Sean Molenaar) #2

This looks like a question about software that brew packages. You’d be better off asking that question upstream: https://github.com/scop/bash-completion


Tried that without success: https://github.com/scop/bash-completion/issues/220

Should I reach out to nvm?

(Sean Molenaar) #4

Maybe check if nvm completion is actually shipped by brew? If it’s not, see if it’s available for nvm.


If your run brew info nvm you’ll see that it’s shipped by brew. It works, I just need to source it manually.

(Dave Finlay) #6

This peaked my interest, as my nvm completion works out of the gate for me with a new login shell.

If you take a look at the nvm completion file, the first evaluated statement reads:

if ! command -v nvm &> /dev/null; then

This is checking if nvm can be invoked, and if it can’t it bails. This explains why you have to manually source it, as it isn’t invokable until later in your profile’s evaluation. Which was strange, until I tried a which nvm that yielded nothing, and then a type nvm, which spit out a huge function definition. Eureka, nvm is just a bash function. So if you move the source command displayed via brew info nvm above your sourcing of bash_completion, you’ll be in business. In general it’s advisable to activate bash-completion at the end of your profile, after paths are set, spells are cast, etc (especially if you have legacy completions that get eager loaded, which is how homebrew installs formula provided ones for compatibility).

# Example .bash_profile statement ordering.
# Make sure these statements execute before sourcing bash completion.
export NVM_DIR="${HOME}/.nvm"
if [[ -f /usr/local/opt/nvm/nvm.sh ]]; then
   .  /usr/local/opt/nvm/nvm.sh
# Then you can bring in completions.
if [[ -f /usr/local/share/bash-completion/bash_completion ]]; then
    . /usr/local/share/bash-completion/bash_completion


@dfinlay you’re awesome! Thank you so much for digging into this. Makes sense and works now!

(Dave Finlay) #8

No problem, happy to help. I’ve had to dig deep into the bowels of Bash’s Programmable Completion before, and couldn’t bear the thought of another visitor to Mordor. :slight_smile: