Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Modify your terminal shell prompt' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Modify your terminal shell prompt
Authored by: agitatedString on Oct 30, '11 02:11:20AM

Is there anything uglier and harder to read and more tedious to modify than ANSI color codes 5 minutes after you have written them?

Is there anything more annoying than ANSI color code spewage in terminals that do not accept them?

Below is what I have come to prefer over raw ANSI color codes. This code (below) has been scraped off the web here and there with miscellaneous changes made that I have long since forgotten. As you can see below the PS1 variable value now almost reads like an English sentence.

I keep the code below near the top of my ~/.bashrc file

START ~/.bashrc code:

#### Smooth the way for DTerm and possibly other terminals that barf on tput colors:
INTERACTIVETERM=-YES-
if [ "$TERM" == "" ]; then INTERACTIVETERM="-NO-"; TERM="vt100"; fi
if [ "$TERM" == "dumb" ]; then INTERACTIVETERM="-NO-"; TERM="vt100"; fi
export INTERACTIVETERM
# Set up TPUT color codes
if [ "$INTERACTIVETERM" == "-YES-" ]; then
    tReset="$(tput sgr0)"
    NORMAL=$(tput sgr0)
    tBold="$(tput bold)"
    BOLD=$(tput bold)
    BRIGHT=$(tput bold)
    tBlack="$(tput setaf 0)"
    BLACK=$(tput setaf 0)
    tRed="$(tput setaf 1)"
    RED=$(tput setaf 1)
    tGreen="$(tput setaf 2)"
    GREEN=$(tput setaf 2)
    tYellow="$(tput setaf 3)"
    YELLOW=$(tput setaf 3)
    pYELLOW="$(tput setaf 190)"
    LIME_YELLOW=$(tput setaf 190)
    tBlue="$(tput setaf 4)"
    BLUE=$(tput setaf 4)
    POWDER_BLUE=$(tput setaf 153)
    tPink="$(tput setaf 5)"
    MAGENTA=$(tput setaf 5)
    tCyan="$(tput setaf 6)"
    CYAN=$(tput setaf 6)
    tGray="$(tput setaf 7)"
    WHITE=$(tput setaf 7)
    tWhite="$(tput setaf 8)"
    TUNON="$(tput smul)"
    TUNOFF="$(tput rmul)"
    BLINK=$(tput blink)
    REVERSE=$(tput smso)
    UNDERLINE=$(tput smul)
    tRandColor=$(tput setaf $(random /0..8/))
    #tRandColor="$(tput setaf $(( $(hostname | openssl sha1 | sed 's/.*\([0-9]\).*/\1/') % 6 + 1 )) )"
else
    tReset=
    tBold=
    tBlack=
    tRed=
    tGreen=
    tYellow=
    tBlue=
    tPink=
    tCyan=
    tGray=
    tWhite=
    tUndOn=
    tUndOff=
    tRandColor=
    BLACK=
    RED=
    GREEN=
    YELLOW=
    BLUE=
    MAGENTA=
    CYAN=
    WHITE=
    LIME_YELLOW=
    POWDER_BLUE=
    BRIGHT=
    NORMAL=
    BLINK=
    REVERSE=
    UNDERLINE=
    BOLD=
fi

export PS1='\[${tRed}\][\!]\[${tBlue}\]\W\[${tReset}${tGreen}\]$(__git_ps1 "[%s]")${tReset}⇶ ' # using this with git-completion.bash

END ~/.bashrc code.

Things like, ${tBold}${tGreen} blah blah blah ${tReset}, work well in here-documents as well. Colors become much easier to use in your bash scripts too:

printf "%s/%s : ${tRed}%s${tReset}\n" "$PWD" "$meat" $(mdls -name kMDItemKind -raw "$meat");



[ Reply to This | # ]