logo
Jiff Slater
🤔 About
✍️ Contact
📚Knowledge
30 Jul 2021
These articles have been archived. You may find them useful but I am no longer offering support for them. Check out my latest articles on plkt.io.
Configuring the default prompt (PS1)
20 July 2008

I use the terminal every day and I have spent a lot of time customising its configuration, particularly the default prompt, to my liking.

Today, I will show you how to customise your default prompt, or PS1.  Before I show you my spartan configuration, let me explain.  I am a minimalist, so I do not see the point of having a long PS1 that shows a multitude of system information when the core *nix utils (/bin) can tell you that anyway.

Note: My terminal is rxvt-unicode, my shell is zsh, and I often run all my cli apps within GNU screen.

PS1="$(print '%{\e[1;31m%}%1%{\e[0m%} %{\e[1;32m%}%B%#%b%{\e[0m%} %(?..(%?%)) %_ \ek\e\\')"

Produces: ~ %

Explanation:  The entire PS1 expression is placed within a print command so my shell outputs a “null title-escape-sequence (<esc>k<esc>\) as a part of the prompt.”  GNU screen will then use the title-escape-sequence when naming the window (normally changed with C-a A).

%{\e[1;31m%}

– light red

%1

– trailing component of $PWD

%{\e[0m%}

– resets the color

%{\e[1;32m%}

– light green

%B

– start boldface mode

%#

– prints a # if I am a superuser (root) or a % if I am not.

%b

– end boldface mode

%{\e[0m%}

– resets the color

%(?..(%?%))

– takes advantage of zsh’s ternary expressions, which picks different strings depending on a test.  In this case, a message is displayed only if the exit code is not zero.

%_

: status of the parser (i.e. if, for, then, while)

\ek\e\\

– the title-escape-sequence for GNU screen

Other common variables you can use include:

%h

– history entry number

%m

– current hostname (up to any dot)

%n

– shows username

%D

– the date in yy-mm-dd format

See “zsh: Prompt Expansion” for the definitive list of zsh prompt sequences.

A list of “colour equivalences:”

Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37

Using a handy script I got from the bash manual, here is a picture of my current color scheme (zenburn):