ggelfond
ggelfond

Reputation: 341

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

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.

Upvotes: 10

Views: 22496

Answers (3)

luke
luke

Reputation: 4155

Solution for Oh-My-Zsh users

How to print available colors

As already mentioned you have to use a 256-color palette. The easiest way to see which colors are available is to use the following command (as ZSH uses spectrum underneath):

spectrum_ls 

it will print all available colors

enter image description here

...

enter image description here

How to use color in Oh-my-zsh theme

To use color in your theme you have to write it like $FG[<0-255>] for example $FG[172]
Upper case might be important there as $fg[172] does not work on my console!

Possible problem

Your terminal might support only 8 colors instead of 256. If it is true you will not see all the colors after executing spectrum_ls.
In such case you have to configure your terminal to support 256 colors.

Source

https://dev.to/yujinyuz/custom-colors-in-oh-my-zsh-themes-4h13

Upvotes: 9

OneOfAKind_12
OneOfAKind_12

Reputation: 566

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

Upvotes: 26

qqx
qqx

Reputation: 19475

Unless you're using a very unusual terminal, you can't use just any color combination that you would like. Standard terminals are limited to (at best) a 256-color palette.

The colors function which ships with zsh is simply to allow the colors from the old 16-color palette to be referred to by name, it will not help in using colors outside of that range.

There is a simple script available which will setup $FG and $BG arrays to provide a way to use colors from the 256-color palette by number, but without needing to deal with the escape sequences necessary for the terminal to deal with those.

Upvotes: 3

Related Questions