Skip to content

Commit 7aee54b

Browse files
benwiley4000ljharb
authored andcommitted
[readme] reorganize “deeper shell integration” instructions
1 parent 6262b5a commit 7aee54b

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

README.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@
4141
- [Use a mirror of node binaries](#use-a-mirror-of-node-binaries)
4242
- [.nvmrc](#nvmrc)
4343
- [Deeper Shell Integration](#deeper-shell-integration)
44-
- [bash](#bash)
45-
- [Automatically call `nvm use`](#automatically-call-nvm-use)
46-
- [zsh](#zsh)
47-
- [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file)
48-
- [fish](#fish)
49-
- [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file-1)
44+
- [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file)
45+
- [bash](#bash)
46+
- [zsh](#zsh)
47+
- [fish](#fish)
5048
- [Running Tests](#running-tests)
5149
- [Environment variables](#environment-variables)
5250
- [Bash Completion](#bash-completion)
@@ -575,52 +573,53 @@ You can also use [`nvshim`](https://github.com/iamogbz/nvshim) to shim the `node
575573

576574
If you prefer a lighter-weight solution, the recipes below have been contributed by `nvm` users. They are **not** supported by the `nvm` maintainers. We are, however, accepting pull requests for more examples.
577575

578-
#### bash
576+
#### Calling `nvm use` automatically in a directory with a `.nvmrc` file
579577

580-
##### Automatically call `nvm use`
578+
In your profile (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`), add the following to `nvm use` whenever you enter a new directory:
579+
580+
##### bash
581581

582582
Put the following at the end of your `$HOME/.bashrc`:
583583

584584
```bash
585585
cdnvm() {
586586
command cd "$@" || return $?
587-
nvm_path=$(nvm_find_up .nvmrc | tr -d '\n')
587+
nvm_path="$(nvm_find_up .nvmrc | command tr -d '\n')"
588588

589589
# If there are no .nvmrc file, use the default nvm version
590590
if [[ ! $nvm_path = *[^[:space:]]* ]]; then
591591

592-
declare default_version;
593-
default_version=$(nvm version default);
592+
declare default_version
593+
default_version="$(nvm version default)"
594594

595595
# If there is no default version, set it to `node`
596596
# This will use the latest version on your machine
597-
if [[ $default_version == "N/A" ]]; then
598-
nvm alias default node;
599-
default_version=$(nvm version default);
597+
if [ $default_version = 'N/A' ]; then
598+
nvm alias default node
599+
default_version=$(nvm version default)
600600
fi
601601

602602
# If the current version is not the default version, set it to use the default version
603-
if [[ $(nvm current) != "$default_version" ]]; then
604-
nvm use default;
603+
if [ "$(nvm current)" != "${default_version}" ]; then
604+
nvm use default
605605
fi
606-
607-
elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
606+
elif [[ -s "${nvm_path}/.nvmrc" && -r "${nvm_path}/.nvmrc" ]]; then
608607
declare nvm_version
609-
nvm_version=$(<"$nvm_path"/.nvmrc)
608+
nvm_version=$(<"${nvm_path}"/.nvmrc)
610609

611610
declare locally_resolved_nvm_version
612611
# `nvm ls` will check all locally-available versions
613612
# If there are multiple matching versions, take the latest one
614613
# Remove the `->` and `*` characters and spaces
615614
# `locally_resolved_nvm_version` will be `N/A` if no local versions are found
616-
locally_resolved_nvm_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr -d '[:space:]')
615+
locally_resolved_nvm_version=$(nvm ls --no-colors "${nvm_version}" | command tail -1 | command tr -d '\->*' | command tr -d '[:space:]')
617616

618617
# If it is not already installed, install it
619618
# `nvm install` will implicitly use the newly-installed version
620-
if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
621-
nvm install "$nvm_version";
622-
elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
623-
nvm use "$nvm_version";
619+
if [ "${locally_resolved_nvm_version}" = 'N/A' ]; then
620+
nvm install "${nvm_version}";
621+
elif [ "$(nvm current)" != "${locally_resolved_nvm_version}" ]; then
622+
nvm use "${nvm_version}";
624623
fi
625624
fi
626625
}
@@ -631,9 +630,9 @@ cdnvm "$PWD" || exit
631630
632631
This alias would search 'up' from your current directory in order to detect a `.nvmrc` file. If it finds it, it will switch to that version; if not, it will use the default version.
633632
634-
#### zsh
633+
##### zsh
635634
636-
##### Calling `nvm use` automatically in a directory with a `.nvmrc` file
635+
This shell function will install (if needed) and `nvm use` the specified Node version when an `.nvmrc` is found, and `nvm use default` otherwise.
637636
638637
Put this into your `$HOME/.zshrc` to call `nvm use` automatically whenever you enter a directory that contains an
639638
`.nvmrc` file with a string telling nvm which node to `use`:
@@ -665,9 +664,8 @@ add-zsh-hook chpwd load-nvmrc
665664
load-nvmrc
666665
```
667666
668-
#### fish
667+
##### fish
669668
670-
##### Calling `nvm use` automatically in a directory with a `.nvmrc` file
671669
This requires that you have [bass](https://github.com/edc/bass) installed.
672670
```fish
673671
# ~/.config/fish/functions/nvm.fish

0 commit comments

Comments
 (0)