Skip to content

Instantly share code, notes, and snippets.

@calendee
Last active May 14, 2021 23:29
Show Gist options
  • Save calendee/bb55962fd720a6dbeed6bf356976656c to your computer and use it in GitHub Desktop.
Save calendee/bb55962fd720a6dbeed6bf356976656c to your computer and use it in GitHub Desktop.
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