ZSH Prompt Colours in OpenSolaris snv_109 and Later

/zsh-prompt-colours-in-opensolaris-snv_109-and-later 2009-03-20T08:12:39+00:00

I've just updated my system to the latest OpenSolaris release (snv_109) and suddenly my ZSH prompt has lost all it's colours (I use them to clearly distinguish between me and the root user - I'm also a bit of a customisation freak ;-) ).

A little bit of research soon revealed that OpenSolaris snv_109 and later now ships with ZSH 4.3.9 in which they changed the prompt colouring scheme, and for the better too as it's far easier to understand.


From the ZSHMISC(1) man page:

     %F (%f)
          Start (stop) using a different  foreground  colour,  if
          supported by the terminal.  The colour may be specified
          two ways: either as a numeric argument, as  normal,  or
          by  a  sequence in braces following the %F, for example
          %F{red}.  In the latter case the values allowed are  as
          described for the fg zle_highlight attribute; see Char-
          acter  Highlighting  in  zshzle(1).   This  means  that
          numeric colours are allowed in the second format also.

So I need to change my PS1/PS2 from using the old $fg[color] format to the new format, which is easy enough. However I still need my colour prompt to work on pre snv_109 hosts, and this is where ZSH's is-at-least comes in handy.

So, now the code that sets my prompt looks as follows (I've removed the rest of the stuff around it):

if ( is-at-least 4.3.9 ); then
    PS1="%F{magenta}[%(!.%F{red}.%F{blue})%m : %F{green}%40<...<%~%F{magenta}]%F{blue}%(!.#.$) %f"
    PS2="%(!.%F{red}.%F{blue})%_< %f"
else
    PS1="%{${fg[magenta]}%}[%(!.%{${fg[red]}%}.%{${fg[blue]}%})%m : %{${fg[green]}%}\
%40<...<%~%{${fg[magenta]}%}]%{${fg[blue]}%}%(!.#.$) %{${fg[default]}%}" 
    PS2="%(!.%{${fg[red]}%}.%{${fg[blue]}%})%_< %{${fg[default]}%}"
fi

If you're curious, this give me the following prompt for a normal user:

          [devon : ...data/70478860/ftp-2009-01-07/crash]$

... and ...

          [devon : /usr/bin]#

... for root and as you can see in the first example, it truncates long paths too.

Copyright © 2005 - 2018 Colin Seymour All rights reserved. Privacy Policy.
Check out the feed if you do the RSS/Atom thing.