1

I noticed that there are two ways of setting the foreground color in your zsh prompt.

PROMPT='%F{blue}$'
PROMPT='%{$fg[blue]%}$'

Both of these lines will produce a blue $ as your prompt. Is there any difference between these two and is there a preferred way of setting your prompt?

If you want to specify a color code, you can do $F{160} or some other number but you can't for %{$fg[160]%}. Is this the only difference between these two options?

4

The direct %F{...} escape is handled directly by zsh C code. Using $fg[blue] is accessing an associative array named fg. For that to work, you need to invoke a colors function that comes with zsh. For prompts that also relies on the PROMPT_SUBST option.

There isn't a lot of difference in terms of what these two ways produce. The former is cleaner and works better with zsh's tracking of the terminal state. It was added to zsh in 2008 whereas the colors function was added in 1999. Nowadays, you're very unlikely to be stuck with a zsh old enough to lack either feature so there's no point in sticking to the old way anymore.

And as you noticed, %F can handle numbers where your terminal supports 88 or 256 colors. If you run a much more recent zsh, you can also specify hex triplets such as %F{#0000ff} if you have a true-color terminal (or load the zsh/nearcolor module).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.