How can I change the color of my prompt in zsh (different from normal text)?

前端 未结 10 1879
醉梦人生
醉梦人生 2020-12-07 07:34

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

相关标签:
10条回答
  • 2020-12-07 08:18

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

    0 讨论(0)
  • 2020-12-07 08:20

    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.

    0 讨论(0)
  • 2020-12-07 08:23

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

    0 讨论(0)
  • 2020-12-07 08:27

    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.

    0 讨论(0)
提交回复
热议问题