I’m pleased to announce that I’m the very first user to have ANSI colour in a zsh prompt. Yep, the very first. I must be, since there is no example to be found anywhere. After a serious amount of searching and fist-shaking at vague documentation, I was able to hack it out myself.
My solution is still incomplete though. Let’s walk through this nonsense..
The official documentation has a nice zsh guide. But since official documentation is garbage as a rule, there is some user-contributed documentation which isn’t so hopeless. But I couldn’t be so lucky as to actually get an example so I could accomplish my everyday certainly not out of the ordinary task.
I draw your attention to other functions. Yes, my completely standard desire was pushed to the bottom of the list. Who cares about something like colour in a prompt? Apparently only I do.
Here’s the documentation..
- colors
- This function initializes several associative arrays to map color names to (and from) the ANSI standard eight-color terminal codes. These are used by the prompt theme system (24.3 Prompt Themes). You seldom should need to run colors more than once.The eight base colors are: black, red, green, yellow, blue, magenta, cyan, and white. Each of these has codes for foreground and background. In addition there are eight intensity attributes: bold, faint, standout, underline, blink, reverse, and conceal. Finally, there are six codes used to negate attributes: none (reset all attributes to the defaults), normal (neither bold nor faint), no-standout, no-underline, no-blink, and no-reverse.
Some terminals do not support all combinations of colors and intensities.
The associative arrays are:
- color
- colour
- Map all the color names to their integer codes, and integer codes to the color names. The eight base names map to the foreground color codes, as do names prefixed with `fg-‘, such as `fg-red‘. Names prefixed with `bg-‘, such as `bg-blue‘, refer to the background codes. The reverse mapping from code to color yields base name for foreground codes and the bg- form for backgrounds.Although it is a misnomer to call them `colors’, these arrays also map the other fourteen attributes from names to codes and codes to names.
- fg
- fg_bold
- fg_no_bold
- Map the eight basic color names to ANSI terminal escape sequences that set the corresponding foreground text properties. The fg sequences change the color without changing the eight intensity attributes.
- bg
- bg_bold
- bg_no_bold
- Map the eight basic color names to ANSI terminal escape sequences that set the corresponding background properties. The bg sequences change the color without changing the eight intensity attributes.
In addition, the scalar parameters reset_color and bold_color are set to the ANSI terminal escapes that turn off all attributes and turn on bold intensity, respectively.
.
GREAT, no example anywhere. Thanks guys. Well at least the zsh user wiki has some decent examples in their prompt article.
autoload -U colors && colors
PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$reset_color%}%% "
.
Hey, something that’s not totally worthless! It actually works too.
But try as I might, I can’t make sense of how I should have light colours. Go ahead and look, there isn’t a single little hint on how to do that.
Oh, and I tried and tried and tried to hack together something that might work. But nothing was working as I expected.
I ended up breaking down and deciding to use straight ANSI codes. But of course nobody has ever used plain ANSI codes in a zsh prompt before. I did a lot more searching and all to no avail.
But I figured it out, no thanks to the complete lack of one single example.
In bash, i have this:
PS1="\w\[\e[34;1m\] > \[\e[0m\]"
.
In zsh, I created this:
PS1=%~$'%{\e[34;1m%} > %{\e[0m%}'
.
But I still have no clue how to do this the “pretty” way.
Come on people. How hard is it to have one teeny example for straight ANSI, and a few examples for the complex monstrosity that’s provided to “help”.
