Skip to content

Instantly share code, notes, and snippets.

@ryanhs
Created April 11, 2023 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanhs/096c8e0bd8a9616bbcb7ba32ed787660 to your computer and use it in GitHub Desktop.
Save ryanhs/096c8e0bd8a9616bbcb7ba32ed787660 to your computer and use it in GitHub Desktop.
bashrc nvm
# --------------------------------------------------------------------------------------------------
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export EDITOR=nano
# node comes first!
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# --------------------------------------------------------------------------------------------------
export PS1="\[\033[01;34m\]\w\[\033[00m\]\$ "
# some clue in command line? :-)
# backup PS1 first, then check if virtualenv auto activation, and if there is .git repo
#PS1="\[\033[01;34m\]\w\[\033[00m\]\$ "
function parseNodeVersion {
node -v | cut -f1,2 -d. | cut -f2 -dv
}
export PS1BACKUP="$PS1"
CURRENT_NODE_VERSION=`parseNodeVersion`
function ps1customizer {
PS1EXTEND="";
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1) /'
}
PS1EXTEND=""
# .nvmrc
if [[ -f .nvmrc && -r .nvmrc ]]; then
NVMRC_VERSION=$(cat .nvmrc)
if nvm version | grep -q $NVMRC_VERSION; then
echo ".nvmrc still in same version: $NVMRC_VERSION" > /dev/null
CURRENT_NODE_VERSION=`parseNodeVersion`
else
nvm use
CURRENT_NODE_VERSION=`parseNodeVersion`
fi
fi
PS1EXTEND="(node $CURRENT_NODE_VERSION)$PS1EXTEND"
# show git branch
if parse_git_branch | grep -q "("; then
export PS1EXTEND="($(parse_git_branch | cut -d "(" -f2)$PS1EXTEND"
fi
export PS1="$PS1EXTEND $PS1BACKUP"
}
function cd {
builtin cd "$@"
ps1customizer
}
# --------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment