237

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?

12 Answers 12

434

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%}%% "
7
  • 5
    Agreed, by far the superior answer (luckily this answer was the third Google result ;)
    – new123456
    Nov 14, 2010 at 5:02
  • 5
    not working for me. :( zsh: colors: function definition file not found
    – balki
    Sep 8, 2011 at 4:20
  • Are 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. Sep 8, 2011 at 22:53
  • 16
    the %{...%} 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 the Visual Effects section in zshmisc man page Jul 14, 2013 at 17:06
  • 2
    do 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? Nov 12, 2019 at 16:59
106

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
2
  • Is there a way to color all the themes? I like the walters prompt but would like it in red.
    – Pat Myron
    Oct 25, 2017 at 21:29
  • 6
    this sounded great but I don't like any of these options...
    – szeitlin
    May 20, 2021 at 17:54
82

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

6
  • 3
    I am using PROMPT='[%!]' in my .zshrc. How can you color it? I run unsuccessfully PROMPT='\e[0;31m[%!] \e[0m' in my .zshrc. Apr 29, 2009 at 3:22
  • 3
    @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. Nov 24, 2013 at 21:53
  • 4
    It 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
    – Moebius
    Jul 17, 2015 at 11:10
  • 3
    I thought zsh didn't need crazy weird ANSI escape stuff...there must be a better answer... Nov 12, 2019 at 16:52
  • 5
    Your 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. See man 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).
    – Perette
    Jul 28, 2020 at 20:36
51

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

1
  • 2
    This is an immensely helpful answer. Thanks for putting this together.
    – shaunakde
    Jul 16, 2020 at 19:49
34

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.

8
  • 1
    FYI. I've just tried in Ubunutu 11.04 and colors didn't work for me without autoload -U colors && colors
    – dpc.pw
    Apr 16, 2011 at 20:09
  • 2
    Weird. Works for me on OS X & Ubuntu 10.04. Apr 16, 2011 at 20:40
  • 1
    @Andrew Marshall Yes, but it is more likely to work with autoload on. Jul 26, 2011 at 3:53
  • @fireDude67 How to set autoload on ?
    – balki
    Sep 8, 2011 at 4:22
  • 2
    If 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.
    – kindall
    Aug 19, 2019 at 22:33
23

Try my favorite: put in

~/.zshrc

this line:

PROMPT='%F{240}%n%F{red}@%F{green}%m:%F{141}%d$ %F{reset}'

don't forget

source ~/.zshrc

to test the changes

you can change the colors/color codes, of course :-)

1
  • 1
    Only this works for me, after try hundreds PS1's that they all not works
    – a55
    Jun 8, 2022 at 9:27
9

In order to not waste space in the terminal with special characters to pieces of information, you can differentiate information using multiple colors.

Eg. in order to get this desired effect:

Desired Outcome

You can do the following to your prompt:

PROMPT='%F{magenta}${PWD/#$HOME/~} %F{green}${vcs_info_msg_0_} %F{cyan}$%F{reset_color} '

The way it works is every time you set a color using $F{myColor} the color from that point onward will stick to that. It's important to add in %{reset_color} at the end so that the input text goes back to the original color (or you could set it to something else if you'd like).

7

Debian/Ubuntu/Raspberry Pi OS style bash prompt:

autoload -U colors && colors
PS1="%B%F{034}%n@%m%f%b:%B%F{019}%~ %#%f%b "

Screen shot

Not entirely sure if autoload -U colors && colors is necessary but I did need it when I was using $fg[...] instead of %F{...}.
In my example;

  • %F(%f) starts(stops) foreground color - I've used 256COLOR values but you could also use hex for example, I've used normal green %F{034} but %F{#00aa00} works too
  • %B(%b) starts(stops) bold
  • %n $USERNAME
  • %m short machine name
  • %~ logged in/working directory with ~ if in home directory
  • %# display % if normal user or # if su

Edit: Tested on macOS Big Sur, Monterey and Ventura. Unsure of earlier zsh versions but Ventura 13.0 is zsh 5.8.1 (x86_64-apple-darwin22.0)

5

I have found that, with zsh5 (the default one on Debian Jessie), all those solutions works:

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

Now, they have a problem: they will move the cursor, resulting in ugly decal when tabbing (for auto-completion). The solution is simply to surround the escape sequences with %{FOOBAR%}. Took me a while to figure this. For 2nd and 3rd solutions loading colors module is mandatory. To keep the 1st solution readable, just define variables for the colors you use.

5

To get a prompt with the color depending on the last command’s exit status, you could use this:

PS1='%(?.%F{green}.%F{red})%n@%m:%~%# %f'

Just add this line to your ~/.zshrc.

The documentation lists possible placeholders.

1
  • PS1='%(?.%F{green}.%F{red})%n@%m:%F{141}%d$ %F{reset}' I went with this to add the PWD.
    – jsquaredz
    Aug 30, 2021 at 22:44
4

To complement all of the above answers another convenient trick is to place the coloured prompt settings into a zsh function. There you may define local variables to alias longer commands, e.g. rc=$reset_color or define your own colour variables. Don't forget to place it into your .zshrc file and call the function you have defined:

# Coloured prompt
autoload -U colors && colors
function myprompt {
  local rc=$reset_color
  export PS1="%F{cyan}%n%{$rc%}@%F{green}%m%{$rc%}:%F{magenta}%~%{$rc%}%# "
}
myprompt
1

man zshall and search for PROMPT EXPANSION

After reading the existing answers here, several of them are conflicting. I've tried the various approaches on systems running zsh 4.2 and 5+ and found that the reason these answers are conflicting is that they do not say which version of ZSH they are targeted at. Different versions use different syntax for this and some of them require various autoloads.

So, the best bet is probably to man zshall and search for PROMPT EXPANSION to find out all the rules for your particular installation of zsh. Note in the comments, things like "I use Ubuntu 11.04 or 10.4 or OSX" Are not very meaningful as it's unclear 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 any number of reasons that an older version was installed. For that matter a newer version of ZSH does not imply which syntax to use without knowing which version of ZSH it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.