How to Customize the zsh Prompt in the macOS Terminal

Since the update to version 10.15 Catalina, macOS includes Z shell (zsh) as default instead of Bash in the Terminal app. That said, you still get the same black window with white text inside the Terminal. Things don’t have to stay that way.

You can spruce up the zsh prompt to make it minimal and make the best of the space. We will show you how to customize the zsh prompt in the Terminal app to stand out on whichever background you choose.

Basics of the zsh Prompt

When you launch the Terminal app, it displays some useful information, like your last login and the command prompt. This is what the prompt typically looks like by default.

Last login: Wed Feb 3 22:00:40 on console
samir@MacBook-Air ~ %

The zsh prompt shows a string of text consisting of your username and computer’s model like MacBook Air, MacBook Pro, Mac Mini, and so on. The tilde (~) indicates the prompt’s location in the home directory.

Related: How to Open the Terminal on a Mac

Create a Z Shell Profile to Store All Settings

Typically, you have to tinker with the system files to change the default look. That’s because macOS updates reset all the system file to default, and you will lose all the changes you make.

So you can create a specific settings file, known as a dotfile, for your zsh profile to store all the changes and settings you want in the zsh prompt.

Well, you won’t find this dotfile by default on macOS, so you’ll have to make one. Creating a new .zshrc profile is recommended to store all the settings like the zsh prompt looks and behaves.

Here’s how to create the zsh profile (dotfile):

1. Open Terminal app.

2. Type the following command and hit the Return key.

touch ~/.zshrc

That’ll create a .zshrc profile in your user account’s home directory. You can see it under /User// path in the Finder if you have enabled viewing hidden system files.

After that, the zsh profile will be available for the login and interactive shells every time you launch Terminal. However, it won’t be active in the SSH sessions.

All the changes you want to make to the zsh prompt can be included in this profile.

Customize the zsh Prompt in Terminal

Typically, the default zsh prompt carries information like the username, machine name, and location starting in the user’s home directory. These details are stored in the zsh shell’s system file at the /etc/zshrc location.

PS1="%n@%m %1~ %#"

In this string of variables:

  • %n is the username of your account.
  • %m is the MacBook’s model name.
  • %1~ symbol means the current working directory path where the ~ strips the $HOME directory location.
  • %# means that the prompt will show # if the shell is running with root (administrator) privileges, or else offers % if it doesn’t.

To make any change to the default zsh prompt, you’ll have to add relevant values for the prompt to appear differently than the default.

Here’s how to go about that. Open Terminal, type the following command, and hit enter.

nano ~/.zshrc

It’ll be blank if you’re accessing it for the first time. You can add a new line with the text PROMPT=’…’ and include relevant values in the ellipses.

For a simple modification to the zsh prompt, you can type these values in the .zshrc profile.

PROMPT='%n~$'

Hit Ctrl + O to confirm making those changes to the file, and then hit Ctrl + X to exit the nano editor.

Open a new Terminal window to confirm and view the changes you’ve just made. Your new zsh prompt will show your mac’s username, home directory, and the $ symbol at the end.

Add Date and Time in the zsh Prompt

Apart from your username, you can append the current date or time so that you don’t have to look away from the active Terminal window to check that information.

Launch the Terminal and open the .zshrc profile.

nano ~/.zshrc

To include the date in the prompt, you can use %D for the date to appear in yy-mm-dd format or %W for it to appear in mm/dd/yy format. Then the new prompt will appear like

PROMPT='%n:%W:~$'

If you want to include the system time in the zsh prompt, add %T for the current time in 24-hour format, %t for the time to appear in am/pm or 12-hour format, or use %* to display the time in the 24-hour format along with seconds.

PROMPT='%n:%T:~$'

Add Color to Text in the zsh Prompt

Are you bored with the white text of the zsh prompt? Apart from modifying the Terminal app with colors, you can add some color to the text so that you get a nice visual break.

Launch Terminal and open the .zshrc profile.

nano ~/.zshrc

The zsh supports color and shades of gray to the prompt text such that it complements the background. You can pick a foreground (text) color between black, white, yellow, green, red, blue, cyan, and magenta.

Here’s how to use them.

PROMPT='%F{cyan}%n%f:~$'

If you want to pick a specific shade, you can choose between 0 to 256 8-bit colors that the Terminal supports. Here’s how to use the numerical values of the colors.

PROMPT='%F{51}%n%f:~$'

You need to place the %F and %f default foreground color variables between the relevant text you wish to color.

Adding Visual Effects in the zsh Prompt

If coloring the prompt’s text isn’t enough, you can highlight your zsh prompt so it stands out amid the text wall, and you can easily spot it. For starters, you can bold the zsh prompt.

PROMPT='%B%F{51}%n%f%b:~$'

Adding %B at the start and putting %b at the end between the relevant text will make it bold.

Similarly, you can put %S at the start and %s at the end to highlight the text. The highlight will carry the same color you’ve chosen to appear between the %S and %s variables.

PROMPT='%S%F{51}%n%f%s:~$'

You can also underline the zsh prompt’s text. For appending an underline to a prompt, you can include the %U variable at the start and %u at the end of the relevant text.

PROMPT='%U%F{51}%n%f%u:~$'

Remove the Last Login Details From the zsh Prompt

Hiding or disabling the top line revealing the last login time in the Terminal app can give your zsh prompt a cleaner look. You can use the following command to hide that information.

touch ~/.hushlogin

The next time you open the Terminal, you won’t see anything above the zsh prompt.

Spice up the zsh Prompt in Terminal

Even if you occasionally use the Terminal app on macOS, you can tweak the way zsh prompt appears there. You can bold, underline, shorten, and even include date and time in it.

Now that you have started to customize the zsh prompt, experiment a little and make it your own.

Source: makeuseof.com

Related posts

The Best Nintendo Switch Cartridge Holders of 2024

6 Polarizing Windows Features That Are Gone for Good

Connections #329: Today’s Answer and Clues (Sunday, May 5, 2024)