[Docs] Add section on automatic `nvm use` in Bash

Daniel Li 2018-07-15 19:08:15 +01:00 committed by Jordan Harband
parent 217a5bb0de
commit 355c4c7915
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
1 changed files with 34 additions and 0 deletions

View File

@ -20,6 +20,8 @@
- [Listing versions](#listing-versions)
- [.nvmrc](#nvmrc)
- [Deeper Shell Integration](#deeper-shell-integration)
- [bash](#bash)
- [Automatically call `nvm use`](#automatically-call-nvm-use)
- [zsh](#zsh)
- [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file)
- [License](#license)
@ -377,6 +379,38 @@ 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
##### Automatically call `nvm use`
Put the following at the end of your `$HOME/.bashrc`:
```bash
cdnvm(){
cd $@
if [[ -f .nvmrc && -s .nvmrc && -r .nvmrc ]]; then
<.nvmrc nvm install;
elif [[ $(nvm current) != $(nvm version default) ]]; then
nvm use default;
fi
}
alias cd='cdnvm'
```
This alias would automatically detect a `.nvmrc` file in the directory you're `cd`ing into, and switch to that version. And when you `cd` back, it will automatically switch back to the `default` version.
If you prefer one-liners:
```
alias cd='cdnvm(){ cd $@; if [[ -f .nvmrc && -s .nvmrc && -r .nvmrc ]]; then <.nvmrc nvm install; elif [[ $(nvm current) != $(nvm version default) ]]; then nvm use default; fi; };cdnvm'
```
You may also run this directly on your terminal:
```bash
$ echo 'alias cd='\''cdnvm(){ cd $@; if [[ -f .nvmrc && -s .nvmrc && -r .nvmrc ]]; then <.nvmrc nvm install; elif [[ $(nvm current) != $(nvm version default) ]]; then nvm use default; fi; };cdnvm'\''' >> ~/.bashrc
```
#### zsh
##### Calling `nvm use` automatically in a directory with a `.nvmrc` file