How can I define custom colors for use in ZSH prom

2019-07-03 12:21发布

问题:

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.

回答1:

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.



回答2:

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