Saturday, July 23, 2011

Mac Terminal Colors & Git Prompt

I always get slightly annoyed by the default color settings in terminals. Fedora had issues with this for a long time. When I got a Mac, I was rather disappointed to see that with a black background the standard color set was hard to read. Anyway, I dug into setting bash terminal colors and thought I'd share.


To colorize your terminal you need these settings in your shell. I put them in my .bash_profile:
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

The CLICOLOR flag turns on color, and the LSCOLORS picks the colors for various types of objects in your directory structure. LSCOLORS is slightly different from how it works on linux, as a Mac follows the BSD way. There are 11 color settings concatenated together here, where each is defined by a pair of characters. The first character is text color, and the second is background. Capital is bold or enhanced for backgrounds. The "x" character means default. The color map is a=black, b=red, c=green, d=yellow, e=blue, f=purple, g=cyan, h=white.

The 11 settings, in order, are: directory, symlink, socket, pipe, executeable, block special, character special, executable with setuid, executable with setgid, directory writeable to others with sticky bit, directory writeable to others without sticky bit.

To control colorization of your bash prompt, we'll set the PS1 environment variable. This and other shell output don't use the same color controls as above, but instead use colorizing escape sequences. For example, \033[0;36m is cyan. These aren't very easy to remember, so I create a .colors file (see below) that I source in .bashrc to give nice names to all the colors, so I can say ${cyan} instead of \033[0;36m .

Next I set an alias that will display the colors and give me examples:
alias colors='{
  echo -e -n "${black}black ${Black}Black ${on_white}${BLACK}BLACK$off "
  echo -e -n "${red}red ${Red}Red ${on_yellow}${RED}RED$off "
  echo -e -n "${green}green ${Green}Green ${on_blue}${GREEN}GREEN$off "
  echo -e -n "${yellow}yellow ${Yellow}Yellow ${on_red}${YELLOW}YELLOW$off "
  echo -e -n "${blue}blue ${Blue}Blue ${on_green}${BLUE}BLUE$off "
  echo -e -n "${purple}purple ${Purple}Purple ${on_cyan}${PURPLE}PURPLE$off "
  echo -e -n "${cyan}cyan ${Cyan}Cyan ${on_blue}${CYAN}CYAN$off "
  echo -e -n "${white}white ${White}White ${on_purple}${WHITE}WHITE$off \n"
}'

On my black background, running this looks something like
btaylor@vancouver ~ $ colors
black Black BLACK red Red RED green Green GREEN yellow Yellow YELLOW blue Blue BLUE purple Purple PURPLE cyan Cyan CYAN white White WHITE

You can colorize any output you want. Here's what I do in .bashrc to colorize my bash prompt with git branch awareness. It's a good idea to use big red "PRODUCTION" on production systems.

source ~/.colors
function color_my_prompt {
    local user_and_host="\[${Yellow}\]\u@\h"
    local current_location="\[${Cyan}\]\w"
    local git_branch_color="\[${Red}\]"
    local git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
    local prompt_tail="\[${Purple}\]$"
    local last_color="\[${off}\]"
    export PS1="$user_and_host $current_location $git_branch_color$git_branch$prompt_tail$last_color "
}
color_my_prompt

This will show the git branch in red when I'm in a git repository:
btaylor@vancouver ~/src/cookbook (master) $ 

A couple references that I've adapted to make this: this stackoverflow.com post, and an arch linux wiki entry. Here's my .colors file:

btaylor@vancouver ~ $ cat .colors
# Reset
off='\033[0m'       # Text Reset

# Regular Colors
black='\033[0;30m'        # Black
red='\033[0;31m'          # Red
green='\033[0;32m'        # Green
yellow='\033[0;33m'       # Yellow
blue='\033[0;34m'         # Blue
purple='\033[0;35m'       # Purple
cyan='\033[0;36m'         # Cyan
white='\033[0;37m'        # White

# Bold
Black='\033[1;30m'       # Black
Red='\033[1;31m'         # Red
Green='\033[1;32m'       # Green
Yellow='\033[1;33m'      # Yellow
Blue='\033[1;34m'        # Blue
Purple='\033[1;35m'      # Purple
Cyan='\033[1;36m'        # Cyan
White='\033[1;37m'       # White

# Underline
_black_='\033[4;30m'       # Black
_red_='\033[4;31m'         # Red
_green_='\033[4;32m'       # Green
_yellow_='\033[4;33m'      # Yellow
_blue_='\033[4;34m'        # Blue
_purple_='\033[4;35m'      # Purple
_cyan_='\033[4;36m'        # Cyan
_white_='\033[4;37m'       # White

# Background
on_black='\033[0;40m'       # Black
on_red='\033[0;41m'         # Red
on_green='\033[0;42m'       # Green
on_yellow='\033[0;43m'      # Yellow
on_blue='\033[0;44m'        # Blue
on_purple='\033[0;45m'      # Purple
on_cyan='\033[0;46m'        # Cyan
on_white='\033[0;47m'       # White

# High Intensty
bLACK='\033[0;90m'       # Black
rED='\033[0;91m'         # Red
gREEN='\033[0;92m'       # Green
yELLOW='\033[0;93m'      # Yellow
bLUE='\033[0;94m'        # Blue
pURPLE='\033[0;95m'      # Purple
cYAN='\033[0;96m'        # Cyan
wHITE='\033[0;97m'       # White

# Bold High Intensty
BLACK='\033[1;90m'      # Black
RED='\033[1;91m'        # Red
GREEN='\033[1;92m'      # Green
YELLOW='\033[1;93m'     # Yellow
BLUE='\033[1;94m'       # Blue
PURPLE='\033[1;95m'     # Purple
CYAN='\033[1;96m'       # Cyan
WHITE='\033[1;97m'      # White

# High Intensty backgrounds
on_BLACK='\033[0;100m'   # Black
on_RED='\033[0;101m'     # Red
on_GREEN='\033[0;102m'   # Green
on_YELLOW='\033[0;103m'  # Yellow
on_BLUE='\033[0;104m'    # Blue
on_PURPLE='\033[10;95m'  # Purple
on_CYAN='\033[0;106m'    # Cyan
on_WHITE='\033[0;107m'   # White


4 comments:

  1. An amazing web journal I visit this blog, it's unbelievably wonderful. Oddly, in this blog's content made without a doubt and reasonable. The substance of data is informative.
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  2. Thank you for sharing
    Yaaron Studios is one of the rapidly growing editing studios in Hyderabad. We are the best Video Editing services in Hyderabad. We provides best graphic works like logo reveals, corporate presentation Etc. And also we gives the best Outdoor/Indoor shoots and Ad Making services.
    Best video editing services in Hyderabad,ameerpet
    Best Graphic Designing services in Hyderabad,ameerpet­
    Best Ad Making services in Hyderabad,ameerpet­

    ReplyDelete