17. Prompting

The default prompt for zsh is:

Example 17.1: Default host prompt
phoenix% echo $PROMPT
%m%#

The %m stands for the short form of the current hostname, and the %# stands for a % or a #, depending on whether the shell is running as root or not. zsh supports many other control sequences in the PROMPT variable.

Example 17.2: Path and history escapes
% PROMPT='%/> '
/u/pfalstad/etc/TeX/zsh>

% PROMPT='%~> '
~/etc/TeX/zsh>

% PROMPT='%h %~> '
6 ~/etc/TeX/zsh>

%h represents the number of current history event.

Example 17.3: Host time user escapes
% PROMPT='%h %~ %M> '
10 ~/etc/TeX/zsh apple-gunkies.gnu.ai.mit.edu>

% PROMPT='%h %~ %m> '
11 ~/etc/TeX/zsh apple-gunkies>

% PROMPT='%h %t> '
12 6:11am>

% PROMPT='%n %w tty%l>'
pfalstad Fri 24 ttyp0>

PROMPT2 is used in multiline commands, like for-loops. The %_ escape sequence was made especially for this prompt. It is replaced by the kind of command that is being entered.

Example 17.4: Multiline PROMPT2 status
% PROMPT2='%_> '
% for i in foo bar
for>

% echo 'hi
quote>

Also available is the RPROMPT parameter. If this is set, the shell puts a prompt on the right side of the screen.

Example 17.5: Right-side RPROMPT
% RPROMPT='%t'
%                                                      6:14am

% RPROMPT='%~'
%                                               ~/etc/TeX/zsh

% PROMPT='%l %T %m[%h] ' RPROMPT=' %~'
p0 6:15 phoenix[5]                              ~/etc/TeX/zsh

These special escape sequences can also be used with the -P option to print:

Example 17.6: print with -P escapes
% print -P %h tty%l
15 ttyp1

The POSTEDIT parameter is printed whenever the editor exits. This can be useful for termcap tricks. To highlight the prompt and command line while leaving command output unhighlighted, try this:

Example 17.7: POSTEDIT highlight trick
% POSTEDIT=`echotc se`
% PROMPT='%S%% '

This trick is mostly superseded by the zle_highlight array parameter.