Understanding colors in zsh
The colors
function records the names of colors and similar attributes (bold, underline and so on) in the associative array color
. This array associates names with terminal attribute strings, which are numbers, e.g. 00
↔ normal
, 42
↔ bg-green
, …
echo ${(o)color}
If you want to see how the array is built, look at the source of the function: which colors
or less $^fpath/colors(N)
.
The colors
function only defines names and escape strings (in the associative arrays fg
and bg
) for the 8 standard colors. Your terminal may have more. See this answer for how to explore what colors are available.
Related videos on Youtube
Amelio Vazquez-Reina
I'm passionate about people, technology and research. Some of my favorite quotes: "Far better an approximate answer to the right question than an exact answer to the wrong question" -- J. Tukey, 1962. "Your title makes you a manager, your people make you a leader" -- Donna Dubinsky, quoted in "Trillion Dollar Coach", 2019.
Updated on September 18, 2022Comments
-
Amelio Vazquez-Reina over 1 year
In this thread, yoda suggests the following solution for using colors in
zsh
#load colors autoload colors && colors for COLOR in RED GREEN YELLOW BLUE MAGENTA CYAN BLACK WHITE; do eval $COLOR='%{$fg_no_bold[${(L)COLOR}]%}' #wrap colours between %{ %} to avoid weird gaps in autocomplete eval BOLD_$COLOR='%{$fg_bold[${(L)COLOR}]%}' done eval RESET='$reset_color'
Correct me if I am wrong, but if I understand correctly,
autoload colors && colors
allows you to call colors by their name, while the rest of the script just wraps them in${ $}
.This made me think about the following questions:
- Is there a way to know what colors are loaded by calling
autoload colors && colors
? - How do I know what colors are supported by my terminal?
- Is there a way to know what colors are loaded by calling