There are two principal ways:
- Parameter expansion by enabling
PROMPT_SUBST
- The
psvar
array
1. Parameter expansion in prompt
If PROMPT_SUBST
is enabled
setopt PROMPT_SUBST
the prompt is subjected to parameter expansion, command substitution and arithmetic expansion before it is evaluated.
That way, the output of a script can be included via command substitution. For example:
PROMPT='Look at this: $(python yourscript.py) >'
If the output contains escape sequences (%~
, %M
, %F{red}
etc.) they will be evaluated before the prompt is printed.
2. The psvar array
One of the first nine values of the array psvar
can be set to the output of the script. It can then be recalled by using %Xv
, where X
is a number between 1 and 9 (defaults to 1 if X
is left out).
psvar[5]=$(python yourscript.py)
PROMPT='Look at this: %5v >'
In order to refresh the value every time before the prompt is printed, the hook function precmd
has to be set:
precmd() {
psvar[5]=$(python yourscript.py)
}
If there already is a precmd
function, or if more than one function is to be used, it is a good idea to use add-zsh-hook
.
# load add-zsh-hook, need to be done only once
autoload -Uz add-zsh-hook
pyscript() {
psvar[5]=$(python yourscript.py)
}
add-zsh-hook precmd pyscript
This adds pycript
to the list of functions that need to be run before printing the prompt.