Search code examples
zsh

Translating PS1 Coloring from .bashrc to ZSH (.zshrc)


How may I translate the following coloring that I had previously on OSX .bashrc to work with ZSH .zshrc?

This was my original coloring setting with .bashrc:

export PS1="\e[0;32m\u@\h\e[0m \e[0;34m\W\e[0m \e[0;35m\$\e[0m "

With ZSH this coloring doesn't work (it's shown as typed in plaintext). How can I translate it to the color syntax that ZSH requires?


Solution

  • zsh is a bit simpler. Details can be found in man zshmisc.

    # No export needed
    PS1='%F{green}%n@%m%f %F{blue}%1~%f %F{magenta}%#%f '
    

    You can change the foreground and background colors by name, palette number, or RGB specifications using %F and %K; no raw ANSI escape sequences needed (and hence no need to wrap them in the zsh equivalent of \[...\]; zsh knows how to calculate the length of a prompt containing its own % escape sequences). %f (and %k) restore the terminal default colors.

    The other escapes map as follows:

    bash      zsh
    \u         %n
    \h         %m
    \W         %1~     Note you can choose how many path components to show
    \$         %#