1

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 ?

3
  • 1
    Please don't change the question once you start getting answers. If problems with an answer come up, use comments beneath the answer; if new questions come up, use the "Ask Question" button. – nohillside Apr 18 '20 at 15:06
  • @nohillside You are rude, you have removed all my UPDATE1, please restore it temporaly at least, avoiding me to all rewrite for another question. – youpilat13 Apr 18 '20 at 17:19
  • It‘s still in the edit history, just click on the blue text on top of my name above – nohillside Apr 18 '20 at 19:47
5

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

6
  • thanks a lot ! Just a last thing, how to make the correspondance between the classical blue/pink colors and the colors that I would like to use, i.e %F{75} and %F{13} and mostly, how to declare them since the following function doesn't work : slash_color () { pwd | awk -F "/" ' {for (i=1; i<=NF; i++) printf "%F{75}$i%F{13}/"; printf"\n" }' } with the following PROMPT : PROMPT='%F{13}|%F{green}%n@%F{cyan}%m%F{13}|%f%T%F{13}|$(slash_color)%F{13}|%F{7} ' (using setopt PROMPT_SUBST before) ? – youpilat13 Apr 18 '20 at 10:23
  • look up the ANSI color codes I linked. "Classic" blue/pink isn't a reliable descriptor. The "blue" I selected came from the 4-bit color codes, so it's probably closest to your "classic" blue. Pink was in the 8-bit and 256-bit color sequences. – Allan Apr 18 '20 at 10:26
  • Also, you can't put a function in your PROMPT if that's what you're trying to do. My answer explains how to get what you want and that's to use precmd() – Allan Apr 18 '20 at 10:43
  • From this post superuser.com/questions/142099/get-function-into-ps1-zsh, I can use a function inside zsh prompt as long as I declare before setopt PROMPT_SUBST – youpilat13 Apr 18 '20 at 14:21
  • @youpilat13 - I'll have to take a look at that. ZSH has a TON of features that need to get enabled, but from my security side of my brain...I don't like the idea of a prompt calling a function..Example, I could write a wrapper that checks for sudo being issued and then issue a nefarious command to download and install malware with admin privileges. That said...there also a TON of useful stuff there as well. – Allan Apr 18 '20 at 23:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .