useNvmIfPresent() {
[[ $PWD == $PREV_PWD ]] && return
PREV_PWD=$PWD
[[ -f ".nvmrc" ]] && nvm use
}
PROMPT_COMMAND=${PROMPT_COMMAND:+"$PROMPT_COMMAND; "}'useNvmIfPresent'
This script runs nvm use
anytime you cd
to a directory with a .nvmrc
file.
With this, you don’t need to remember to run nvm use
when switching between directories.
It leverages the built-in PROMPT_COMMAND
variable in bash, to run the useNvmIfPresent
function before each PS1
prompt.
To use it, just throw the script above into your .bashrc
, .bash_profile
, or whatever system you’re using to automatically source scripts on shell startup.
Thanks for reading! If you enjoyed this, you may also enjoy following me on twitter!