Search code examples
zshpromptzshrc

Conditionally Color ZSH Prompt


I am trying to color my prompt based on the return code of the previous command. I have the following:

PROMPT='%{$fg_bold[cyan]%}%~$(git_prompt_info) %($?.%{fg_bold[cyan]%}.%{$fg_bold[red]%})»%{$reset_color%} '

Specifically I'm having trouble with the %($?.%{fg_bold[cyan]%}.%{$fg_bold[red]%}) portion.

Unfortunately my prompt ends up looking like this:

enter image description here

What am I doing wrong?

EDIT:

After fixing my typo I now have:

PROMPT='%{$fg_bold[cyan]%}%~$(git_prompt_info) %($?.%{$fg_bold[cyan]%}.%{$fg_bold[red]%})»%{$reset_color%} '

My prompt now looks like this:

enter image description here


Solution

  • You forgot the $ for the parameter expansion, and you don't precede the ? with a dollar; it's a flag for the conditional, not a parameter reference.

    %(?.%{$fg_bold[cyan]%}.%{$fg_bold[red]%})
      ^   ^