How can I define custom colors for use in ZSH prompt?

ggelfond picture ggelfond · Dec 13, 2012 · Viewed 7.7k times · Source

I'm having some difficulty configuring my zsh prompt. Specifically I would like the font to have the color defined by the hex code: #87afdf

Currently, I've set up the prompt as follows:

PROMPT='%B[%d] 
➞  %b'

I've attempted to add colors in the following way:

autoload -U colors && colors

PROMPT='%{$fg[#87afdf]%}%B[%d]
➞  %b%{$reset_color%}'

But this only gives me the following gibberish:

$fg[#87afdf][/Users/gregory]
➞  $reset_color

Any ideas on how to proceed would be very much appreciated.

Answer

OneOfAKind_12 picture OneOfAKind_12 · Apr 10, 2018

You have to use a 256-color palette. You can see the numerical values for each of the 256 colors in ZSH using the following command:

for code in {000..255}; do print -P -- "$code: %F{$code}Color%f"; done

The same for bash:

for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Color"; done