Instantly share code, notes, and snippets.

Embed
What would you like to do?
NVM Automatic Switching
# NVM version shortcut
# change title name of tab in terminal
function title {
    echo -ne "\033]0;"$*"\007"
}
cd() {
  builtin cd "$@" || return
  #echo $PREV_PWD
  if [ "$PWD" != "$PREV_PWD" ]; then
    PREV_PWD="$PWD";
    title $(echo ${PWD##*/}) $(node -v);
    if [ -e ".nvmrc" ]; then
      nvm use;
      # set tab terminal name to be cwd and node version
      title $(echo ${PWD##*/}) $(node -v);
    else
      nvm use default;
    fi
  fi
}
# Instructions
- Add the contents of `.bash_profile` to your `~/.bash_profile`.
- In your project directory, `echo X.X.X > .nvmrc` like as `echo 9.4.0 > .nvmrc`
- Now, any time you `cd` into this directory you'll see:
```sh
> cd angular-pro-app-seed
Found '/Users/jn/Documents/Apps/my-awesome-project/.nvmrc' with version <9.4.0>
Now using node v9.4.0 (npm v5.6.0)
```
# Credits
Blake Johnston for the `.bash_profile` script
More details and usage for zsh : https://medium.com/@kinduff/automatic-version-switch-for-nvm-ff9e00ae67f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment