How can I change the color of my prompt in zsh (different from normal text)?
Solution 1
Here's an example of how to set a red prompt:
PS1=$'\e[0;31m$ \e[0m'
The magic is the \e[0;31m
(turn on red foreground) and \e[0m
(turn off character attributes). These are called escape sequences. Different escape sequences give you different results, from absolute cursor positioning, to color, to being able to change the title bar of your window, and so on.
For more on escape sequences, see the wikipedia entry on ANSI escape codes
Solution 2
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
, & white
(from this answer) although different computers may have different valid options.
Surround color codes (and any other non-printable chars) with %{....%}
. This is for the text wrapping to work correctly.
Additionally, here is how you can get this to work with the directory-trimming from here.
PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%(5~|%-1~/.../%3~|%4~) %{$reset_color%}%% "
Solution 3
Zsh comes with colored prompts builtin. Try
autoload -U promptinit && promptinit
and then prompt -l
lists available prompts, -p fire
previews the "fire" prompt, -s fire
sets it.
When you are ready to add a prompt add something like this below the autoload line above:
prompt fade red
Solution 4
The answer by Bryan Oakley above has a glitch as it has already been pointed out and the solution offered by Andrew Marshall though it does not carry the glitch, nevertheless it does not make it obvious for too much customization on the colors used.
As macOS Catalina asks for zsh to be the default shell from now on, I think several more people may want to customize their prompt and might be coming here for an answer. So, I thought I would try to give a broader summary and touch upon other very closely-related notions that allow more customization.
3-Digit Codes for Various Colors. First of all, here we can find 3-digit codes for various colors: https://unix.stackexchange.com/a/124409/194343. For example, 214 is some kind of orange color.
Foreground and Background. The other key information is that for Foreground and bacKground colors one can define what they want with F and K respectively. Source is zsh manual on visual effects: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Visual-effects
So, for example, the following two commands
autoload -U colors && colors
export PS1="%F{214}%K{000}%m%F{015}%K{000}:%F{039}%K{000}%~%F{015}%K{000}\$ "
present the hostname in orange with black background, followed by a colon in white with black background, followed by the current working directory in bright blue with black background, followed by the dollar sign in white with black background.
More related information is found below.
Prompt information on the right-hand side. For example, adding a timestamp. See https://superuser.com/a/1251045/290299. Of course, this can be color-coded, for example with some light blue/purple-ish color, like this:
RPROMPT="%F{111}%K{000}[%D{%f/%m/%y}|%@]"
Colors for ls
. After reading the manual for ls, one for example can activate the colors for ls
using the following two commands:
export CLICOLOR=1
export LSCOLORS=gafacadabaegedabagacad
Finally, as a last remark that I have not tested as I am happy with my configuration, another avenue might be for someone to install the port coreutils
from MacPorts and then use gdircolors
(source: https://unix.stackexchange.com/a/174596/194343). (I may edit this last part in the future as all the above are related pieces that make every-day life much more fun and easier to cope with.)
Solution 5
I don't think the autoload -U colors && colors
is needed anymore and one can simply do:
PS1="%{%F{red}%}%n%{%f%}@%{%F{blue}%}%m %{%F{yellow}%}%~ %{$%f%}%% "
to achieve the same result as FireDude's answer. See the ZSH documentation for more info.
Related videos on Youtube
Mnementh
My name is Jörgen Kosche. I'm a programmer using mostly Java.
Updated on November 20, 2021Comments
-
Mnementh over 2 years
To recognize better the start and the end of output on a commandline, I want to change the color of my prompt, so that it is visibly different from the programs output. As I use zsh, can anyone give me a hint?
-
Michal aka Miki about 15 yearsI am using PROMPT='[%!]' in my .zshrc. How can you color it? I run unsuccessfully PROMPT='\e[0;31m[%!] \e[0m' in my .zshrc.
-
new123456 over 13 yearsAgreed, by far the superior answer (luckily this answer was the third Google result ;)
-
dpc.pw about 13 yearsFYI. I've just tried in Ubunutu 11.04 and colors didn't work for me without
autoload -U colors && colors
-
Andrew Marshall about 13 yearsWeird. Works for me on OS X & Ubuntu 10.04.
-
Joe the Person almost 13 years@Andrew Marshall Yes, but it is more likely to work with
autoload
on. -
balki over 12 yearsnot working for me. :(
zsh: colors: function definition file not found
-
balki over 12 years@fireDude67 How to set
autoload on
? -
Joe the Person over 12 yearsAre you using ZSH4? I don't think it works on versions less than 4.3.11. If you have the latest version maybe it is not using the right files.
-
Joe the Person over 12 years@balki autoload -U colors && colors
-
chakrit almost 12 yearsWhat are the valid color values?
-
Emanuel Berg about 11 yearsThe
autoload
is not needed for me with Debian andzsh 4.3.17 (i686-pc-linux-gnu)
- also check out the documentation mentioned above for some cool effects, for example%K (%k)
for background color. Thanks for this, Mr. Marshall. -
Bruno Penteado almost 11 yearsthe
%{...%}
means that the content will be interpreted as a literal escape sequence, so the cursor wont move while printing the sequence. If you dont use this, the color codes can actually move the cursor and produce undesired effects. This is documented in theVisual Effects
section in zshmisc man page -
Russell Smith over 10 years@erikb85: why do you say it's wrong? Is there something I can do to make the answer better? For me, doing the above gives me a red dollar sign, so I don't understand why you think it's wrong.
-
Eric Dand about 9 yearsI needed the
autoload
in Crunchbang (essentially Debian) andzsh 4.3.17 (x86_64-unknown-linux-gnu)
. -
Moebius almost 9 yearsIt is working, but the $ before the string make zshrc to glitch. The text is put on the left when I use tabulation. I prefer @Joe the Person answer
-
diek over 6 yearsI agree @Moebius I wanted this one to work it was clear and simple, but it completely messes up the tab on cd
-
Pat Myron over 6 yearsIs there a way to color all the themes? I like the walters prompt but would like it in red.
-
kindall over 4 yearsIf you don't need
autoload -U colors
, that means that your distro already has it in some other zsh initialization file. You should still include it in scripts you intend to share with others. -
Charlie Parker over 4 yearsI thought zsh didn't need crazy weird ANSI escape stuff...there must be a better answer...
-
Charlie Parker over 4 yearsdo you mind explaining in detail what all the symbols in your prompt are doing or at least a link to a reference? e.g. what is are the
%
outside doing? Also what does things like{$fg...}
etc do? its difficult to understand your answer because it assumes to much knowledge of shells or zsh. Can you make it a little bit more self contained or link to references please? -
shaunakde almost 4 yearsThis is an immensely helpful answer. Thanks for putting this together.
-
Perette almost 4 yearsYour answer is missing the
%{
and%}
markers that identify the escape sequences. Without these,zsh
will count escape sequences as part of the display length of your prompt, and you'll end up with bizarre cursor positioning and line editor behavior on longer commands. Seeman zshmisc
, EXPANSION OF PROMPT SEQUENCES, Visual Effects. Not all shells require you do this; Korn shell, for example, parses common escape sequences in the PS1 string (though it will display the aforementioned problem on sequences, usually unusual ones, that it doesn't identify correctly). -
szeitlin almost 3 yearsthis sounded great but I don't like any of these options...
-
jsquaredz over 2 years
PS1='%(?.%F{green}.%F{red})%n@%m:%F{141}%d$ %F{reset}'
I went with this to add the PWD.