2

I'm using a Mac Silicon with the Ventura MacOS. In the Terminal app, all the file names have the same color. Take note that the default shell is zsh in Ventura, and is the one I want to use. Currently, my .zshrc config file has this content (so the directories are all shown with some color in the Terminal. I don't want to change this):

 PROMPT='%U%B%F{red}%n%u: ~%f%b'
 alias ls="ls -G"
 alias ll="ls -lG"

The -G option in BSD ls (also used on macOS) turns on colored output.

I would like to add something in there to change the color used to display hidden files (those whose name starts with a .) when using an ls option like -A that causes them to be shown. I would like all hidden files (but not directories) to be shown in some vivid red color in the Terminal (say magenta). Please, take note that I'm a noob with the Terminal commands (I only know some basic Unix commands), so I need something simple, no fancy tricks.

So how should I do this? What commands should I add into my .zshrc file above?

7
  • Do you want to always see hidden files? Or do you want them to be a specific color if you explicitly tell ls to list them using -a or -A?
    – terdon
    Commented Apr 11, 2023 at 18:34
  • @terdon, well, I want to see magenta invisible files as long as I use -a or -A
    – Cham
    Commented Apr 11, 2023 at 18:37
  • So you want to have hidden files (that's what they're called, the ones whose names starts with a .) visible but colored? Those are two separate things. To always see hidden files, make a new alias, like the ones you show there, something like la='ls -A' and then use la instead of ls. But please edit your question and clarify that you want two things: i) always show hidden files and ii) show them in a specific color.
    – terdon
    Commented Apr 11, 2023 at 19:11
  • @terdon, I don't see why my question isn't clear enough. I simply need to colorize all the files that start with a dot to some color in the Terminal app. I don't want these files to become visible in the Finder! I want the invisible files to be shown with some color in the list, to make them stand by.
    – Cham
    Commented Apr 11, 2023 at 19:22
  • Yes, but ls doesn't show these files. So you want two things: you want to see them in the output of ls (which is done by using ls -A) and you want to set them to a specific color. Finder isn't relevant, we're only discussing the command line.
    – terdon
    Commented Apr 11, 2023 at 19:53

2 Answers 2

3

The ls command is the only responsible with the appearance of what it prints. When ls is running, zsh only wait ls to exit. (The different color zsh options deal with what zshprints).

On MacOS, the way to setup the different ls colors are presented here : https://www.cyberciti.biz/faq/apple-mac-osx-terminal-color-ls-output-option/ See "Understanding LSCOLORS values" (man ls also show how colors are handled).

Unfortunately, you can't deal with the file/directory names, but only the kind of file (directory, socket, symbolic link, etc.) changes the color according to the palette you give.

One solution would be to use an other ls command, but the one from GNU doesn't seem to handle a .*pattern (about color handling). Only *.extension. Something like LS_COLORS='.*=01;33' ls -a indicates a unrecognized prefix. (Which is normal according to http://www.bigsoft.co.uk/blog/2008/04/11/configuring-ls_colors)

7
  • Your first link just explains the -G option which the OP is already using, so I don't think it is adding anything useful.
    – terdon
    Commented Apr 11, 2023 at 19:52
  • I give the reference manual about color configuration, and this reference doesn't provide a way to handle colors like the OP want. I would have prefer the reference to have something more powerful, but the fact is that the ls coloring scheme of the Mac is too limited. Commented Apr 11, 2023 at 20:01
  • Yes, I am just talking about the first link (cyberciti.biz/faq/apple-mac-osx-terminal-color-ls-output-option). Since the only vaguely relevant part is that -G enables colored ls output which is already in the OP, I don't see why you would link to some random site for that instead of just mentioning ls -G, that's all.
    – terdon
    Commented Apr 11, 2023 at 20:06
  • I have edited the post to stress the "Understanding LSCOLORS values". It is the key point since it indicates THE variable used to indicate colors. And the way to do it doesn't permits to have a color dependant of the file/directory name. Commented Apr 11, 2023 at 20:13
  • So should I conclude there's currently no way to colorize the hidden files (i.e that begin with a dot) in the Terminal, independently of all the other normal files? This seems odd to me.
    – Cham
    Commented Apr 11, 2023 at 20:15
1

Note that while GNU ls colouring can only be done (via $LS_COLORS usually set with the help of the dircolors utility) based on file metadata or fixed suffix, zsh completion colouring can be configured based on any pattern.

It extends GNU ls's $LS_COLORS specification with =pattern=attributes. So with:

# set LS_COLORS for GNU ls
eval "$(dircolors)"

# use same colouring for completions, but also specify magenta for hidden files
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS} '=.*=35'

Or if you don't have GNU ls and dircolors, just:

zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS} '=.*=35'

(using the default colours for the rest which are similar to GNU ls').

That's using the more modern list-colors zstyle, but the syntax is the same as for $ZLS_COLORS).

Then assuming you've enabled coloured completion via compinstall or manually, you'll see hidden files in magenta.

You'll likely also want to either enable the globdots aka dotglob option (which affects all glob expansions), or add:

zstyle ':completion:*' file-patterns '*(D)'

For hidden files to be included by default in completion listings.

A minimum ~/.zshrc to test that would look like:

zstyle ':completion:*' menu select=2
zstyle ':completion:*:default' list-colors '=.*=35'
zstyle ':completion:*' file-patterns '*(D)'
autoload -Uz compinit
compinit -i

But again, best would be to use compinstall to tune the completion system to your taste and add those two list-colors and file-patterns style separately.

In any case, that's only for zsh completion listing, that won't affect ls output.

2
  • I tried your last code. It doesn't work. I get that error message: command not found: dircolors
    – Cham
    Commented Apr 12, 2023 at 10:52
  • @Cham, dircolors comes with GNU ls, both part of GNU coreutils. Commented Apr 12, 2023 at 12:44

You must log in to answer this question.

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