Colour Coding the Terminal (for mac and unix shells)
I originally needed a dark background and a light font for the command prompt.
But then I also needed to highlight my git branch name.
And to know the directory I’m in.
And see directories I can cd in…
And so, here I am typing out this post. For you, or me — when I need some new changes again.
First Know Your Shell
To make any of these changes you need to know what shell your computer uses.
For MacOS Mojave and previous MacOSes/linux terminal/git bash, the shell is Bash, ie Bourne Again SHell. For MacOS Catalina and newer OSes, your shell is Zsh, ie Z Shell.
Command Prompt with Colours, Your Computer’s Name, Your Present Directory, Git Branch Name
If your Shell is BASH
To make changes in the terminal, you need to edit the .bashrc or .bash_profile files in your root directory.
Open a terminal and go to your root directory like so
cd ~
then check whats in your .bashrc or .bash_profile (I use the bash profile) with
nano .bash_profile
or just open the file in a text editor to make the changes
if nothing shows up, you need to make that file first like this
touch .bash_profile
now edit this file (either with nano or file editor)
Copy and paste the following:
this will add git branch colours too, but you’ll probably need that soon anyways, →if you aren’t planning to use git, just delete the parse_git_branch(){…} and delete the parse_git_branch within the PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
#git clrs
export PS1="\u@\h \[\033[00;97m\]\w \[\e[00;36m\]\$(parse_git_branch)\[\e[96m\]$ "
Once the file is saved, you will need to quit terminal and reopen it.
If your Shell is ZSH
Turns out OS Catalina changed the shell to Zshell instead of bash. The syntax is different from the bash scripts too. The process will be the same though:
To make changes in the terminal, you need to edit the .zshrc or .zprofile files in your root directory.
Open a terminal and go to your root directory like so
cd ~
then check whats in your .zshrc or .zprofile (I use the .zshrc) with
nano .zshrc
or just open the file in a text editor to make the changes
if nothing shows up, you need to make that file first like this
touch .zshrc
now edit this file (either with nano or file editor)
Here’s an example to copy and paste to show your mac’s name and the present working directory in a cyan|white theme:
autoload -U colors && colors
export PS1="%F{214}%K{000}%m%F{015}%K{000}:%F{039}%K{000}%~%F{015}%K{000}\$ "
Aaand if you need Git branch names on there as well;
Here’s an example to copy and paste to show your mac’s name, the present working directory, and the branch name in a cyan|blue|purple theme:
#adding git branches
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}setopt PROMPT_SUBST
PROMPT='%F{123}%K{000}%m%F{015}%K{000}:%F{039}%K{000}%~%F{141}%}$(parse_git_branch)%{%F{none}%} $ '
Showing Directories in a Different Colour than Files
Eventually my directories got messy and I needed to make colour differences between folders and files. This is so when I list everything in a directory (ls
) I can easily see the folders.
If your Shell is BASH put this into your .bash_profile or .bashrc
If your Shell is ZSH put this into your .zshprofile or .zshrc
export CLICOLOR=1
export LSCOLORS=gxBxhxDxfxhxhxhxhxcxcx
alias ls='ls -Gp'
What this all means:
The export CLICOLOR=1
turns the colour choosing on
The export LSCOLORS=gxBxhxDxfxhxhxhxhxcxcx
is your personalized colour scheme.
NOTE: its called LSCOLORS
on MacOS, its called LS_COLORS
on linux.
Letters indicate the colour:
a = black
b = red
c = green
d = brown
e = blue
f = magenta
g = cyan
h = light gray
x = default
The position in the sequence indicate what we’re trying to colour. This is the sequence:
DIR
SYM_LINK
SOCKET
PIPE
EXE
BLOCK_SP
CHAR_SP
EXE_SUID
EXE_GUID
DIR_STICKY
DIR_WO_STICKY
The alias ls=’ls -Gp’
lets you type ‘ls
' and get colours added to the files and folders AND adds a backslash on folderNames/
And that should be all you need to happily type away :)
Additional Resources and References:
Other ways to do this — and for more customization (but it involves downloading ‘GNU coreutils’ and then changing that environment set up, etc.)
Learn about that from here: https://superuser.com/questions/468966/colouring-output-of-ls-according-to-file-extension/468969#468969
Or by getting it from a github repo here https://github.com/seebi/dircolors-solarized
and here: https://askubuntu.com/questions/466198/how-do-i-change-the-color-for-directories-with-ls-in-the-console
References : https://unix.stackexchange.com/questions/2897/clicolor-and-ls-colors-in-bash
https://apple.stackexchange.com/questions/33677/how-can-i-configure-mac-terminal-to-have-color-ls-output