How to customize your Ubuntu terminal prompt

This tutorial guides you to customize the look of the Ubuntu Terminal. The second part of the article can be applied to any other distribution as well.

The Terminal in Ubuntu, or in any GNU/Linux distribution for that matter, is the essential tool of all. It is the tool that is used to control, manipulate, and administrate over the whole system or even a group of systems.

Even though it looks like just a prompt with a host-name and user-name, its look has been improved over Linux’s years and is aesthetically pleasing in most graphical distributions now. Here, we’re going to explain how users can customize the look of their Terminal in the most commonly used distribution, Ubuntu. The final part of this article can be used in any other distribution as well.

Terminal Customization

Using the ‘Preferences‘ option:

Ubuntu’s Terminal has an existing ‘Preferences‘ option that can be used to customize the Terminal to some extent. It can be accessed by simply right-clicking on an empty area in the Terminal, and choosing ‘Preferences.’

Accessing preferences.
Accessing preferences

There are various options listed under multiple tabs. They have been listed and explained concisely below:

1. Text

Let’s explore the basic possibilities of changing the text style in the Terminal and some other options.

  • Terminal dimensions (in terms of rows and columns)
  • Font and font size
  • Spacing between cells
  • Cursor shape and blinking mode

Text options
Text options

2. Colors

Changes available are:

  • Available color themes
  • Default background and foreground (text) color
  • Bold text, cursor, and highlighted text color
  • Transparency slider

Color options
Color options

3. Scrolling

Options here are to:

  • Enable/disable scrollbar
  • Scrolling on output (the cursor comes back down to the bottom if there is new output)
  • Scrolling on keystrokes (the cursor comes back down to the bottom if any key is pressed)
  • Scrolling limit

Scrolling options.
Scrolling options

Those were all options that are provided by default for changes in the Terminal.

Customization using .bashrc file

Editing the parts and layout

Every Linux distribution has a Bash profile customization file in the home directory. It can be used (very extensively) to change the prompt’s appearance and function. The prompt is the part that comes up after a user logs into a user account. The default Ubuntu prompt looks like this:

Ubuntu default PS1
Ubuntu default PS1

The first part is the username, followed by the ‘@’ sign, and the host-name (the name of the system/server). Then there is the location of the working directory and finally a ‘$’ sign, indicating that its a non-root user. The root user has a ‘#’ sign, instead.

In conclusion, the default prompt looks like:

user@system-name:working_directory$

This prompt is represented by the name ‘PS1‘. The Bash profile file can be used to customize the PS1 prompt in multiple ways.

The default prompt is expressed as:

\u@\h:\w$

It can be explained as:

  • \u: The username of the user
  • @: The ‘@’ symbol
  • \h: The host-name of the system
  • ‘:’: The ‘:’ symbol
  • \w: The path of the working directory
  • \$: The ‘$’ symbol

Thus creating the default prompt layout. Now, some of the other options are the following:

  • \d: Date in the format ‘Weekday Month Date’ (like ‘Tue October 1’).
  • \t: Time in HH:MM: SS format.
  • \n: A newline (goes to the next line)

A more extensive list is available here. Now that we have learned a bit, we can try these out. However, for safety, save your current format using this command:

DEFAULT=$PS1

It saves the current format of PS1 to the variable ‘DEFAULT.’ Now, if we want to try just having the user name in the prompt, how would that be? According to the codes, just ‘\u.’ Therefore, we have to enter the following command:

PS1="\u$ "

The ‘$’ is necessary to act as a border. The expected result shows up:

Only username
Only username

We can even add Bash commands to show up in the prompt. That can be done the following way:

PS1="[`uname -sr`] \u$ "

Username and a command
Username and a command

The command that we used, “uname -sr,” prints the Linux Kernel version. Similar to this, you can use pretty much any other command. You can also add simple text like this:

PS1="(This is just sample text) \u$ "

Sample text
With sample text

NOTE: Brackets come off merely as brackets.

Brackets
Brackets

You can now revert to the original layout using:

PS1=$DEFAULT

Adding colors

Now we can move on to the difficult things, that is the actual .bashrc file. If we see the PS1 variable set there, it looks something like this:

\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]$

Not to worry, this is pretty simple too. You can see at first glance, that there is a pattern here. Opening using ‘\[‘ and closing using ‘\]‘. Other than that, there is always a ‘033′ before the other bunch of numbers and signs. What do these mean?

The ‘\[‘ and ‘\]’ indicate that the characters included within are non-printing characters, and denote some formatting of the text. It applies till another formatting bracket like itself, or to the whole part after this is written (if no other formatting is applied).

The ‘033’ denotes that the formatting is of color and look of the text.

The part that expresses color

The parts that express colors are the numbers ending with ‘m‘ So, ‘xxm‘ denotes a color. Some of these standard colors are:

  • 30: Black
  • 31: Red
  • 32: Green
  • 34: Blue
  • 37: White
  • 35: Purple
  • 33: Yellow

More elaborated lists can be found easily.

Okay, cracked most of it, just the leftover part is the numbers before semicolons that some of the colors have. These denote some specific text formatting, like the text being bold, underlined, etc. Some codes are as given:

  • 0: Normal text
  • 1: Bold text
  • 4: Underlined text
  • 2: Dim text
  • 8: Hidden text

Phew! Finally, let’s try having just a username, in color red, and formatted bold. The command would look something like:

PS1="\[3[1;31m\]\u$ "

Color changes
Color changes

Breaking it down one last time:

  • \[ : Opens formatting bracket
  • 033: Tells that this is going to format the text color and formatting.
  • [1; 31m: Tells that the text should be bold, and of the color red.
  • \]: Closes formatting bracket
  • \u: Fetches user name

NOTE: Remember; the square bracket opened after ‘033‘ is not supposed to be closed.

Now for the final part, manipulate everything you have learned to create the perfect PS1 format for yourself, and add it permanently the following way:

nano ~/.bashrc

Go to the end of the file, and enter your desired PS1 format.

Editing .bashrc
Editing .bashrc

Press CTRL + X, hit ‘Y’ and press ‘Enter’ to save the file.

New formatted PS1
New formatted PS1

Conclusion

So that’s (almost) everything you need to know about customizing your Terminal. The latter part of the article can go much deeper, but we have kept it to a limit so that users don’t get confused. I hope you enjoyed the article.

Pulkit Chandak
Pulkit Chandak is a Linux enthusiast and has been using and experimenting with open source software and hardware too since a long time. He is a huge admirer of open source software and wants to ventilate it to all around him. He is interested in reviewing and writing tutorials on Linux and its many distributions. He believes that freedom in software leads to freedom of the mind from the chains of limits.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,437FansLike
375FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to change Hostname on Ubuntu

Generally, a hostname is just an assigned name to a computer, laptop, or any other connected device to a network. You can say a hostname is a computer's nickname, and it is used to identify the machine over the network uniquely.

Manjaro vs. Ubuntu – which is better for you?

If you are a person associated with Computer technology and spend most of the time in the open-source arena, you must have heard or worked with some of the popular Linux distributions we have in the market. Some of the names that you will never miss are; Ubuntu, Arch Linux, Debian, and Mint.

9 Useful Tips Working with Operating System using Python

There are several situations when we want to work with the operating system using python. We may want to see the user details or want to do some tasks with the files and directories. If you are a system administrator, you will find it useful to work with Operating System as one can easily automate some repeating tasks of the Operating System using python.

System76 Lemur Pro Review

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.

Top 10 New Features in Linux Kernel 5.8

Linus Torvalds recently announced the release of Linux Kernel 5.8, and he seems delighted with it. He has pointed it out as the most significant release of all time. To developers, this new kernel comes with an addition of 800,000 new code lines and more than 14,000 changed files. To the average user, you might not see many eye-candy changes, as seen in the earlier releases.

How to install Flutter on Linux

Do you want to install Flutter and start creating awesome Android and iOS applications? Then you have come to the right place. Here is a comprehensive tutorial on how to install Flutter on Linux. If you are new to Flutter, don't worry, we got that covered too as we shall go in-depth understanding of Flutter and what it brings to the table.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

6 best task managers for Linux

One of the essential tools in any Linux distribution is a Task Manager. It is a system monitor application that gives you a report of all programs running on your computer and the status of your RAM and CPU usage.

3 Best Ways to Uninstall Software on Ubuntu

Uninstallation of programs can be done by graphical way using the Ubuntu Software Center, and the Synaptic Package manager. Command-line way of doing it is also possible using apt-get and aptitude commands. We shall discuss each one of them in detail.

6 Best Linux Distros for Laptops

Whether buying a Linux pre-installed laptop or selecting a Linux distro for your existing laptop, there are many things to consider. Let's take you through some of the best Linux distros that are optimized for Laptops in this 2019 edition of the article. Read on.

10 Top Reasons to Switch to Manjaro Linux

Manjaro is Linux distro based on Arch-Linux which follows a rolling release model. Is this distro good for you? Let's find out the main reasons for using Manjaro.