zsh

How Do I Change My ZSH Prompt Name

ZSH is a Unix shell that is useful for interactive login and command interpretation. It is an extension of the Bourne Again shell offering additional features such as plugins and themes.

However, you may find the default ZSH shell lacking and want to customize it to fit your needs. This tutorial will discuss customizing the ZSH prompt to display helpful information and beautifying it with custom colors.

Setting up the Environment

Before we get to customizing the ZSH prompt, let us ensure we have ZSH installed and working on our system.

By default, most Linux distros don’t have ZSH installed, but, fortunately, you can use the package manager to get it up and to run.

Use the commands provided below:

# Debian/Ubuntu

sudo apt-get install zsh

Arch/Manjaro

sudo pacman -S zsh

Fedora/CentOS

sudo yum install zsh

Once we have zsh installed, set it as the default shell using the command:

chsh -s $(which zsh)

If you are running ZSH for the first time, a prompt will ask you to configure the .zshrc configuration. You can select the default configuration by pressing 2.

If you haven’t done any ZSH configuration, the prompt will be in the form of [email protected], the current working directory, and then the prompt symbol.

debian@hostname ~/Documents %

How to Create the ZSH configuration File

To customize the ZSH prompt, we will need to create the configuration file. If you used the default ZSH configuration, you need not worry about this.

However, if you do not have the ZSH configuration, you can create it using the touch command in your home directory.

touch ~/.zshrc

Next, edit the configuration file using your favorite text editor.

vim ~/.zshrc

NOTE: If you wish to edit the .zshrc file using a graphical text editor, you will need to enable “show hidden files” in your file manager. Use the shortcut CTRL + SHIFT + H.

How to Customize the ZSH Prompt

To customize the ZSH prompt, we need to modify the prompt= variable inside the .zshrc file. We can populate the prompt variable with various placeholders, which will alter how the ZSH prompt appears.

Although we will mention some of the essential prompt placeholders, you can learn more about many of them in the official documentation.

https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html#Prompt-Expansion

If you are using the default ZSH configuration, the prompt will take the form of:

%K{blue}%n@%m%k %B%F{cyan}%(4~|...|)%3~%F{white} %# %b%f%k

To determine the prompt format, use the $PS1 variable.

echo $PS1

%K{blue}%n@%m%k %B%F{cyan}%(4~|...|)%3~%F{white} %# %b%f%k

Let us break down the components of the above prompt format.

%K – This option tells the prompt to start in a different background color. It is similar to the %F. To set a color, set %K followed by the name of the color in curly braces.

%n – This displays the username.

%m – The system’s hostname up to the first ‘.’ You can add an integer value after the % to indicate how many hostname components you desire. If you want to display the full hostname, use the %M instead.

%B – Start Boldface mode.

%F – This is similar to the %K and starts in a different background mode. Similarly, you can pass the color inside a pair of curly braces in numerical or normal format.

% – Shows the prompt symbol.

Now that we have a complete idea of how the PROMPT variable works and various placeholders we can use, let us customize our prompt. We will not do anything crazy with our example; we shall only add a few descriptive information.

Let us begin by showing the previous command’s exit code, which we can do using the ? symbol.

If the exit code is 0, we display a unique symbol; otherwise, show the exit code. To illustrate this as the prompt, we add the placeholders as:

%(?.√.?%?)

In the above example, we check if the exit status is equal to 0; if true, we display a square root symbol; if not, we show the status code.

Finally, we should display the current working directory. If the pwd is home, we display a squiggly symbol as (~).

Once completed, we should have the prompt variable with the values as shown:

PROMPT='%(?.%F{blue}√.%F{red}?%?)%f %B%F{240}%1~%f%b

Save the file and apply the changes with the command:

source ~/.zshrc

By the end, you should see a prompt similar to the one shown below:

If the exit code is 0, it shows a square root symbol in the blue color, and if not, it displays the exit code in the red color indicating an error.

How to Set Values For The Right Hand Prompt

To set the values for the right-hand prompt, use the $RPROMPT variables. For example, to show time on the right-hand set the variables as:

RPROMPT='%*’

The above placeholders will show the current time of day in the 24-hour format.

Here is an example prompt format:

Closing

In this tutorial, we learned how to customize our ZSH prompt using pre-defined prompt placeholders. Consider the ZSH prompt customization docs to learn more.

Thank you for reading.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list