How to make path prompt easier to read by color-coding each `/` (forward slash)?

I would like to get another configuration for the path, more a detail than a real request:

Below an example of zsh PROMPT :

prompt zsh

In order to make reading the path easier, I would like to change dynamically the color, except for the first slash (into ~/) of every slash in the path in blue (more precisely %F{75}) by the color pink (%F{13}) But I would like to be able to use with deeper path, that would help me to better locate myself into the tree.

On the image above, this would be only one pink slash changed (Phd_2019_2020/Travail_3_...).

Here's my current PROMPT:

PROMPT='%F{13}|%F{green}%n@%F{cyan}%m%F{13}|%f%T%F{13}|%F{75}%~%F{13}|%F{7} '

Is it possible to perform this dynamic change of color with zsh-5.8 ?


If I understand the question correctly, you want to have your prompt display the entire path of your current directory in blue but with each of the forward slashes ("/") in pink.

Unfortunately, you can't do this in your prompt.


Use the builtin precmd () function.

ZSH gives us a precmd function which is similar to Bash's PROMPT_COMMAND. These allow a command to be issued before the prompt is displayed.

I also wouldn't recommend a long prompt anyway as it makes usability very difficult when entering commands. Putting the full directory above your prompt is much better from a UI standpoint. The following code accomplishes that.

Simply put the following in your ~/.zprofile.

precmd ()  { printf "\n"; pwd | awk -F "/" ' {for (i=1; i<=NF; i++) printf "\033[01;34m"$i"\033[38;5;206m/"; printf"\n" }' }

And here are the results:

iTerm Window with precmd results

The above screenshot is my iTerm window showing that I have traversed deep into my /Applications/Firefox.app directory with the current directory formatted in blue separated by pink forward slashes.


How it Works...

I've expanded the one line premcmd function above so that it's multi-line and added in some variables so we can better see what's going on.

precmd() {
printf "\n";
pwd | awk -F "/" '{ blue="\033[01;34m"; \
                    pink="\033[38;5;206m"; \
                    for (i=1; i<=NF; i++) \
                      printf blue $i pink "/"; \
                      printf "\n"
                   }'
} 

Here's what it all means:

  • printf "\n" prints out a new line char. This is for aesthetics.
  • pwd returns the working directory name (man pwd) which is then piped ("|") to the next command, awk (man awk).
  • -F "/" defines the field separator to the forward slash; awk's default is a space. I kept my prompt to it's short and succinct setup.

Within the awk "program," delimited by the braces ("{" and "}") we have the following:

  • variables blue and pink are set to their ANSI escape codes \033[01;34m and \033[38;5;206m respectively.
  • We then have a for/do loop that iterates through each of the fields that awk has processed. NF is the total number of fields.
  • printf blue $i and printf pink "/" prints out the directory name in blue and the forward slash in pink