Using escape sequences in prompts

One of the very simple yet key ideas behind customizing your prompt is that there are many special 'escape' sequences that are used to specify different kinds of information. Most of these sequences start with a percent sign. (%) Then, within the PROMPT variable, any occurance of these % sequences are replaced by the information that they represent.

Some of these % escapes take an optional integer argument, which should appear immediately after the %, and before the next character of the escape sequence.

List and description of escape sequences

The % escapes are listed on the left. The information they are replaced by is described on the right.

NOTE: New escape sequences may be added in the future. Refer to the man pages (man zshmisc) for the definitive list.

Literal characters

%%
A %
%)
A )
Directories
%d
The current directory ($PWD)

%/
Same as %d

%~
$PWD, but will do two types of substitutions. If a named dir 'X' is a prefix of the current directory, then ~X is displayed. If the current directory is your home directory, $HOME, just ~ is displayed.

%c
Trailing component of $PWD. If you want n tailing componenets, put an interger 'n' after the %.

%.
Same as %c

%C
Just like %c and %. except that ~'s are never displayed in place of directory names.
Hostname info
%M
The full machine hostname.

%m
The hostname up to the first . (dot). An integer may follow the % to specify how many components of the hostname are desired.
Current time info
%t
Current time of day, in 12-hour, am/pm format.

%@
Same as %t

%T
Current time of day, in 24-hour format.

%*
Current time of day in 24-hour format, with seconds.

Current date info

%w
The date in day-dd format.

%W
The date in mm/dd/yy format.

%D
The date in yy-mm-dd format.

%D{string}
string is formatted using the strftime function. See strftime(3) for more details. Three additional codes are available: %f prints the day of the month, like %e but without any preceding space if the day is a single digit, and %K/%L correspond to %k/%l for the hour of the day (24/12 hour clock) in the same way.
Miscellaneous info

%h
Current history event number.

%!
Same as %h

%L
The current value of $SHLVL.

%n
Equivalent to $USERNAME.

%l
The line (tty) the user is logged in on.

%?
The return code of the last command executed just before the prompt.

%_
Print the state of the parser. More plainly, If you are in a for loop, %_ is replaced with 'for'. If you are in a for loop inside of a while loop inside of an if statement, %_ is replaced by 'if while for'

%E
Clears to end of line.

%#
A `#' if the shell is running with privileges, a `%' if not. The definition of privileged, for these purposes, is that either the effective user ID is zero, or, if POSIX.1e capabilities are supported, that at least one capability is raised in either the Effective or Inheritable capability vectors.

%v
The value of the first element of the psvar array parameter. Following the `%' with an integer gives that element of the array.