Created
April 20, 2019 21:45
-
-
Save iansu/e0a12f811a55f6c8adbc47050e050bcc to your computer and use it in GitHub Desktop.
Automatically run nvm when entering a directory with a .nvmrc file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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 |
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
What is the NVM_DIRTY for?