22

I just was introduced to Zsh and so far I am really liking the customizability.

I use the following line to set up colors in .zshrc:

zstyle ':completion:*' list-colors ''

This gives me these colors:

However when I use ls (I have aliased it to ls -G) I get the following colors:

Is there a way to make the Zsh list-colors the same as ls?

EDIT:

I have also tried setting the colors to the ls defaults from man ls without success (the colors still appear the same as the first image):

zstyle ':completion:*' list-colors 'exfxcxdxbxegedabagacad'      

5 Answers5

15

In my case, I also had to

export CLICOLOR=1

to get ls to colorize its output, as documented in man ls.

joelpt
  • 251
  • 3
    This may be necessary on BSD / MacOS systems, but with GNU ls implementations, --color is the only consideration. – HeroCC Jul 29 '19 at 21:49
14

The proper way of configuring colors with GNU ls is using LS_COLORS (see https://superuser.com/a/530467/175441). Now assuming you have LS_COLORS set, you should now use

# Zsh to use the same colors as ls
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}  

BTW, the above works. Search for list-colors in the Zsh manual (i.e. man zshall). If you cannot get this to work, try running this (short LS_COLORS for convenience...):

LS_COLORS='no=00;37:fi=00:di=00;33:ln=04;36:pi=40;33:so=01;35:bd=40;33;01:'
export LS_COLORS
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
Francisco
  • 2,298
  • 21
  • 21
  • Zsh doesn't like those colors. You get the same output as zstyle ':completion:*' list-colors '' – carloabelli Jan 29 '14 at 01:16
  • Did you check whether LS_COLORS is empty? Either way you are doing something wrong. The command I described is in the Zsh manual. – Francisco Jan 29 '14 at 09:38
  • @cabellicar123 make sure your TERM is included among valid terminals in the dircolors configuration file, i.e. include yours if it is not there. Otherwise, dircolors generates an empty value. – Francisco Jan 29 '14 at 10:14
  • 3
    To clarify, to set ls colors in ZSH you define LSCOLORS, which uses the 'exfxcxdxbxegedabagacad' format. GNU uses LS_COLORS which uses the 'no=00;37:fi=00:di=00;33:ln=04;36:pi=40;33:so=01;35:bd=40;33;01:' format, as does autocomplete. If you want them to match, use http://geoff.greer.fm/lscolors/ to generate both – Brad Urani Jan 02 '16 at 23:22
  • I tried the above but it doesn't seem to obey the ones I am putting, it seems to be using the default ones. I used a different variable: LS_COLORS='di=1:fi=96:*.m=31:*.py=32:*.txt=36:*.out=35' export LS_COLORS zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} – Charlie Parker Nov 12 '19 at 18:58
  • Why is this the proper way? The @carloabelli answer in conjunction with the notif about the CLICOLOR variable works just fine for me on MacOS – Scott Anderson Jul 27 '20 at 11:36
  • Should we define it inside .zshrc? – alper Jul 31 '21 at 18:30
  • I put zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} in ~/.zshrc, and worked, but only after I source'd the file twice ... – Life5ign Jul 04 '22 at 03:22
  • My god. Thank you @Francisco I was tearing out my hair. Was missing the third line, it solved my problem. – skytwosea Jun 01 '23 at 17:18
12

Default ls parameter --color=auto does colorize the output in zsh as well. Thus, there will be no need to use zstyle or CLICOLOR/LS_COLORS.

Simply adjust the following alias for your needs and append it to the .zshrc file.

alias ls="ls --color=auto"
4

Turns out zsh doesn't like the way ls stores its colors. You have to convert the colors into something zsh understands.

zstyle ':completion:*' list-colors 'di=34:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
2

If you are on MacOS the BSD version of ls does not use LS_COLORS, but you can use the GNU version of ls instead:

brew install coreutils
alias ls="gls --color"

From: https://github.com/sharkdp/vivid#on-macos