DEV Community

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

Posted on

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

Top comments (0)