Skip to content

Instantly share code, notes, and snippets.

@jusopi
Last active November 14, 2017 09:57
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jusopi/fa6d03321991620778a2 to your computer and use it in GitHub Desktop.
Save jusopi/fa6d03321991620778a2 to your computer and use it in GitHub Desktop.
Set node version per project using .nvmrc file
#!/bin/sh
enter_directory(){
if [ "$PWD" != "$PREV_PWD" ]; then
PREV_PWD="$PWD";
if [ -e ".nvmrc" ]; then
nvm use;
fi
fi
}
export PROMPT_COMMAND="$PROMPT_COMMAND enter_directory;"
@mfields-gpsw
Copy link

Thanks for this! I did have to modify this a bit for it to work properly for me (on Mac OSX). Added a double quote after $PWD and put spaces inside the if conditions.

enter_directory(){
        if [ "$PWD" != "$PREV_PWD" ]; then
                PREV_PWD="$PWD";
                if [ -e ".nvmrc" ]; then
                        nvm use;
                fi
        fi
}

@jusopi
Copy link
Author

jusopi commented Feb 17, 2016

@mfields-gpsw ah good catch on the missing double-quote. I will update that. Thank you.

@jusopi
Copy link
Author

jusopi commented Feb 22, 2016

updated per comment suggestion nvm-sh/nvm#110 (comment)

@codeaholics
Copy link

Typo on line 11?

@andywer
Copy link

andywer commented Mar 21, 2016

+1
Should be $PROMPT_COMMAND instead of $PROMP_COMMAND

@augbog
Copy link

augbog commented Apr 18, 2016

Thanks for this! One thing I added to my bash was after I started using this I would lose track which tabs in iTerm were what node version (I work on a bunch of projects that use different node version sadly...) It helped to just change the tab name to say the node version as well. Hope someone finds it useful.

# change title name of tab in terminal
function title {
    echo -ne "\033]0;"$*"\007"
}

enter_directory(){
  if [ "$PWD" != "$PREV_PWD" ]; then
    PREV_PWD="$PWD";
    title $(echo ${PWD##*/}) $(node -v);
    if [ -e ".nvmrc" ]; then
      nvm use;
      # set tab terminal name to be cwd and node version
      title $(echo ${PWD##*/}) $(node -v);
    fi
  fi
}

@jusopi
Copy link
Author

jusopi commented Aug 12, 2016

Fixed the misspelled $PROMP_COMMAND typo

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