Zsh Prompt – Reevaluate Prompt Expression Each Time in Zsh

promptvariable substitutionzsh

I'm adjusting my zsh prompt, based upon the dallas theme and the dstufft theme from oh-my-zsh. I love how dallas has various sections of the prompt contained in variables, which makes it much easier to understand what's going on.

The problem is, these strings are evaluated once for expansion. So when I attempt to use something dynamic, such as the ${PWD/#$HOME/~} of dstufft, then it no longer updates dynamically.

How can I get the best of both worlds? I'd like the prompt broken up into subsections that are evaluated each time the prompt gets written.

Best Answer

Make sure that the prompt_subst option is turned on. If necessary, add the following line to your ~/.zshrc:

setopt prompt_subst

This tells zsh to reevaluate the prompt string each time it's displaying a prompt. Then, make sure you are assigning PS1 (or some other variable that is used by the prompt theme) as desired:

PS1='${PWD/#$HOME/~}'

The single quotes protect the special characters such as $ from being evaluated when you set the variable.

Related Question