1

I want to color only the directories and not other files like .txt etc.

Here is my current .zshrc:

PROMPT='%n%F{086}—%f%M %F{057}::%f %F{028}%~%f %F{057}»%f '

If I take away the last %f PROMPT='%n%F{086}—%f%M %F{057}::%f %F{028}%~%f %F{057}» ' it colors all file types the same 057 color.

How can I target just the directories to apply color and set their own color apart from the 057 color?

vertexgraph
  • 131
  • 1
  • 6
  • 2
    You need to configure your version of `ls`, not your `zsh` prompt. – chepner Dec 31 '20 at 22:00
  • @chepner Why does the PROMPT variable change the color of files—when the last `%f` is taken out—using `ls`? – vertexgraph Dec 31 '20 at 23:19
  • It's not necessarily restricted to the prompt; each instruction simply tells the terminal to display *everything* in a particular color until you change it again. – chepner Jan 01 '21 at 14:05
  • @vertexgraph : Which part in your PROMPT actually displays a **file**? I just see that you have your working directory in your prompt string. – user1934428 Jan 02 '21 at 09:26

2 Answers2

12

I was able to add color to directories by editing the ls as according to the man ls

In .zshrc add:

alias ls='ls -G'

export CLICOLOR=1

export LSCOLORS=gxFxCxDxBxegedabagaced

The article Adding Color to Your macOS “ls” Output or reading through man ls is useful for knowing how to set which colors you want in the LSCOLORS variable.

Laurel
  • 5,965
  • 14
  • 31
  • 57
vertexgraph
  • 131
  • 1
  • 6
0

You cannot configure command line file type highlighting through your prompt. What’s happening is, because you don’t reset the foreground color (%f), it starts to bleed into your command line.

If you want to highlight certain parts of your command line, you need to do the following:

  1. Create a function.
  2. In there, parse the command line for file names. You can use words=( ${(Z+C+)BUFFER} ) to split the command line into shell words. See http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
  3. Use glob qualifiers to filter the shell words for different types of files. For example, dirs=( $^words(/) ) will get you all words that are directories.
  4. Use this info and the region_highlight special array to highlight the correct parts of the command line.
  5. Finally, use add-zle-hook-widget to set up your function as a line-pre-redraw hook, so it will get called each time the command line gets redrawn.
Marlon Richert
  • 5,250
  • 1
  • 18
  • 27
  • I'm looking for a way to do it without installing anything extra. if not adding that last `%f` colors everything else after it, there should be a way to target specific file types for what color they should be? – vertexgraph Dec 31 '20 at 23:27
  • I updated my answer with instructions on how to do that. – Marlon Richert Jan 01 '21 at 14:08