Zsh – How to Get Function Into PS1 Prompt

promptzsh

This works in bash (parse_git_branch is a defined function)

export PS1="\$(parse_git_branch)"

But I cannot figure out the equivalent in zsh.

Note: If I do

PROMPT="$(parse_git_branch)"

It seems to work, but in fact it's running the command when I set the prompt, which is not the point.

Best Answer

You have to include

setopt PROMPT_SUBST

in your .zshrc, man zshall explains it in the PROMPT EXPANSION section:

If the PROMPT_SUBST option is set, the prompt string is first subjected to parameter expansion, command substitution and arithmetic expansion.

Related Question