Skip to content

Instantly share code, notes, and snippets.

@foobear

foobear/.bashrc Secret

Last active January 5, 2021 17:38
Show Gist options
  • Save foobear/d77d246d99484f56b61da077b5554216 to your computer and use it in GitHub Desktop.
Save foobear/d77d246d99484f56b61da077b5554216 to your computer and use it in GitHub Desktop.
Bash: Automatically "nvm use" when .nvmrc is detected in current or ancestor directories. Put this at the end of your ~/.bashrc
find-up () {
path=$(pwd)
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
automatic-nvm-use() {
NVM_PATH=$(find-up .nvmrc | tr -d '[:space:]')
if [[ $NVM_PATH == $NVM_PATH_WAS ]]; then
return
fi
NVM_PATH_WAS=$NVM_PATH
if [[ -f "$NVM_PATH/.nvmrc" ]]; then
nvm use $(<"$NVM_PATH/.nvmrc")
else
nvm use default
fi
}
if [[ "$PROMPT_COMMAND" ]]; then
export PROMPT_COMMAND=$PROMPT_COMMAND;automatic-nvm-use
else
export PROMPT_COMMAND=automatic-nvm-use
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment