Skip to content

Instantly share code, notes, and snippets.

@iansu
Created April 20, 2019 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iansu/e0a12f811a55f6c8adbc47050e050bcc to your computer and use it in GitHub Desktop.
Save iansu/e0a12f811a55f6c8adbc47050e050bcc to your computer and use it in GitHub Desktop.
Automatically run nvm when entering a directory with a .nvmrc file
_enter_dir() {
local git_root
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ "$git_root" == "$PREV_PWD" ]]; then
return
elif [[ -n "$git_root" && -f "$git_root/.nvmrc" ]]; then
nvm use
NVM_DIRTY=1
elif [[ "$NVM_DIRTY" == 1 ]]; then
nvm use default
NVM_DIRTY=0
fi
PREV_PWD="$git_root"
}
export PROMPT_COMMAND=_enter_dir
@trajano
Copy link

trajano commented Feb 24, 2023

What is the NVM_DIRTY for?

@trajano
Copy link

trajano commented Feb 24, 2023

you can save a second by putting in --silent if you don't need to see the active version (e.g. because Starship already shows it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment