2015-05-10 update - I've been using some more complex stuff, which can be found around my shell-random/zsh github repository. See 3-interactive.sh and colours.sh
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".
Last updated 2017-12-18 at 10:25:21
thanks a lot for the tips on editing PS1 in zsh!
Previously, I have used something called TerminalColours.bundle which allows further coloring of the terminal by having folders appear one color, executables another, etc in your Terminal environment. Now I use Visor which includes this bundle.
Unfortunately, when i began using zsh instead of bash, the colors went away. In order to make colors work originally in bash, i had to add the lines
to my .bash_profile in my home folder. I've tried making a .profile and .zsh_profile as equivalents but this didn't do the trick!
do you have any ideas? your help is much appreciated, and i believe the solution is closely tied with this topic of changing colors which you have talked about ; )
I think these were the applications you mentioned:
TerminalColors:
https://web.archive.org/web/20190603214257/http://www.culater.net:80/software/TerminalColors/TerminalColors.php
Visor:
https://visor.binaryage.com/
I assume you're on a mac. If so, then things may be wildly different for you.
I only see something vaguely similar with my environment variables..
But your issue is at the shell level. This should probably work:
If so, then it's an issue at the ls level. Maybe this works?:
If that works then I'd suggest just solving this with an alias.
If not, then I have no clue, sorry.
For bright versions of the colors, try "fg_bold" instead of "fg": PS1="%{$fg_bold[red]%}%n%{$reset_color%}@%{$fg_bold[cyan]%}%m %{$fg_bold[yellow]%}%~ %{$reset_color%}%% "
The secret sauce is in using "fg_bold" instead of "fg" for the color names.
Hey, thanks a lot! In my ~/.zshrc I did:
One day I should bother with a more complex prompt.. I've been pretty minimal.