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
% 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.
- A %
- A )
- The current directory ($PWD)
- Same as
- $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.
- Trailing component of $PWD. If you want n
tailing componenets, put an interger 'n' after the
- Same as
- Just like
%. except that
~'s are never displayed in place of directory names.
Current time info
- The full machine hostname.
- The hostname up to the first
. (dot). An integer may
% to specify how many components of the
hostname are desired.
Current date info
- Current time of day, in 12-hour, am/pm format.
- Same as
- Current time of day, in 24-hour format.
- Current time of day in 24-hour format, with seconds.
- The date in day-dd format.
- The date in mm/dd/yy format.
- The date in yy-mm-dd format.
- 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.
- Current history event number.
- Same as
- The current value of
- Equivalent to
- 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
%_ 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'
- 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.
- The value of the first element of the psvar array
parameter. Following the `%' with an integer gives that
element of the array.