391

How can I configure Mac Terminal to have color ls output? I am using MacOS 10.5

bmike
  • 235,889
  • 6
    This is a great question... I've always expected ls --color (a la gnu ls) to just work everywhere. MacOS doesn't accept --color, so I assumed that it didn't have colored output as an option at all. – Armentage May 01 '11 at 16:35
  • https://www.cyberciti.biz/faq/apple-mac-osx-terminal-color-ls-output-option/ – slm Sep 14 '18 at 13:22

13 Answers13

443

Edit:

~/.bash_profile

or

~/.profile

and add the following line to simply enable color output via ls:

export CLICOLOR=1

To customize the coloring shown by ls you can optionally add this variable, LSCOLORS.

Examples

  • Default

    export LSCOLORS=ExFxCxDxBxegedabagacad
    
  • You can use this if you are using a black background

    export LSCOLORS=gxBxhxDxfxhxhxhxhxcxcx
    
  • If you'd like to mimic the colors of a typical Linux terminal:

    export LSCOLORS=ExGxBxDxCxEgEdxbxgxcxd
    

Once you've add the above to either ~/.bash_profile or ~/.profile you can either logout/login or source the file in your shell, for eg:

$ . ~/.bash_profile

NOTE: If you need help in selecting colors to use you can use this online tool called LSCOLORS Generator.

slm
  • 4,798
  • 1
    Is there a way to make the colored text not look bold? – MemphiZ Nov 04 '14 at 16:45
  • I found this (and up-voted it) in July 2015, when running Mavericks. Now on El Capitan (using the same .profile file, it no longer works. I can check the values using echo $LSCOLORS $CLICOLOR and they are as defined but it no longer works. Any reason this would be the case? – Steven C. Howell Jul 26 '16 at 02:01
  • 1
    @JamieCook I find Mikulas' colors more linux consistent. Specifically, a broken symlink should be dark red. With your lscolors its blue... this is not a small issue. – Ray Foss Jul 10 '17 at 14:51
  • 1
    Does not work on mac sierra and later – tread Mar 20 '18 at 19:35
  • 1
    @RayFoss indeed, that is no small issue, but OS X cannot make a difference between valid and broken symlinks by default. The only viable alternative is to use gnu ls – Ciprian Tomoiagă Aug 08 '18 at 08:23
  • 2
    @StevieG Works for me on High Sierra. Make sure you set it right: echo $CLICOLOR should give 1? – Franklin Yu Dec 11 '18 at 21:39
  • The default on Mac is actually LSCOLORS=exfxcxdxbxegedabagacad Source: https://www.freebsd.org/cgi/man.cgi?ls – ruohola Mar 30 '21 at 21:11
  • Note that .zprofile can be used for the same two lines, if you are using zsh. – MikeBeaton Oct 11 '22 at 08:48
201

You can add

alias ls='ls -G'

to your ~/.bash_profile to get colored ls output.

Barry Wark
  • 2,111
  • 47
    Mine is alias ls="ls -Gp" — the -p adds a slash after each directory. For me, it provides that much more visual differentiation, which is helpful. – Quinn Taylor Jun 12 '09 at 15:40
  • 42
    I see your -p and raise you a -F which in addition puts an * after executables, | after pipes, @ after symlinks, et cetera. –  Dec 27 '10 at 18:57
  • 26
    Just for fun, throw in a -h, which will format sizes in "human readable" units, i.e. 100b 10k, 23m, 4.2g – Armentage May 01 '11 at 16:29
  • 3
    If you don't have a .bash_profile already at ~/.bash_profile, be sure to source it, so that it will work. Do this with "source ~/.bash_profile" – Josh Hibschman Jul 28 '11 at 18:00
  • 2
    Dunno if this has changed since 2011, but you can replace the alias with export CLICOLOR=1 to accomplish the same thing. – David Lord May 31 '15 at 11:47
  • 2
    For the record, I usually prefer not to alias ls, because 1) If you actually memorize the command line parameters (which aren't that difficult to remember and type out), you'll be able to achieve the same behavior and output on multiple systems where you don't maintain an updated personal .bash_profile, and 2) because many times I like to pipe my ls output to other commands in order to execute operations on files/directories in the output, which always fails if there is a * or a @ included in the filenames (without first piping to tr or sed to remove the unwanted characters). – rubynorails Nov 21 '15 at 21:09
  • ok. then i advise to put -t to sort by date – Thupten Oct 03 '16 at 05:52
38

I find that all I need really is adding this to my ~/.bash_profile or ~/.bashrc (for Bash) or ~/.zshrc (for Zsh, the new default shell in Catalina)

export CLICOLOR=1
jaume
  • 15,010
filype
  • 483
13

If you want a readable Mac OS X Terminal color scheme, you may want to look into this:

I've been using this for over a year now, and I might not be able to function without it!

Here's an updated link:

grg
  • 201,078
Jeff
  • 231
  • 6
    As of Mac OS X Lion 10.7, Terminal allows customizing the ANSI colors, so using SIMBL or other extensions is no longer necessary. It also supports 256 colors. – Chris Page Sep 04 '11 at 08:59
12

Also you can customize the prompt color (and its format) by adding:

PS1='\[\e[0;33m\]\h:\W \u\$\[\e[m\] '

to ~/.bash_profile or ~/.profile file. Where 0;33 is regular yellow which looks nice in my black/semitransparent terminal window.

Here is a full list of colors and their explanations: https://wiki.archlinux.org/index.php/Color_Bash_Prompt

My awesome terminal window

Monomeeth
  • 64,558
nacho4d
  • 229
10

Personally, I'm using Oh My Zsh for adding color and other tricks to my Terminal. I think that is the easiest way.

oh-my-zsh is an open source, community-driven framework for managing your Zsh configuration.

It comes bundled with a ton of helpful functions, helpers, plugins, themes, and a few things that make you shout…

enter image description here

StrawHara
  • 4,893
8

Another option is to use the GNU ls which is part of the 'coreutils' program.

You can get it via Rudix or Homebrew (brew info coreutils)or Macports or Fink. That might be preferable to using a "Mac OS X-only" solution if you use the same shell config files on different systems, or are already familiar with GNU ls.

mmmmmm
  • 30,160
TJ Luoma
  • 20,659
7

UPDATE: I switched to Oh my zsh a year back and it is awesome. My favourite theme is lambda-mod and my version of it.


Combining all the answers here is what I use:

COLOR_RED="\033[0;31m"
COLOR_YELLOW="\033[0;33m"
COLOR_GREEN="\033[0;32m"
COLOR_OCHRE="\033[38;5;95m"
COLOR_BLUE="\033[0;34m"
COLOR_WHITE="\033[0;37m"
COLOR_RESET="\033[0m"

#git_color
function git_color {
  local git_status="$(git status 2> /dev/null)"

  if [[ ! $git_status =~ "working directory clean" ]]; then
    echo -e $COLOR_RED
  elif [[ $git_status =~ "Your branch is ahead of" ]]; then
    echo -e $COLOR_YELLOW
  elif [[ $git_status =~ "nothing to commit" ]]; then
    echo -e $COLOR_GREEN
  else
    echo -e $COLOR_OCHRE
  fi
}

#git_branch
function git_branch {
  local git_status="$(git status 2> /dev/null)"
  local on_branch="On branch ([^${IFS}]*)"
  local on_commit="HEAD detached at ([^${IFS}]*)"

  if [[ $git_status =~ $on_branch ]]; then
    local branch=${BASH_REMATCH[1]}
    echo "($branch)"
  elif [[ $git_status =~ $on_commit ]]; then
    local commit=${BASH_REMATCH[1]}
    echo "($commit)"
  fi
}

PS1='\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
PS1+="\[\$(git_color)\]"        # colors git status
PS1+="\$(git_branch)"           # prints current branch
PS1+="\[$COLOR_BLUE\]\[$COLOR_RESET\]\$ "
export PS1

export LC_ALL=en_US.UTF-8
export CLICOLOR=1
export LSCOLORS=gxBxhxDxfxhxhxhxhxcxcx
alias ls="ls -Gp"

enter image description here

5

Forget all those decades-old cryptic codes for gosh sakes, use the built in Terminal --> Preferences... Settings pane to set the default skin, and edit the ANSI colors to your liking. You can set the font, too. I prefer Menlo 12pt. This is how any regular Joe can do it, and avoid all the crazy command-line, unix-esque way of doing things as other posters have suggested.

  • 15
    I think anybody experienced enough to even know that ls can have colored output is also able to run a few Unix commands to configure it correctly. – nohillside Sep 21 '12 at 21:35
  • 10
    But this just makes ls show in one colour it does not make links, directories, files show in different colours as ls can do – mmmmmm Sep 25 '12 at 12:29
4

Simply add the following line to ~/.bash_profile file:

export PS1=" \[\033[34m\]\u@\h \[\033[33m\]\w\[\033[31m\]\[\033[00m\] $ "

Preview: enter image description here

This is my preferred colors. You can customize each part of prompt's color by changing m codes (e.g. 34m) which are ANSI color codes.

List of ANSI Color codes:

  • Black: 30m
  • Red: 31m
  • Green: 32m
  • Yellow: 33m
  • Blue: 34m
  • Purple: 35m
  • Cyan: 36m
  • White: 37m
4

For the Catalina version, you need to add export CLICOLOR=1 in ~/.zshrc.

If you want to adjust the colour palette, change colours theme in terminal settings.

Ebram
  • 41
1

Absolutely nothing worked for me. Eventually I found a very weird solution that fixed everything.

I opened ~/Library/Preferences/com.apple.Terminal.plist and searched for 'terminal' with Ctrl+F. I removed a section, that said 'DisableColor' or something like that.

0

Terminal Settings -> Profiles -> Shell

Check Run command: and enter export CLICOLOR=1

enter image description here

ttt
  • 641