Customizing Zsh Prompt
While most zsh customization guides only go through the installation of the theme, there are times when you want to tweak minor things like changing colors, showing time, and how things are displayed altogether.
So in this tutorial, I will walk you through the following:
- Changing prompt text (the
username@hostname
part) - Changing colors and formatting
- Adding time to the corner part
Let's start with the first one.
Customizing the prompt text
By default, when you install zsh, you are welcomed with a boring prompt that shows nothing but a hostname in a plain white color:
This is a part where you start regretting your decision to install zsh in the first place but think of it as a blank canvas.
To make this prompt pleasant and informative, you need to use prompt expansion characters.
First, open the zsh config file using the following command:
nano ~/.zshrc
Here, paste the following lines:
#Default prompt
PS1="%n@%m %1~:"
Here,
PS1
: it is a shell prompt string variable that defines text displayed before each command you enter.%n
: it is used to display the username of the logged-in user.%m
: it shows the hostname of the system.%1~
: the%1
part shows the name of the current working directory and the tilde symbol will only be displayed if you are inside your home directory.
The @
and :
symbols were only used for aesthetic purposes.
Once you paste the line, save the changes and exit from the nano text editor. To take effect from the changes you've made, source the file:
source ~/.zshrc
After executing the above command, your terminal prompt will look like this:
But there are a few more prompt expansions that you can use:
Expression | Description |
---|---|
%j |
The number of jobs currently managed by the shell. |
%L |
The current value of the $SHLVL variable. |
%T |
The current time in 24-hour format. |
%r |
Show current time in a 12-hour format with seconds. |
%D |
Show the date in "yyyy-mm-dd" format. |
Change colors and formatting
Earlier, I explained how you can add/remove elements from your terminal prompt using prompt expansion characters and in this section, I will walk you through how you can color them.
Let's say I want to bold the username and hostname, then I will cover the hostname and username part with the %B
and %b
:
PS1="%B%n@%m%b %1~:"
After saving and sourcing the file, my prompt looked like this:
Also, if you want to add colors to the prompt, then you can use the %F{color}...%f
around the part you want to color.
For example, here, I wanted to apply a dark green color to the entire username@hostname
part so I used the %F{color}...%f
in the following manner:
PS1="%F{#008000}%B%n@%m%b %1~:%f"
After sourcing the file, my prompt looked like this:
I personally change the background color for the directory indicator so that the directory name will always be highlighted and easy to read.
The directory name indicator is %1~
so I will use the %K{color}...%k
around:
PS1="%F{#008000}%B%n@%m%b %K{#3a3a3a}%1~%k:%f"
Once done, save changes, exit from the editor, and source the file:
source ~/.zshrc
The final output will look like this:
Here's a list of options that I used in this section in case you want to take notes:
Start | End | Effect |
---|---|---|
%B |
%b |
Bold |
%U |
%u |
Underline |
%S |
%s |
Highlight |
%F{color} |
%f |
Foreground color |
%K{color} |
%k |
Background color |
Add time to the right corner
While the section title "Addition time to the right corner" won't do the proper justice as you can add everything you can or have added to the right side such as username, hostname, etc. but adding time will make more sense than those elements.
To add time or any other element to the right corner, first open the zshrc file:
nano ~/.zshrc
Here, you have to add a variable RPROMPT
and define what elements you want to add.
For example, here, I added a time in 12-hrs format so my line would look like this:
RPROMPT="%t"
The final result looks like this:
Looks boring? Let's bold the text and color it:
RPROMPT="%F{241}%B%t%b%f"
I added the dark grey with bold formatting to the time so the end result looks like this:
Looks simple, and gets the job done without any distractions.
Wrapping Up...
This was my take on how you can configure the zsh prompt without using any theme or extension and I strongly believe that zsh is one of the most flexible shells out there.
Sure, there are lots of extensions and themes that can be used to make it look and work even better but this tutorial was meant to show how easy it is to configure the zsh prompt without any additional package.
I hope you will find this guide helpful.