DEV Community

Cover image for Customize prompt in Zsh: prompt in Z shell
terngr
terngr

Posted on

2

Customize prompt in Zsh: prompt in Z shell

Prompt customization is good to relieve stress from hard work. Follow the simple steps below to customize your Zsh prompt.

TL;DR

Image description
PROMPT='%F{green}%n%f@%F{blue}%m%f %(?.>.[%?]>) '

Customize user name color from green
Customize hostname color from blue

Add prompt contents

First, choose contents

  • %n User name
  • %m Hostname(First field only, divide by '.')
  • %M Hostname(Full)
  • %B%i~%b Last i level of current directory, replace i with number

Conditional content can also be added, such as an error code from previous command.
%(?.a.b)

  • If the previous command return zero, print 'a'
  • If the previous command return non-zero, print 'b', replace 'b' with '%?' to show an error code

Other ASCII Characters can be inserted directly

Put everything together and assign to PROMPT



sukkarin@M-208 >> PROMPT='%n@%M >> '
sukkarin@M-208 >>


Enter fullscreen mode Exit fullscreen mode

To show the error code, add '%(?.>>.[%?]>>)'
Listing the non-existing-file results to an error code [2]



sukkarin@M-208 >> PROMPT='%n@%M %(?.>>.[%?]>>) '
sukkarin@M-208 >> ls not-existing-file
ls: cannot access 'not-existing-file': No such file or directory
sukkarin@M-208 [2]>>

Enter fullscreen mode Exit fullscreen mode




Customize colors

  • %K{} Set background color from this point, put desired color code inside {}
  • %k Set background color to default, start from this point
  • %F{} Set character color from this point
  • %f Reset character color to default, start from this point

Color can be set by %F{blue} or %F{012}
See full list of XTerm Color Numbers

From the previous command. Set user name to green and hostname to blue.

Image description

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay