44

I have a new MacBookPro, and I installed the latest release of iTerm2.

Even after I imported various color schemes, I notice that they just change the color of the text and background only.

What I was expecting to see were different colors for files VS directories, VS hidden files, etc etc. How do I do that on iTerm2?

A related question is how do I actually specify what I want the colors of directories, files, etc to be?

Thanks

2

6 Answers 6

50

You have to do two things:

  • set the LSCOLORS environment variable
  • create an alias for ls so that it shows colors by default

If you are using Zsh - starting with Catalina it's the default shell, see MacOs Terminal zsh - cannot use ansi. Otherwise for Bash edit ~/.bash_profile and add the following:

export LSCOLORS="EHfxcxdxBxegecabagacad" 

alias ls='ls -lGH'        <-----This shows in list format, follow symlinks colorized

The the colors are set by each bit above; the first being foreground and the second being background. The first two characters refer to directories having a bold blue foreground and a light grey background.

However, there's a great online utility to see what each of the colors mean and look like in real time. It will even generate the "code" for you. (I am not affiliated with this at all). It will work in both MacOS/FreeBSD and Linux. Make sure you select the BSD option for macOS.

LSCOLORS Calculator


The order of the attributes are as follows:


1.   directory
2.   symbolic link
3.   socket
4.   pipe
5.   executable
6.   block special
7.   character special
8.   executable with setuid bit set
9.   executable with setgid bit set
10.   directory writable to others, with sticky bit
11.   directory writable to others, without sticky

The color designators are as follows:

a    black
b    red
c    green
d    brown
e    blue
f    magenta
g    cyan
h    light grey
A    bold black, usually shows up as dark grey
B    bold red
C    bold green
D    bold brown, usually shows up as yellow
E    bold blue
F    bold magenta
G    bold cyan
H    bold light grey; looks like bright white
x    default foreground or background
5
  • Hi Allan, thanks! A couple questions, can you explain some more about i) Why I have to put it in .bash_profile and not say, the .zshrc I think it was, and ii) a little bit more about the export LSCOLORS part? Do I go to that site you mentioned and set my color scheme from there? How does this jive then with the color schemes of iterm2? Thanks!
    – Spacey
    Commented Apr 28, 2017 at 16:38
  • Perhaps this is what is confusing me: I am using iterm2. What does this mean for which file we change? (.bash_profile VS .zshrc)? For what it's worth, on this new machine I currently have neither...
    – Spacey
    Commented Apr 28, 2017 at 16:45
  • Ah, ok, the echo shows bash. So I should make a new ~/.bash_profile I am guessing since none exists.
    – Spacey
    Commented Apr 28, 2017 at 16:49
  • I had to edit ~/.zprofile on a '21 MacBook Pro, as bash_profile isn't sourced for new terminals.
    – Billy
    Commented Dec 30, 2021 at 2:25
  • The following LSCOLORS string works well for me on a "Homebrew" themed Terminal.app tab with a black bg, yellow fg: "GxFxeHbHcxegecabagacad" = bold cyan directories, bold magenta symlinks, green executables, blue-on-white sockets, red-on-white pipes. The main win here is fixing that default dark blue directory color which is tough to read on a black bg.
    – Trutane
    Commented Apr 19, 2022 at 21:39
21

Open bash_profile using command:

open ~/.bash_profile

and add the following lines:

export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -GFh'

then source bash_profile using:

source ~/.bash_profile
4
  • KayV what does this do exactly? Can you expand please? Commented May 29, 2019 at 15:30
  • 2
    Thanks. This was what I was looking for my macbook. Best solution for coloring directories differently from files.
    – notilas
    Commented May 20, 2020 at 4:30
  • 1
    I found in zsh that the PS1 line just changed my terminal to that string.
    – Billy
    Commented Dec 30, 2021 at 2:23
  • This works but now my terminal prompt starts with (base) \[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]$
    – Jeff Bezos
    Commented Jun 16, 2022 at 17:06
10

For anyone using zsh, this is what I was able to do on my machine to get it to work:

1. Create the .zshrc file

touch .zshrc

2. Add these to the file

PS1="%{%F{033}%}%n%{%f%}@%{%F{green}%}%m:%{%F{yellow}%}%~%{$%f%}%  "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad

3. Restart iTerm2

Will produce a terminal that looks like this

Picture of my terminal with colors on for example

I was able to get this thanks to the different answers in this thread:

https://stackoverflow.com/questions/689765/how-can-i-change-the-color-of-my-prompt-in-zsh-different-from-normal-text#2534676

Higly recommend the thread to learn more on how the customizations work

4
  • 2
    this looks good
    – Jacky
    Commented Jun 15, 2022 at 4:26
  • 1
    This is perfect.
    – Jay Lim
    Commented Apr 15, 2023 at 14:01
  • You don't need to restart iTerm2 to use these, just open a new tab in iTerm2, or type source ~/.zshrc
    – jessica
    Commented Apr 27, 2023 at 22:43
  • worked for me well, Thanks! Commented Sep 15, 2023 at 6:30
7

An alternative to LSCOLORS is GRC (the GeneRic Colouriser), which can be used with pretty much any command-line app, not just ls.

If you've got Homebrew installed, install grc with brew install grc - this will set up aliases automatically, including for ls. It comes with aliases and config files for many different commands, and it's (relatively) trivial to hack a config file using Python regular expressions for any command that grc doesn't currently cover (tmutil and launchctl, for example).

(I was going to add this to the question I flagged as a possible dupe, but with your edit it's probably more useful here!)

3
  • I installed grc and ls was not colorized at all. 'type ls' gives '/bin/ls', nothing aliased. Back to the bash profile solution I guess. Commented Jul 11, 2019 at 16:06
  • 2
    @6005, did you install GRC with brew? (And did it install successfully, with no errors?) What does brew doctor report - any problems? What shell are you using, and have you modified the shell's config at all?You may need to edit your .bash_profile (or config for whatever shell you use) to source the GRC aliases. My .bash_profile has this (though I added it myself - my bash config is pretty esoteric...): [ -f /usr/local/etc/grc.bashrc ] && . /usr/local/etc/grc.bashrc
    – John N
    Commented Jul 12, 2019 at 17:22
  • 1
    Note: for some reason ls is commented out by default in the grc.bashrc when installed via homebrew.
    – justsome
    Commented Jun 22, 2020 at 13:58
1

Since OP asked specifically about iTerm2:

FYI the app now has Solarized color profiles available by default!

enter image description here

0

If you are using ZSH, you can try Oh My ZSH. If you just want to select from some pre-made themes which have sensible colors instead of manually selecting colors for all the various options for hours on end, Oh My ZSH is a good solution. A single command to install it and it just works.

9
  • OMZ should be avoided especially by novices. It creates more problems than it solves all for the goal of creating a CLI experience influenced by r/unixcorn.
    – Allan
    Commented May 4, 2023 at 18:01
  • @Allan, can you elaborate on the problems it causes? Commented May 4, 2023 at 18:03
  • Yes. People complain here of all sorts of weird shell behavior, none know how to do basic shell tasks only to find that uninstalling OMZ solves the issue. If you need a fancy prompt, you should learn how to do it manually, but by the time you learn it, you learn the fancy prompt is nothing useful.
    – Allan
    Commented May 4, 2023 at 18:09
  • I've had a Mac for a week now, and I just want a terminal that has basic stuff like search, themes, etc. Stuff that you would think a company with a 2.6 trillion dollar market cap would add into their SW ;) But I guess I'll be on the lookout for the shell weirdness. So far, the only weirdness I've seen has been introduced by Apple itself via shell sessions, weird default settings for Terminal, and the code in /etc/zsh_Apple_terminal. Maybe you know the answer to this: superuser.com/questions/1782411/… ? Commented May 4, 2023 at 18:39
  • You don’t need OMZ for “search”. We have tools like find, locate, and mdfind (same as the GUI Spotlight searches). OMZ will not help you with any of this. This $2.6T company is the only one making a certified Unix. All Linux distros are “unix like.” Most people aren’t aware of the power behind that Terminal screen with macOS
    – Allan
    Commented May 4, 2023 at 18:45

You must log in to answer this question.

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