From 1e2643af97c7a49f1c8fc476d689fe34ce130c1c Mon Sep 17 00:00:00 2001 From: Ben Wiley Date: Thu, 29 Jun 2017 14:23:03 -0400 Subject: [PATCH 1/2] Alternative plain bash instructions for zsh hook --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 926c254..899b97b 100644 --- a/README.md +++ b/README.md @@ -341,9 +341,9 @@ 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. -#### zsh +#### Calling `nvm use` automatically in a directory with a `.nvmrc` file -##### Calling `nvm use` automatically in a directory with a `.nvmrc` file +##### zsh Put this into your `$HOME/.zshrc` to call `nvm use` automatically whenever you enter a directory that contains an `.nvmrc` file with a string telling nvm which node to `use`: @@ -372,6 +372,23 @@ add-zsh-hook chpwd load-nvmrc load-nvmrc ``` +##### bash + +Put this into `$HOME/.bashrc` (or `$HOME/.bash_profile`, or whatever you use) to override the `cd` command to mimic the above zsh-hook: + +```bash +# place this after nvm initialization! +load-nvmrc() { + # same definition as zsh-hook +} +load-nvmrc + +cd() { + builtin cd "$1" + load-nvmrc +} +``` + ## License nvm is released under the MIT license. From 202d8a50a4511d2b8afb689638855a0be904db88 Mon Sep 17 00:00:00 2001 From: Ben Wiley Date: Thu, 29 Jun 2017 20:57:39 -0400 Subject: [PATCH 2/2] Shell hook instructions made more organized/specific. --- README.md | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 899b97b..9971798 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,9 @@ - [Listing versions](#listing-versions) - [.nvmrc](#nvmrc) - [Deeper Shell Integration](#deeper-shell-integration) - - [zsh](#zsh) - - [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file) + - [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file) + - [zsh](#zsh) + - [other shells (e.g. bash)](#other-shells-eg-bash) - [License](#license) - [Running tests](#running-tests) - [Bash completion](#bash-completion) @@ -343,14 +344,9 @@ If you prefer a lighter-weight solution, the recipes below have been contributed #### Calling `nvm use` automatically in a directory with a `.nvmrc` file -##### zsh +This shell function will install (if needed) and `nvm use` the specified Node version when an `.nvmrc` is found, and `nvm use default` otherwise. -Put this into your `$HOME/.zshrc` to call `nvm use` automatically whenever you enter a directory that contains an -`.nvmrc` file with a string telling nvm which node to `use`: - -```zsh -# place this after nvm initialization! -autoload -U add-zsh-hook +```sh load-nvmrc() { local node_version="$(nvm version)" local nvmrc_path="$(nvm_find_nvmrc)" @@ -368,25 +364,27 @@ load-nvmrc() { nvm use default fi } +``` + +In your profile (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`), add `load-nvmrc` along with one of the following hook listeners to call `load-nvmrc` whenever you enter a new directory: + +##### zsh + +```zsh +autoload -U add-zsh-hook add-zsh-hook chpwd load-nvmrc load-nvmrc ``` -##### bash - -Put this into `$HOME/.bashrc` (or `$HOME/.bash_profile`, or whatever you use) to override the `cd` command to mimic the above zsh-hook: - -```bash -# place this after nvm initialization! -load-nvmrc() { - # same definition as zsh-hook -} -load-nvmrc +##### other shells (e.g. bash) +```sh +# note: overrides the `cd` command cd() { builtin cd "$1" load-nvmrc } +load-nvmrc ``` ## License