## Monday, September 29, 2014

### Bash prompt with colors, and git branch

So, you're working on several machines at the same time, travelling from directories to directories, and using git with multiple branches. And, like me, you can't remember where you are, which git branch you're in, because you have half a dozen or more terminals open on your screen.

Here is a little trick you can do with bash, change the prompt.
First of all, you have to download this nice little script called git-prompt.sh and save it in your home directory as .git-prompt.sh

source ~/.git-prompt.sh

wherever you want. It can be near the beginning for example. If you don't have .bashrc, search for .bash_profile instead.

Then search for the definition of the variable PS1 . And replace its line by the following:

export PS1='\h:$\e[33m$\W$\e[0m$$(__git_ps1 " (%s)")\$ '


If you can't find it, just add the line wherever you want but in any case, after the source command from above.

The variable PS1 defines your bash prompt as follow:
• \h : print the hostname you are in
• $and$ encloses ANSI escape commands. Here I use ANSI commands to change the color
• \e is the ESCape character
• [33m is for yellow
• [0m is for "back to normal color"
• \W is the basename of your working directory. Example /home/foobar/mydir will be written as mydir only
• $(__git_ps1 " (%s)"): calls the __git_ps1 function which determines your GIT branch or nothing if you're not in a GIT-managed directory • \$ transforms into \$ if you're a normal user or # if you're root