538

I just got a Mac after working with Ubuntu Linux for some time. Among the many things I'm trying to figure out is the absence of colors in my terminal window- like the ones that are shown (on Linux) when running ls -la or git status.

How can one activate colors in their shell?

starball
  • 20,030
  • 7
  • 43
  • 238
sa125
  • 28,121
  • 38
  • 111
  • 153
  • 10
    possible duplicate of [How to configure Mac Terminal to have color ls output](http://stackoverflow.com/questions/973899/how-to-configure-mac-terminal-to-have-color-ls-output) –  May 24 '10 at 00:31
  • 40
    Closed as "off topic" but look at the tags: osx, shell, terminal, colours ... seems like to me it's totally on-topic. Covers OSX, it's about the shell, using terminal and wants to add colours. Have to edit the bash profile, so yeah, I'd say it's on-topic. – Danijel-James W Mar 08 '14 at 08:37
  • 15
    Clearly on topic, it's about bash scripting. – superluminary Jul 20 '14 at 18:19
  • 7
    Closed, you have to be kidding! Clearly this has utility and value to many (mostly new) Mac coders. If those closing are serious about it being off topic, then why not migrate it to say SE Think Different. I take it SO supports that function used in other SE groups. – Cam_Aust Jul 13 '17 at 10:50

7 Answers7

802

Here is a solution I've found to enable the global terminal colors.

Edit your .bash_profile (since OS X 10.8) — or (for 10.7 and earlier): .profile or .bashrc or /etc/profile (depending on availability) — in your home directory and add following code:

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

CLICOLOR=1 simply enables coloring of your terminal.

LSCOLORS=... specifies how to color specific items.

After editing .bash_profile, start a Terminal and force the changes to take place by executing:

source ~/.bash_profile

Then go to Terminal > Preferences, click on the Profiles tab and then the Text subtab and check Display ANSI Colors.

Verified on Sierra (May 2017).

0xPixelfrost
  • 10,244
  • 5
  • 39
  • 58
  • 21
    just did this by adding it to .bash_profile and it works. didn't work when I added it to .profile though. Thanks! – Morten Aug 14 '11 at 07:40
  • on mountain lion I modified my `/etc/profile` and it works. For some reason I did not have a `~/.bash_profile` Thanks! – Alex Spencer Jan 11 '13 at 23:39
  • 3
    To @Morten and any others struggling with their bash profiles, see these answers for an explanation and a solution: http://stackoverflow.com/a/7780055/665488, http://superuser.com/a/244990. – Cam Jackson Jan 15 '13 at 00:12
  • 1
    @AlexSpencer you should be able to just create it: a text file called `.bash_profile` in your home directory containing the exports in the answer. – drevicko Mar 13 '13 at 23:41
  • 1
    To make it work in your .bashrc, simply add `source .bashrc` to your .bash_profile. Don't forget to enter `source .bash_profile` after modifying the files. – Cody Poll Apr 14 '13 at 21:47
  • 4
    [dotfiles/.aliases](https://github.com/mathiasbynens/dotfiles/blob/master/.aliases) has a great `export LS_COLORS` statement – bobobobo Apr 22 '13 at 18:23
  • command+c the code shown above, open terminal, vi ~/.profile, press i, arrow key down to the bottom, make sure it is a new line, command+v, press escape, type !wq, source ~/.profile, quit terminal, open terminal again, type ls, say byebye to black&white term. – Gary Tsui Sep 14 '13 at 04:20
  • 1
    Well, I made this one line command if anyone needs it ;) `printf 'export CLICOLOR=1\nexport LSCOLORS=GxFxCxDxBxegedabagaced' >> ~/.bash_profile` – Giacomo Pigani Oct 29 '14 at 08:11
  • @Midas: try http://ohmyz.sh/. There are themes and is more powerful than the default shell. – 0xPixelfrost Jun 09 '16 at 13:44
  • You have to check `Displasy ANSI colors` from Preferences then, on El Capitan at least – Jemshit Nov 16 '16 at 12:35
  • 2
    Just tried, it works on macOS Sierra (10.12). After making changes to .bash_profile do not forget to: 1. In terminal type, `source ~/.bash_profile` 2. In terminal preferences, select 'Display ANSI colors' under profiles tab. @MatthewBordas @JohnnyHuo – Vineet Kapoor Nov 19 '16 at 15:04
  • 1
    Worked it macOS Sierra (10.12). I edited ~/.profile and opened a new window. No other changes necessary. – ChrisBob Feb 20 '17 at 14:37
  • 26
    If you want the same colors that Linux uses (at least Debian and Ubuntu), use `LSCOLORS=ExGxFxdxCxDxDxxbaDecac` – remram Jul 24 '17 at 18:53
  • Nice work. More details on what the letters in the customization string mean, here: http://www.marinamele.com/2014/05/customize-colors-of-your-terminal-in-mac-os-x.html – jamesnotjim Sep 02 '18 at 12:34
  • 2
    I use a Mac M1 pro with macOS Monterey and the file who I need to edit is ~/.zprofile – Lucas Bodin Jun 02 '22 at 08:42
135

You can use the Linux based syntax in one of your startup scripts. Just tested this on an OS X Mountain Lion box.

eg. in your ~/.bash_profile

export TERM="xterm-color" 
export PS1='\[\e[0;33m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[0;34m\]\w\[\e[0m\]\$ '

This gives you a nice colored prompt. To add the colored ls output, you can add alias ls="ls -G".

To test, just run a source ~/.bash_profile to update your current terminal.

Side note about the colors: The colors are preceded by an escape sequence \e and defined by a color value, composed of [style;color+m] and wrapped in an escaped [] sequence. eg.

  • red = \[\e[0;31m\]
  • bold red (style 1) = \[\e[1;31m\]
  • clear coloring = \[\e[0m\]

I always add a slightly modified color-scheme in the root's .bash_profile to make the username red, so I always see clearly if I'm logged in as root (handy to avoid mistakes if I have many terminal windows open).

In /root/.bash_profile:

PS1='\[\e[0;31m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[0;34m\]\w\[\e[0m\]\$ '

For all my SSH accounts online I make sure to put the hostname in red, to distinguish if I'm in a local or remote terminal. Just edit the .bash_profile file in your home dir on the server.. If there is no .bash_profile file on the server, you can create it and it should be sourced upon login.

If this is not working as expected for you, please read some of the comments below since I'm not using MacOS very often..

If you want to do this on a remote server, check if the ~/.bash_profile file exists. If not, simply create it and it should be automatically sourced upon your next login.

4levels
  • 3,134
  • 1
  • 23
  • 22
  • 2
    how do you edit bash_profile for ssh accounts online? – Andrew Apr 08 '13 at 20:52
  • 1
    I like `PS1` better with `$PWD` instead of `\u`, as such: `PS1=\[\e[0;31m\]$PWD\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[0;34m\]\w\[\e[0m\]\$ ` – cregox Aug 21 '13 at 17:39
  • 1
    Here's some more explanation of the codes for the prompt: https://wiki.archlinux.org/index.php/Color_Bash_Prompt#Step_by_step. – JohnK Sep 10 '13 at 14:22
  • 1
    These links might also be helpful: [ANSI color code escape sequences](http://ascii-table.com/ansi-escape-sequences.php). [More on PS1 codes](http://bitmote.com/index.php?post/2012/11/19/Using-ANSI-Color-Codes-to-Colorize-Your-Bash-Prompt-on-Linux). – JohnK Sep 10 '13 at 14:48
  • @Cawas `$PWD` will not work as intended - it will indeed be replaced with current directory, but when the variable is assigned - that is, the value that is the current directory will go into the value of the prompt and from then on changing directories will have no effect on the directory contained in the prompt. This has to do with basic shell variable assignment and evaluation. – Armen Michaeli Sep 25 '13 at 16:20
  • Really like your red color root prompt suggestion :) Thanks! – kpsfoo Nov 19 '13 at 20:25
  • works in Yosemite perfectly! – JohnnyHuo Jun 22 '15 at 17:44
  • 1
    I was having issues getting PROMPT_COMMAND colors to display correctly in El Capitan (OS X 10.11) using these color codes. Turns out that when used in `PROMPT_COMMAND`, you need to use the `\033` syntax instead of `\e`. Example to add a colored timestamp before your prompt (include this in your `~/.bashrc` or `~/.bash_profile`): `PROMPT_COMMAND="${PROMPT_COMMAND}; echo; echo -e '\033[0;35m'\[\$(date +%F\ %T)\]'\033[m'"` – user5359531 Dec 29 '15 at 20:14
  • 1
    bold red (style 1) = \[\e[1;31m\] , its 31 not 3m. Thanks – JavaDeveloper Nov 28 '16 at 20:08
46

MartinVonMartinsgrün and 4Levels methods confirmed work great on Mac OS X Mountain Lion.

The file I needed to update was ~/.profile.

However, I couldn't leave this question without recommending my favorite application, iTerm 2.

iTerm 2 lets you load global color schemes from a file. Really easy to experiment and try a bunch of color schemes.

Here's a screenshot of the iTerm 2 window and the color preferences. iTerm2 Color Preferences Screenshot Mac

Once I added the following to my ~/.profile file iTerm 2 was able to override the colors.

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Here is a great repository with some nice presets:

iTerm2 Color Schemes on Github by mbadolato

Bonus: Choose "Show/hide iTerm2 with a system-wide hotkey" and bind the key with BetterTouchTool for an instant hide/show the terminal with a mouse gesture.

Nick Woodhams
  • 11,977
  • 10
  • 50
  • 52
19

If you want to have your ls colorized you have to edit your ~/.bash_profile file and add the following line (if not already written) :

source .bashrc

Then you edit or create ~/.bashrc file and write an alias to the ls command :

alias ls="ls -G"

Now you have to type source .bashrc in a terminal if already launched, or simply open a new terminal.

If you want more options in your ls juste read the manual ( man ls ). Options are not exactly the same as in a GNU/Linux system.

Marcio
  • 1,979
  • 15
  • 25
  • 6
    A somewhat better option is to replace the 'alias' definition with 'export CLICOLOR=" "'. This has the advantage of usually continuing to work even if you switch shells during a terminal session (as long as environment variables are inherited - aliases aren't). – Ned Deily Oct 11 '09 at 17:32
7

If you are using tcsh, then edit your ~/.cshrc file to include the lines:

setenv CLICOLOR 1
setenv LSCOLORS dxfxcxdxbxegedabagacad

Where, like Martin says, LSCOLORS specifies the color scheme you want to use.

To generate the LSCOLORS you want to use, checkout this site

miken32
  • 42,008
  • 16
  • 111
  • 154
4

Check what $TERM gives: mine is xterm-color and ls -alG then does colorised output.

William Rose
  • 971
  • 1
  • 6
  • 13
  • well, on capitan with `xterm-256color`, I get no colours. `CLICOLOR` and `LSCOLORS` don't change that ): – drevicko Jul 18 '16 at 09:41
2

When I worked on Mac OS X in the lab I was able to get the terminal colors from using Terminal (rather than X11) and then editing the profile (from the Mac menu bar). The interface is a bit odd on the colors, but you have to set the modified theme as default.

Further settings worked by editing .bashrc.

Cajunluke
  • 3,103
  • 28
  • 28
ccook
  • 5,869
  • 6
  • 56
  • 81