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

前端 未结 10 1855
醉梦人生
醉梦人生 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:05

    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

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

    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%}%% "
    
    0 讨论(0)
  • 2020-12-07 08:10

    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
    
    0 讨论(0)
  • 2020-12-07 08:11

    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.

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

    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.

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

    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
    
    0 讨论(0)
提交回复
热议问题