Cool tips to change Ubuntu command line prompt colour

By | 09/10/2012

I have recently installed Ubuntu 12.04 LTS on my laptop. The first surprise that I got was in the form of the new unity GUI. Anyway, that’s a different story. Other thing that bothered me after sometime was the Ubuntu command line prompt colour. The concern was that while analysing problems where I had to go through lots of data on command line, I was many time lost as to where the output began.

This was because my command line prompt and the output were of the same colour. This problem bothered me a lot while I was analysing outputs for strace command tutorial. Just have a look at the image below and you will get the idea of what I am talking about :

Looking at the image above, you might get the idea of how difficult it was to search the command prompt between the two outputs. I researched a lot and finally I was able to change the colour of command line prompt. So, here in this article, I will share all the cool tips and tricks that I learned while understanding how to change Ubuntu command line prompt colour.

How to change Ubuntu command line prompt colour

1. Use the default green colour

Open your .bashrc file and search for the following line :

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

As you can see above, the comment itself says that if the line (in bold) is uncommented then the prompt will be coloured. So just uncomment the line (in bold) above.

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

Now, save the file and open a new terminal tab or window. You will see that the command line prompt colour was changed to something like :

So we see that the colour of the command line prompt was changed to green.

2. Don’t like green colour, read on

For those who are not happy and satisfied with the colour tips in point (1) above, there is nothing to worry. We can change the colour of the command prompt to any colour that is available. To do this, first find the following logic in your .bashrc file :

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

change the colour code 01;32 to any other combination like 0;31

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

Now just save your .bashrc file and open a separate terminal window or tab. You’ll see something like this :

So you see that the colour was changed to red. The details of colours and their codes are :

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

More details can be found here.

3. Change the colour of tilde (~)

For the curious bunch who are still not satisfied and want customize more, here is how this can be done.

In the same logic where we tweaked a bit in point (2) above :

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

To change the colour of the tilde (~) in your command prompt, change the area highlighted in bold above to :

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u@\h\[\033[00m\]:\[\033[1;33m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

This will change the colour of tilde(~) from blue to yellow :

So we see that the colour of tilde (~) was changed to yellow.

4. Change the colour of colon (:) and the dollar prompt ($)

This can be done by changing the following codes(in bold) at the following two places :

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u@\h\[\033[0;34m\]:\[\033[01;34m\]\w\[\033[0;34m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

This should change the colour of : and $ from existing black to blue.

So we see that the colour of both : and $ was changed to blue.

5. Make your user name look distinct

This can be done by inserting the following colour code (in bold) :

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u\[\033[0;32m\]@\h\[\033[1;35m\]:\[\033[1;33m\]\w\[\033[0;34m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

This should change the the colour of @ symbol and the name that follows it.

So you see that this way you can make your user-name look a bit different.

6. Change the colour of @

Well, if you want, the colour of @ can be changed too. Here is the code :

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u\[\033[0;32m\]@\[\033[0;35m\]\h\[\033[1;35m\]:\[\033[1;33m\]\w\[\033[0;34m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

This should exclusively change the colour of @.

So we see that the colour of @ was changed to green.

7. Change the prompt symbol

Well, this is the coolest of them all. Even the prompt symbol can be changed. First remove ‘\’ before ‘$’ and then change replace it with ‘#’.

Try this :
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u\[\033[0;32m\]@\[\033[0;35m\]\h\[\033[1;35m\]:\[\033[1;33m\]\w\[\033[0;34m\]# '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

This is how it looks like :

That’s cool. Isn’t it?

 

Some Useful Links

 

4 thoughts on “Cool tips to change Ubuntu command line prompt colour

  1. Oleg

    sudo apt-get install zsh
    In ~/.zshrc:
    autoload -Uz promptinit
    promptinit
    prompt elite2

    $ prompt
    Usage: prompt
    Options:
    -c Show currently selected theme and parameters
    -l List currently available prompt themes
    -p [] Preview given themes (defaults to all)
    -h [] Display help (for given theme)
    -s Set and save theme
    Switch to new theme immediately (changes not saved)

    Use prompt -h for help on specific themes.

    Reply
  2. David Dreggors

    In a fresh install of Fedora, Redhat, CentOS, Scientific, etc…

    It is far more simple, no need to use the “force_color_prompt=yes” in your .bashrc and even the PS1 line is simpler to use. Only ANSI colors codes:

    PS1='[\[\e[31;1m\]\u\[\e[0m\]@\[\e[32;1m\]\h\[\e[0m\] \[33[01;34m\]\W \[33[00m\]]\$ ‘

    No need for all that “${debian_chroot:+($debian_chroot)}” stuff.

    Reply
  3. Claude

    Thanks for the tutorial…it is very helpful. I was trying to change the color for a long time and your instruction is simple and works.

    Reply
  4. Zizhao Zhang

    Hi. Thanks for your helping.

    I follow it, but the color only changes when in root mode? Why did it happen?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *