Colorise your ubuntu prompt

This is found here

If you have difficulties using the terminal because the prompt isn’t visible enough or you would simply like it to look nicer, this howto is for you. It should work on any distribution if you use bash shell. I’ve attached a before and after picture of my terminal as an example of what you could do.
So let’s get started. Fire up your terminal and stay in your home directory.

1. Backup

First, let’s backup our .bashrc file since we’re about to make some changes to it:

cp .bashrc .bashrc-backup

The .bashrc file tells bash what to do when terminal is started, including how to show prompt.

2. Preparing the .bashrc

Use your favorite text editor to edit the original .bashrc file, eg.:

gedit .bashrc

You should now comment in those lines in your .bashrc that format your prompt (you should put # in the beginning of all those lines) because you don’t want to use the old prompt any more.

If you’re using Ubuntu’s default .bashrc file, those are the lines between set a fancy prompt (non-color, unless we know we "want" color) and Comment in the above and uncomment this below for a color prompt.

After that, paste this code somewhere where you want it (preferably below the text you just commented in or at the end of file):

# ANSI color codes
RS="\[\033[0m\]"    # reset
HC="\[\033[1m\]"    # hicolor
UL="\[\033[4m\]"    # underline
INV="\[\033[7m\]"   # inverse background and foreground
FBLK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
FGRN="\[\033[32m\]" # foreground green
FYEL="\[\033[33m\]" # foreground yellow
FBLE="\[\033[34m\]" # foreground blue
FMAG="\[\033[35m\]" # foreground magenta
FCYN="\[\033[36m\]" # foreground cyan
FWHT="\[\033[37m\]" # foreground white
BBLK="\[\033[40m\]" # background black
BRED="\[\033[41m\]" # background red
BGRN="\[\033[42m\]" # background green
BYEL="\[\033[43m\]" # background yellow
BBLE="\[\033[44m\]" # background blue
BMAG="\[\033[45m\]" # background magenta
BCYN="\[\033[46m\]" # background cyan
BWHT="\[\033[47m\]" # background white

This is so you don’t have to write all that ANSI code over and over again. Now you can just use $FBLE to set foreground (text) to blue, instead of \[\033[34m\].

3. Setting the prompt

Now that you have everything prepared, you can design your own prompt. Bash reads PS1 variable to see how to show primary prompt and PS2 for a secondary prompt (used when writing multi-line commands). Write these below the code you just pasted. I’ll explain now how I did mine.

First you need to decide what should be shown in a prompt (without any color):

PS1="[ \u: \w ]\\$ "
PS2="> "

Secondary prompt is simple. It shows just >.

However, the primary one is a little bit more complicated. It contains some special characters for additional info about a session. You can find a list of those special characters in bash’s man page under section PROMPTING:

man bash

I used \u (username), \w (current working directory) and, of course, \\$ (the $ or # symbol, depending on your privileges). So my primary prompt will be shown like this (when I’m in my home directory): [ penguin: ~ ]$

Now that you’re done with layout, it’s time to add some color. The simplest way to deal with it is by inserting variables from that chunk of code you pasted before. First put a $RS at the end of both PS1 and PS2 so you don’t change formating of other text in the terminal.

All ANSI color codes change only the text behind them, and you can only turn off $HC, $UL and $INV codes by using $RS. Here’s how I added color to my prompt:

PS1="$HC$FYEL[ $FBLE\u$FYEL: $FBLE\w $FYEL]\\$ $RS"
PS2="$HC$FYEL> $RS"

I used hicolor at the beginning and left it on until that last $RS. Then I just switched between colors I wanted (blue and yellow). So I got this prompt: [ penguin: ~ ]$

When you want to see what you’ve done, just save changes to .bashrc file, start another terminal and enjoy your new prompt. If you want to see how your secondary prompt looks like, enter just \ as a command in the terminal.

Tip: If you’re using gnome-terminal and want to fine tune your color selection, right click the terminal, choose Edit Current Profile… and go to Colors tab. Note that these changes effect the complete terminal, not just prompt. I don’t know how to do this for any other terminal emulator, I’m a GNOME man.

4. Reverting to old settings

If for some reason you wish to return the old prompt or if you broke your .bashrc, the easiest way is to just recover old .bashrc file you backed up. Run a terminal and use:

cp .bashrc-backup .bashrc

Related posts

Share this post


Leave a Reply