How to change the color of my invitation in zsh (different from plain text)?

To better know the beginning and end of the output on the command line, I want to change the color of my prompt so that it differs noticeably from the output of the programs. How I use zsh, can someone give me a hint?

+88
colors zsh prompt
Mar 27 '09 at 13:29
source share
6 answers

Here is an example of how to set a red prompt:

PS1=$'\e[0;31m$ \e[0m' 

Magic is \e[0;31m (enable red foreground) and \e[0m (disable character attributes). These are called escape sequences. Different escape sequences give you different results: from absolute cursor positioning to color, from the ability to change the title bar of your window, etc.

For more information on escape sequences, see the ANSI escape wikipedia entry .

+46
Mar 27 '09 at 13:41
source share
โ€” -

Put this in ~/.zshrc :

 autoload -U colors && colors PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$reset_color%}%% " 

Supported colors:
Red, blue, green, cyan, yellow, magenta, black and white (from this answer ), although different computers may have different valid options.

Ambient color codes (and any other non-printable characters) with %{....%} . This is for the correct operation of text packaging.

+224
Mar 28 '10 at 21:37
source share

Zsh comes with color tips. Try

 autoload -U promptinit && promptinit 

and then prompt -l lists the available prompts, -p fire previews the -p fire prompt, -s fire sets it.

When you're ready to add a prompt to add something like this below the startup line above:

 prompt fade red 
+52
Dec 21 '10 at 0:32
source share

I donโ€™t think autoload -U colors && colors are needed anymore, and you can just do:

 PS1="%{%F{red}%}%n%{%f%}@%{%F{blue}%}%m %{%F{yellow}%}%~ %{$%f%}%% " 

to achieve the same result as the FireDude answer . See the ZSH documentation for more information.

+19
Apr 08 2018-11-11T00: 00Z
source share

I found that with zsh5 (by default on Debian Jessie) all these solutions work:

  • $'\e[00m
  • $fg[white]
  • $fg{white}

Now they have a problem: they will move the cursor, which leads to an ugly inscription when tabulating (for autocomplete). The solution is to surround the escape sequences with %{FOOBAR%} . It took a while to figure this out. For the 2nd and 3rd solutions, the loading of the colors module is required. To leave the 1st solution readable, simply define the variables for the colors you are using.

+4
Feb 29 '16 at 10:30
source share

man zshall and search for PROMPT EXPANSION

After reading the existing answers here, some of them conflict. I tried various approaches to systems working with zsh 4.2 and 5+, and found that the reason these answers contradict each other is because they do not say which version of ZSH they are aimed at. Different versions use different syntax for this, and some of them require different startups.

So, the best option is probably for a zshall person and search for PROMPT EXPANSION to find out all the rules for your particular zsh installation. Check out the comments, things like "I'm using Ubuntu 11.04 or 10.4 or OSX." Not very significant because it is not clear which version of ZSH you are using. Ubuntu 11.04 does not imply a newer version of ZSH than Ubuntu 10.04. There may be several reasons why an older version was installed. In this case, the newer version of ZSH does not imply which syntax to use without knowing which version of ZSH it is.

0
Jul 17 '14 at 16:02
source share



All Articles