Skip to content

[MacOS] How To Beautify The Default Terminal

Last Updated on 2021-09-24 by Clay

After getting used to the Linux terminal with rich colors (I used to spend a lot of time adjusting it), I suddenly switched to the white background Mac default terminal: Terminal.app, and I felt uncomfortable.

There is a sense of collapse of "Oh my God I need to configure it again..."

For a simple comparison, I have beautiful colors and file sorting in my remote Linux server:

And my MacBook's Terminal.app shown:

And I can't even see the text

We need to do something to make Mac OS default terminal more beautiful. I share my configuration method in below, maybe one day I can come back to refer my own configuration.

The following is divided into several parts.

  • background color, font type
  • terminal PS1
  • the terminal display text color via ls command

Background color and font type

First we can use graphical interface to configure. Open your terminal and select the top option "Terminal".

Or you can use command + , to open the setting window.

In here you can select the default terminal, text, color, ANSI basic color, terminal cursor color and shape... etc.

After the setting is complete, you should have the terminal appearance you wish to have:

(Note: The default interactive shell is now zsh ... because I change the default scripting language from zsh to bash. More info: [MacOS] Change default shell script zsh to bash)


PS1

In Mac OS, the user name in terminal have no any color, but we can set PS1 to fix it, even add the current directory path.

If there is no .bash_profile file in your user directory, you can create it by yourself. (Different scripting languages have different configuration files.)

For example I can use vim editor:

vim ~/.bash_profile

And set PS1:

export PS1="My name is Clay: "

Save and exit, use source command to active the ~/.bash_profile settings.

source ~/.bash_profile

You will see the following screen:

We can enter any command, you will find the only thing that changes is the user name.

So how to change the color and add the current path? We need to know some color code.

There are several kinds of colors that we can set in the terminal:

  • Effects
  • Text color
  • Background color

To specify text in the terminal, you need to use the following syntax:

\[\e[EFFECTS;TEXT_COLOR;BACKGROUND_COLOR\]TEXT\[\e[m\]

Regardless of effects, text colors and background colors they have their own codes.


Effect Code

effect_color_code


Text Color Code

front_color_code


Background Color Code

back_color_code


Symbols to get different information

In addition to colors, there are also symbols that automatically obtain formats such as date and time that can be used:

SymbolsPurposes
\d日期
\h主機名稱
\n換行
\t時間
\u使用者名稱
\W當前工作目錄
\w完整當前工作目錄

For example, you can use \u to get the user name, and use \w to get the current work path.

Let's modify the ~/.bash_profile again.

export PS1="\[\e[1;32m\]\u@MacBook-Pro\[\e[m\]:\[\e[1;34m\]\w\[\e[m\]$ "

Save and exit, and use source command.

source ~/.bash_profile

Output:

It's more beautiful.


ls Command To Display Different File Colors (256 Colors)

Up to now, the user name in the terminal appears to be colored, however, you will find that the folders and files are still white.

so the next step is to adjust the file color displayed by ls command!

First we need to install coreutils tool to configure different colors of file. (If you need to enable Homebrew, you can refer: [MacOS] Use "brew" Command To Install Package On Terminal)

The downloaded ~/.dircolors is a set of color settings that I recommend. If there are colors you want to modify, you can also edit the configuration file.

brew install coreutils
wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O ~/.dircolors

After downloading, edit ~/.bash_profile again:

# GDIRCOLORS
if brew list --formula | grep coreutils > /dev/null ; then
  PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
  alias ls="ls --show-control-chars --color=auto --sort=extension"
  eval `gdircolors -b $HOME/.dircolors`
fi



Save and exit and use the following command:

source ~/.bash_profile

Now if you use ls command, you will see the colorful result:

If you have any hate color of file, you can change it:

vim ~/.dircolors

Output:

We take the folder DIR as an example

After the modification is blue, save and exit. Use source ~/.bash_profile to make the color setting take effect, and check the color again.


Solve ~/.bash_profile Slow Loading Time (2020/11/21 Update)

Maybe you have a new problem: you set the ~/.bash_profile, but the terminal open time become too long.

After testing, I find the problem is the following setting:

# GDIRCOLORS
if brew list --formula | grep coreutils > /dev/null ; then
  PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
  alias ls="ls --show-control-chars --color=auto --sort=extension"
  eval `gdircolors -b $HOME/.dircolors`
fi



We can modify the command to load it faster.

First, check the display result of brew --prefix coreutils command.

brew --prefix coreutils

Output:

/usr/local/opt/coreutils

In my device, the above path is displayed. Of course, you may be different from mine.

Replace this path with $(brew --prefix coreutils) in ~/.bash_profile, and delete the command to determine whether the coreutils tool exists. The final configuration is as follows:

# GDIRCOLORS
PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
alias ls="ls --show-control-chars --color=auto --sort=extension"
eval `gdircolors -b $HOME/.dircolors`



In this way, the speed of opening the terminal will be very fast.


References


Read More

Leave a Reply