256 colour prompt in Zsh

18,010

Solution 1

First, ensure that your terminal supports 256 colors, which I suppose you already have. Second, use a PS1 variable with the correct code, for example:

export PS1='%{^[[01;38;05;214;48;05;123m%}%m%{^[[0m%} '

This will give you a prompt with the host name in bold, with a foreground color of 214 and a background color of 123.

Note that the ^[ is "entered" by typing Ctrl+v and Ctrl+[. See this excellent article "That 256 Color Thing" for the whole list of attributes.

Solution 2

export PS1='%F{214}%K{123}%m%k%f'

From man zshmisc:

   %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.

   %K (%k)
          Start (stop) using a different bacKground colour.  The syntax is identical to that for %F and %f.

Also to try it out it could be used like that:

$> print -P '%F{214}%K{123}%m%k%f'
Share:
18,010

Related videos on Youtube

Sardathrion - against SE abuse
Author by

Sardathrion - against SE abuse

I am deeply saddened and worried by the abusing behaviour of stack exchange staff with regards to its users and have stopped all activities in this network. I urge you to do educate yourselves before it is too late. The final words… Firing mods and forced relicensing: is Stack Exchange still interested in cooperating with the community? Time line of events Sign the open letter, if you wish… I did. Nescire autem quid ante quam natus sis acciderit, id est semper esse puerum. Quid enim est aetas hominis, nisi ea memoria rerum veterum cum superiorum aetate contexitur? -- Cicero, Marcus Tullius (106-43BC)

Updated on September 18, 2022

Comments

  • Sardathrion - against SE abuse
    Sardathrion - against SE abuse over 1 year

    How can I set my prompt to be colourized in 256 colours? I am looking for an equivalent to the bash prompt:

      local DEFAULT="\[\033[0;39m\]"
      local ROOK="\[\033[38;5;24m\]"
      PS1="${ROOK}\$${DEFAULT} "
    
  • Amelio Vazquez-Reina
    Amelio Vazquez-Reina about 11 years
    How can I verify if my terminal supports 256 colors? For example, does gnome-terminal (Ubuntu) support 256 colors?
  • phunehehe
    phunehehe about 11 years
    Use this script (full article frexx.de/xterm-256-notes). gnome-terminal does support 256 colors.
  • LouisDev
    LouisDev almost 11 years
    The host of the link is broken (502 bad gateway).
  • Totor
    Totor over 10 years
    @Pikrass I have 404 not found.
  • phunehehe
    phunehehe over 10 years
    All the links seem to be broken, you can download the testing script from here: 256colors2.pl
  • phunehehe
    phunehehe over 8 years
    Not sure how I missed this earlier, but I updated the link in the answer with an archive.org one.