From f81f5cd142fa8e9e86ab055b1edc130b8fa1deb1 Mon Sep 17 00:00:00 2001 From: Derick Bailey Date: Tue, 31 May 2016 09:04:59 -0500 Subject: [PATCH] example to automatically run 'nvm use' in Bash I added an example to automatically `nvm use` when changing directories in a Bash shell, similar to the one for Zsh. The function is the same as the Zsh example, but the method of implementing overrides the `cd` command in bash, and then calls the built-in `cd` before calling the `load-nvmrc` function. I've been using this on OSX for a bit now, and it seems to be working well in my basic testing. --- README.markdown | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.markdown b/README.markdown index 058f57b..ba18522 100644 --- a/README.markdown +++ b/README.markdown @@ -192,6 +192,23 @@ You can use [`avn`](https://github.com/wbyoung/avn) to deeply integrate into you If you prefer a lighter-weight solution, the recipes below have been contributed by `nvm` users. They are **not** supported by the `nvm` development team. We are, however, accepting pull requests for more examples. +#### Bash + +Put this into your `$HOME/.bash_profile` or `$HOME/.bashrc` to call `nvm use` automatically whenever you enter a directory that contains an `.nvmrc` file with a string telling nvm which node to `use`: + +``` +load-nvmrc() { + if [[ -f .nvmrc && -r .nvmrc ]]; then + nvm use + elif [[ $(nvm version) != $(nvm version default) ]]; then + echo "Reverting to nvm default version" + nvm use default + fi +} + +function cd () { builtin cd "$@" && load-nvmrc; } +``` + #### Zsh ##### Calling `nvm use` automatically in a directory with a `.nvmrc` file