Prompt Expansion
****************
Prompt sequences undergo a special form of expansion. This type of
expansion is also available using the -P option to the print builtin.
If the PROMPT_SUBST option is set, the prompt string is first subjected
to _parameter expansion_, _command substitution_ and _arithmetic
expansion_. See Note:Expansion.
Certain escape sequences may be recognised in the prompt string.
If the PROMPT_BANG option is set, a `!' in the prompt is replaced by
the current history event number. A literal `!' may then be
represented as `!!'.
If the PROMPT_PERCENT option is set, certain escape sequences that
start with `%' are expanded. Some escapes take an optional integer
argument, which should appear between the `%' and the next character of
the sequence. The following escape sequences are recognized:
%%
A `%'.
%)
A `)'.
%d
%/
Present working directory ($PWD). If an integer follows the `%',
it specifies a number of trailing components of $PWD to show; zero
means the whole path. A negative integer specifies leading
components, i.e. %-1d specifies the first component.
%~
As %d and %/, but if $PWD has a named directory as its prefix,
that part is replaced by a `~' followed by the name of the
directory. If it starts with $HOME, that part is replaced by a
`~'.
%h
%!
Current history event number.
%L
The current value of $SHLVL.
%M
The full machine hostname.
%m
The hostname up to the first `.'. An integer may follow the `%'
to specify how many components of the hostname are desired. With
a negative integer, trailing components of the hostname are shown.
%S (%s)
Start (stop) standout mode.
%U (%u)
Start (stop) underline mode.
%B (%b)
Start (stop) boldface mode.
%t
%@
Current time of day, in 12-hour, am/pm format.
%T
Current time of day, in 24-hour format.
%*
Current time of day in 24-hour format, with seconds.
%n
$USERNAME.
%N
The name of the script, sourced file, or shell function that zsh is
currently executing, whichever was started most recently. If
there is none, this is equivalent to the parameter $0. An integer
may follow the `%' to specify a number of trailing path components
to show; zero means the full path. A negative integer specifies
leading components.
%i
The line number currently being executed in the script, sourced
file, or shell function given by %N. This is most useful for
debugging as part of $PS4.
%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 man page
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.
%l
The line (tty) the user is logged in on without /dev/ prefix. If
name starts with /dev/tty this is stripped.
%y
The line (tty) the user is logged in on without /dev/ prefix. It
does not treat /dev/tty* specially.
%?
The return code of the last command executed just before the
prompt.
%_
The status of the parser, i.e. the shell constructs (like `if' and
`for') that have been started on the command line. If given an
integer number that many strings will be printed; zero or negative
or no integer means print as many as there are. This is most
useful in prompts PS2 for continuation lines and PS4 for debugging
with the XTRACE option; in the latter case it will also work
non-interactively.
%E
Clears to end of line.
%#
A `#' if the shell is running with privileges, a `%' if not.
Equivalent to `%(!.#.%%)'. 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.
Negative integers count from the end of the array.
%{...%}
Include a string as a literal escape sequence. The string within
the braces should not change the cursor position. Brace pairs can
nest.
%(X.TRUE-TEXT.FALSE-TEXT)
Specifies a ternary expression. The character following the X is
arbitrary; the same character is used to separate the text for the
`true' result from that for the `false' result. This separator
may not appear in the TRUE-TEXT, except as part of a %-escape
sequence. A `)' may appear in the FALSE-TEXT as `%)'. TRUE-TEXT
and FALSE-TEXT may both contain arbitrarily-nested escape
sequences, including further ternary expressions.
The left parenthesis may be preceded or followed by a positive
integer N, which defaults to zero. A negative integer will be
multiplied by -1. The test character X may be any of the
following:
c
.
~
True if the current path, with prefix replacement, has at
least N elements.
/
C
True if the current absolute path has at least N elements.
t
True if the time in minutes is equal to N.
T
True if the time in hours is equal to N.
d
True if the day of the month is equal to N.
D
True if the month is equal to N (January = 0).
w
True if the day of the week is equal to N (Sunday = 0).
?
True if the exit status of the last command was N.
#
True if the effective uid of the current process is N.
g
True if the effective gid of the current process is N.
l
True if at least N characters have already been printed on
the current line.
L
True if the SHLVL parameter is at least N.
S
True if the SECONDS parameter is at least N.
v
True if the array psvar has at least N elements.
_
True if at least N shell constructs were started.
!
True if the shell is running with privileges.
%<STRING<
%>STRING>
%[XSTRING]
Specifies truncation behaviour for the remainder of the prompt
string. The third, deprecated, form is equivalent to `%XSTRINGX',
i.e. X may be `<' or `>'. The numeric argument, which in the
third form may appear immediately after the `[', specifies the
maximum permitted length of the various strings that can be
displayed in the prompt. The STRING will be displayed in place of
the truncated portion of any string; note this does not undergo
prompt expansion.
The forms with `<' truncate at the left of the string, and the
forms with `>' truncate at the right of the string. For example,
if the current directory is `/home/pike', the prompt `%8<..<%/'
will expand to `..e/pike'. In this string, the terminating
character (`<', `>' or `]'), or in fact any character, may be
quoted by a preceding `\'; note when using print -P, however, that
this must be doubled as the string is also subject to standard
print processing, in addition to any backslashes removed by a
double quoted string: the worst case is therefore `print -P
"%<\\\\<<..."'.
If the STRING is longer than the specified truncation length, it
will appear in full, completely replacing the truncated string.
The part of the prompt string to be truncated runs to the end of
the string, or to the end of the next enclosing group of the `%('
construct, or to the next truncation encountered at the same
grouping level (i.e. truncations inside a `%(' are separate), which
ever comes first. In particular, a truncation with argument zero
(e.g. `%<<') marks the end of the range of the string to be
truncated while turning off truncation from there on. For example,
the prompt '%10<...<%~%<<%# ' will print a truncated
representation of the current directory, followed by a `%' or `#',
followed by a space. Without the `%<<', those two characters
would be included in the string to be truncated.
%c
%.
%C
Trailing component of $PWD. An integer may follow the `%' to get
more than one component. Unless `%C' is used, tilde contraction
is performed first. These are deprecated as %c and %C are
equivalent to %1~ and %1/, respectively, while explicit positive
integers have the same effect as for the latter two sequences.