Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do zsh ansi colour codes work?

Tags:

zsh

zshrc

I want to make my hostname in my terminal orange. How do I do that?

like image 305
bneil Avatar asked May 28 '11 05:05

bneil


People also ask

What is ANSI color in terminal?

The ANSI ASCII standard represents the escape ESC character by the decimal number 27 (33 in octal, 1B in hexadecimal). This is one of the control characters (0-31 and 127), not one of the printable characters (32-126).


3 Answers

Running the following code in your terminal should tell you whether your terminal supports 256 colors.

for COLOR in {0..255}  do     for STYLE in "38;5"     do          TAG="\033[${STYLE};${COLOR}m"         STR="${STYLE};${COLOR}"         echo -ne "${TAG}${STR}${NONE}  "     done     echo done 

it also shows you the code for each color in the form 38;5;x where x is the code for one of the 256 available colors. Also, note that changing the "38;5" to "48;5" will show you the background color equivalent. You can then use any colors you like to make up the prompt as previously mentioned.

like image 68
Saad Farooq Avatar answered Sep 29 '22 08:09

Saad Farooq


First off, I'm not sure what terminal you're using or if it will even support the color orange. Mine supports the following: Red, Blue, Green, Cyan, Yellow, Magenta, Black & White. And here's how I get colors in my terminal:


You need to first load the colors using autoload. I use the following to load the colors and assign them to meaningful names

#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%}' 

You can set the hostname in your prompt using the %m string. So to set, say a red hostname, you'd do

${RED}%m${WHITE}\> 

which will print something like bneil.so>

like image 33
abcd Avatar answered Sep 29 '22 10:09

abcd


Print

<ESC>[33mHostname<ESC>[0m

Being the escape character \x1b

like image 39
Hyperboreus Avatar answered Sep 29 '22 08:09

Hyperboreus