How I added color and git to my Ubuntu/Linux terminal

Zain Butt
3 min readDec 13, 2018

--

I have been using Ubuntu for about 3 years now and for all my development purposes use its Terminal. Working with different projects sometimes confuse me which branch I am using and I have to type in the command to check. So I wanted a consistent reminder for that, so I decided why not customizing my Terminal to show this information.

Yeah I did consider Oh-my-Zsh and other options but for just this little thing I believe its an overkill.

So to begin customizing your Terminal, open .bashrc file in your favorite editor or just the terminal itself located in your home directory, scroll down where you will find this line:

if [ "$color_prompt" = yes ]; then

Before this line just add this function:

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

Its a function that would check if current directory has a git branch and display it. But with this we only get the branch but right now it does not show it on the Terminal. So before the end of the file before the export statements add this line:

PS1="\[\033[01;42m\]\w \[\033[0;46m\]\$(parse_git_branch)\$\[\033[0;37m\] "

This is the Shell Prompt variable, this decides what and how to show things on terminal, by that i mean information like your username and computer name in the Terminal:

Just an image via google search

By now you would be confused with all the weird numbers in the PS1 variable, these just determine which color to use for different parts of the prompt.

\[\033[01;42m\] 01 tells whether you want a Regular or Bold font color, 42m tells whether is font color or background color.

00 means regular, non-bold color, 01 means bold color.

If you want to change font color and not the background color, the next number should be in 30’s range like 31m, 32m. If you want to change the background let it be 42m. I will provide the available combinations for the colors so can mix and match according to your taste.

The PS1 variable above is my configuration that does not show user or computer name, It just shows working directory and git branch with background color, go ahead try it and make sure to save your changes.

It should be like this, working directory with the current git branch.

These are the color codes that you can try:

-> Black 0;30
-> Dark Gray 1;30
-> Blue 0;34
-> Bold Blue 1;34
-> Green 0;32
-> Bold Green 1;32
-> Cyan 0;36
-> Bold Cyan 1;36
-> Red 0;31
-> Bold Red 1;31
-> Purple 0;35
-> Bold Purple 1;35
-> Brown 0;33
-> Yellow 1;33
-> Bold Gray 0;37
-> White 1;37

--

--