Ubuntu – How to change the font style of terminal prompt in Ubuntu MATE 16.04

command linefontsmatepromptubuntu-mate

The Mate terminal like any other terminal emulator in Ubuntu or any other OS has an input line like:

abc@xyz:~$

How to change the font style of this particular line?
Other than this one inputs commands to the line above and then the command generates messages thereafter. I want to set this text to a different style. I can do that using by going to Edit>Profile preferences. But this changes the Font style of the input line to the same one. I want there to be a difference between the input line text and rest of the text in the terminal so that after a command has printed messages verbosely, I can trace-back the input line without hard-gazing.

Ubuntu 16.04 seems to come with a default color for the input line that remains unchanged by tweaking options in Profile preferences.

Best Answer

(Some of the screenshots below reveal use of a particular text editor. If this is problematic for the reader, they are hereby begged to consider the use of this editor by the post author an act of blameless naivety)

The terminal prompt is a part of your user environment called PS1. It is defined in your .bashrc file, which you can edit to change it. You can see what it is currently with echo $PS1. Here is mine:

$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;35m\]\w\$\[\033[00m\]

This makes my prompt look like this compared to some command...

Making a coloured prompt is achieved by uncommenting this line in your .bashrc

#force_color_prompt=yes

so it looks like this:

force_color_prompt=yes

This causes the prompt to be set by the line after if [ "$color_prompt" = yes ]; then

which by default is:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

The colours are set by these codes:

  • light green \[\033[01;32m\]
  • white \[\033[00m\]
  • light blue \[\033[01;34m\]

and then back to white. You can see they are the same apart from the number in the second set of brackets. Here's a few more escape codes to choose from:

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

Here are some other options to make your prompt stand out

---All tested in MATE terminal on Ubuntu MATE 16.04---

When applying these changes to a white prompt, I leave this line commented:

#force_color_prompt=yes

And edit the fourth line of this section of .bashrc:

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\$ '

If you want to apply underline, and highlight effects to a coloured prompt, then as explained before you need to uncomment force_color_prompt=yes, and edit the second line instead of the fourth. In the examples below, I am referring to the "fourth line" and "second line" from the snippet above.

Bold prompt:

Change the fourth line to include the escape sequences \[\e[1m\] (bold) and \[\e[0m\] (stop bold)

PS1='${debian_chroot:+($debian_chroot)}\[\e[1m\]\u@\h:\w\$ \[\e[0m\]'

Not a big difference - I include the line above for comparison.

In the colour escape sequences "light" colours (with 1 instead of 0 as shown in table above) are considered bold. I tried using the bold sequence on the coloured prompt, but it seems MATE terminal doesn't allow this to make it even more bold. I also tried it using setaf code but still no result. Maybe I'm getting something wrong. I also couldn't manage an italic prompt, and I didn't even try a blinking prompt - that's just not OK.

Underlined prompt:

Change the fourth line to include the escape sequences \[\e[4m\] and \[\e[24m\]

PS1='${debian_chroot:+($debian_chroot)}\[\e[4m\]\u@\h:\w\$ \[\e[24m\]'

For underlined coloured prompt, uncomment force_color_prompt=yes and add the \[\e[4m\] sequence to the second line twice. The existing reset codes will terminate the effect:

PS1='${debian_chroot:+($debian_chroot)}\[\e[4m\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\e[4m\]\[\033[01;34m\]\w\[\033[00m\]\$ '

Highlighted prompt:

Change the fourth line to include \[\e[7m\] and \[\e[27m\]

PS1='${debian_chroot:+($debian_chroot)}\[\e[7m\]\u@\h:\w\$ \[\e[27m\]'

For colour highlighting, uncomment the #force_color_prompt=yes and change the colour escape codes in the second line to have a 4 instead of a 3, for example:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;42m\]\u@\h\[\033[00m\]:\[\033[01;44m\]\w\[\033[00m\]\$ '

To get the highlighting all one colour, remove the first reset and the second colour sequence:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;42m\]\u@\h:\w\[\033[00m\]\$ '

For more exotic options, see my favourite AU question and this site for a detailed guide.

Obviously I recommend making backups as you play around , but if you ever FUBAR your .bashrc you can just copy a fresh one from /etc/skel:

cp /etc/skel/.bashrc ~/.bashrc