Reevaluate the prompt expression each time a prompt is displayed in zsh

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:


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

In zsh, precmd can do anything (such as setting a variable) before each prompt:

function precmd() {
    current_git_branch=`git rev-parse --abbrev-ref HEAD`

This isn't so necessary for the current directory as in the original question, but may be helpful for people who find this question for other cases.

(precmd is zsh-only — in bash, there is $PROMPT_COMMAND.)