Merikanto

一簫一劍平生意,負盡狂名十五年

Customize Mac's Terminal

The standard Mac’s terminal appearance is just some boring black texts on a white background, like the one shown below:

Normal Terminal

Even though Apple includes a few nice themes, but to really makes your terminal unique, certain customization is needed. In this post we’re going to talk about some tips for customization.

Reference: Shell Color Codes


First Step

We first go to Terminal -> Preferences, and choose Pro under the Profiles tab. This will render a half-transparent black background. After that, we can adjust the ANSI colors as marked below:

ANSI colors

So now we have finished out first step. The purpose of adjusting ANSI colors will be obvious as we proceed.

Bash Profile

Now we type the commands below in Terminal to get .bash_profile:

1
2
cd ~ 
open .bash_profile

That will open the bash profile in TextEdit. Now we add these two lines:

1
2
3
export CLICOLOR=1

export LSCOLORS=gxfxaxdxcxegedabagacad

Then save and quit. After we restart terminal, we could see that the folders are in light blue.

In the second line, the bunch of charaters after the “=” sign means the color assignment. As we can see, there’re 22 characters, i.e. 11 pairs (for 11 types of files). The first character in each pair represents the foreground color, while the second represents the background color. Below is the listing:


Char Color
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

Terminal Prompt

Our last step is to customize the Terminal prompt. To edit your Mac’s name, we go to System Preferences -> Sharing. Then we open .bash_profile, and add these two lines:

1
2
3
export TERM="xterm-color"

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

And now our customization is done. Let us first use a table to see what the second line means:

Char Meaning
\u Current username
\! History command’s number
\h Host
\# Current command’s number
\$ Prompt (root is #)
\d Date
\t Time
\s Shell’s name
\w Current dir.’s path
\W Current dir’s name
\nnn nnn in octal

And the ANSI color codes:

Forground Background Color
30 40 Black
31 41 Red
32 42 Green
33 43 Yellow
34 44 Blue
35 45 Pink
36 46 Skyblue
37 47 White

And the numbers before the ;:

Char Meaning
0 off
1 Highlight
4 Underline
5 Blink
8 Invisible

Now let’s look at the second line again. Basically it redefines the color of the terminal prompt. I mentioned adjusting the ANSI colors in Terminal’s preference settings, since changing that will change the appearance of the colors. The effect is shown below:

Effects