Skip to content

Instantly share code, notes, and snippets.

@tombl
Forked from eugenet8k/Readme.md
Last active June 10, 2020 18:53
Show Gist options
  • Save tombl/86b2dd8f4802ff7abe6a14d4f2b514c3 to your computer and use it in GitHub Desktop.
Save tombl/86b2dd8f4802ff7abe6a14d4f2b514c3 to your computer and use it in GitHub Desktop.
Getting `nvm` support in the `fish` shell

Getting nvm support in the fish shell

How to get nvm to run inside the fish shell.

Step 0: Ensure fish and nvm are installed

See the following for instructions:

Step 1: Install fisher

fisher is a package manager for fish. It can be installed using the following command:

curl https://git.io/fisher --create-dirs -sLo ~/.config/fish/functions/fisher.fish

Step 2: Install bass

bass is a way to run bash scripts on fish. It can be installed using the following command:

fisher add edc/bass

Step 3: Setup nvm with bass

Create a file at ~/.config/fish/functions/nvm.fish containing:

function nvm
    bass source ~/.nvm/nvm.sh --no-use ';' nvm $argv
end

replacing ~/.nvm/nvm.sh with your installation directory if installed in a non-standard location or by a package manager. nvm is now working inside the fish shell.

Step 4 (optional but recommended): Calling nvm use automatically in a directory with a .nvmrc file

nvm provides scripts to allow nvm to have deeper integration into bash or zsh. This is a port of the original zsh function by nvm to fish. Put this into your ~/.config/fish/config.fish to call nvm use automatically whenever you enter a directory that has contains, or has a parent directory that contains, an .nvmrc file with a string telling nvm which version of node to use:

function find_up
    set path (pwd)
    while test $path != "/"
        and not test -e "$path/$argv[1]"
        set path (dirname $path)
    end
    if test -e "$path/$argv[1]"
        echo $path
    else
        echo ""
    end
end

function __check_nvm --on-variable PWD
    if status --is-command-substitution
        return
    end

    set nvm_path (find_up ".nvmrc" | tr -d '[:space:]')

    if test "$nvm_path" != ""

        set nvmrc_node_version (nvm version (cat "$nvm_path/.nvmrc") '; 2>1')

        if test "$nvmrc_node_version" = "N/A"
            echo "Installing node version "(cat "$nvm_path/.nvmrc")
            nvm install
            set nvm_node_version (nvm version)
        else if test "$nvmrc_node_version" != (nvm version)
            echo "Changing node version to $nvmrc_node_version"
            nvm use
            set nvm_node_version (nvm version)
        end
        echo "$nvm_path/.nvmrc"
    else if test (nvm version) = "none"
        nvm use default --silent
    else if test (nvm version) != (nvm version default)
        echo "Reverting node version to default"
        nvm use default
    end
end

__check_nvm
@mlg87
Copy link

mlg87 commented Apr 17, 2019

thanks for this. works great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment