MacOs Terminal zsh – cannot use ansi

terminalzsh

my .bash_profile is setting up my prompt like this

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

however, the result is that my prompt looks like this:

\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $  

Just to get the username I tried the following so ANSI could be used in zsh

PROMPT=$'%{\u%}'

but the prompt became empty, like if \u was ignored.

How could ANSI be used as usual within zsh?

Best Answer

Zsh provides a very easy way to add color to your prompt; you don't need Escape codes.

PROMPT='%F{blue}%n%f %#'
  • %F = Sets foreground color defined in braces {}
  • %f = resets to default foreground color
  • %n = username

You can even access the 256 color pallet for your colors by specifying the number within the braces - %F{146} for “light steel blue“

For a detailed list of all the codes and what they do, see the official Zsh Documentation - Chapter 13 Visual Effects.