Text coloring in zsh

tools May 9, 2020

You can color your text in zsh by using the %F{...} syntax. To view all available colors use:

for i in {1..256}; do print -P "%F{$i}Color : $i"; done;
Listing all possible colors in zsh

To reset the colors use the special value %F{reset}.

Usage example

I am displaying the current time in on the right hand side of my terminal, in dark gray. The clock will conveniently stop for executed tasks, so that I automatically can see when I started that particular task.

You can do that by using the following config in your .zshrc:

# Show timestamp whan a command
# was issued in the right prompt
RPROMPT='%F{240}%D{%H:%M:%S}%F{reset}'

TMOUT=1

TRAPALRM() {
    zle reset-prompt
}

Other Shells

Users of bash can use this guide: https://redandblack.io/blog/2020/bash-prompt-with-updating-time/


Photo credit: Robert Katzki

Tags