Month: July 2018

Show git branch name in bash command prompt

I work with git command in terminal a lot and I often forget what branch I was working on. Running command git branch often is not very convenient I needed to put git branch in command prompt when I was inside git repository.

I wanted my console to look like this:

eanlr@vps some-repository (master) $ 

To get that you need to add following lines to your .bash_profile file in your home directory:

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^*/d' -e 's/* (.*)/ (\1)/'
}

export PS1="\u@\h \W[\033[32m]$(parse_git_branch)[\033[00m] $ "

You can of course customize command prompt by changing PS1 variable.

Slow shell startup with Node Version Manager (NVM)

While I was working with some frontend projects I used node and npm tools. The problem is that each projects requires different version of those tools so you need something to manage various versions and switch on demand. Node version manager (nvm) is a great tool for that purpose and I was very satisfied with it.

Unfortunatelly after installing nvm my shell startup became quite slow and it was irritating when I opened new terminal tab. It turned out nvm was responsible for that. As i use frontend tools not very often I decided to load nvm on demand.

I changed a fragment responsible for nvm in my .bash_profile file from this:

. "/usr/local/opt/nvm/nvm.sh"

To that:

load_nvm() {
  . "/usr/local/opt/nvm/nvm.sh"
}

Now when I need node and npm tools i simply run load_nvm and my shell is ready for javascript development.

eanlr@vps ~ $ load_nvm
eanlr@vps ~ $ node -v
v8.9.4