How do I get my iTerm prompt to display differently when I'm in a Git branch? How do I get my iTerm prompt to display differently when I'm in a Git branch? bash bash

How do I get my iTerm prompt to display differently when I'm in a Git branch?


I use git-aware-prompt.

A lot of solutions I had before only displayed the git branch if I were only in that directory when the terminal loaded. If I started iTerm in a non-git repo, then it wouldn't work when I cd into directory with a git repo.

This github project solved that for me.


Rather than using archaic terminal codes, use tput instead which makes the code much easier to read and a lot harder to mess up:

BLACK=$(tput setaf 0)RED=$(tput setaf 1)GREEN=$(tput setaf 2)YELLOW=$(tput setaf 3)LIME_YELLOW=$(tput setaf 190)POWDER_BLUE=$(tput setaf 153)BLUE=$(tput setaf 4)MAGENTA=$(tput setaf 5)CYAN=$(tput setaf 6)WHITE=$(tput setaf 7)BRIGHT=$(tput bold)NORMAL=$(tput sgr0)BLINK=$(tput blink)REVERSE=$(tput smso)UNDERLINE=$(tput smul)# Set Titlebar and PromptTITLEBAR='\e]0;\h: ${PWD/$HOME/~}\a'PS1="${TITLEBAR}${WHITE}[${POWDER_BLUE}\u@\h${WHITE}]${NORMAL}$ "

Setting the titlebar is optional. Just be sure to use ${NORMAL} at the end to turn off the color change.


I've just written a post about how to do all this. I've covered all the basics but had to guess a couple of things, e.g. how Paul uses the symbols etc. If you want to read it, check out http://digitalformula.net/articles/pimp-my-prompt-like-paul-irish.

There's also an article on digitalformula.net that shows a couple of other prompt examples - see http://digitalformula.net/articles/a-couple-more-bash-prompt-examples.

EDITED: The code part is as follows:

PATH=$PATH:~/Data/Scripts:~/Data/Utils/rar:~/_Applications:~/_Applications/lynx# alias to quickly show if any Handbrake processes are runningalias hb='sudo ps -aef | grep HandBrakeCLI'# alias for quick DNS cache flushingalias fc='sudo dscacheutil -flushcache'# enable the git bash completion commandssource ~/.git-completion# enable git unstaged indicators - set to a non-empty valueGIT_PS1_SHOWDIRTYSTATE="."# enable showing of untracked files - set to a non-empty valueGIT_PS1_SHOWUNTRACKEDFILES="."# enable stash checking - set to a non-empty valueGIT_PS1_SHOWSTASHSTATE="."# enable showing of HEAD vs its upstreamGIT_PS1_SHOWUPSTREAM="auto"BLACK=$(tput setaf 0)RED=$(tput setaf 1)GREEN=$(tput setaf 2)YELLOW=$(tput setaf 3)LIME_YELLOW=$(tput setaf 190)POWDER_BLUE=$(tput setaf 153)BLUE=$(tput setaf 4)MAGENTA=$(tput setaf 5)CYAN=$(tput setaf 6)WHITE=$(tput setaf 7)BRIGHT=$(tput bold)NORMAL=$(tput sgr0)BLINK=$(tput blink)REVERSE=$(tput smso)UNDERLINE=$(tput smul)# set the prompt to show current working directory and git branch name, if it exists# this prompt is a green username, black @ symbol, cyan host, magenta current working directory and white git branch (only shows if you're in a git branch)# unstaged and untracked symbols are shown, too (see above)# this prompt uses the short colour codes defined above# PS1='${GREEN}\u${BLACK}@${CYAN}\h:${MAGENTA}\w${WHITE}`__git_ps1 " (%s)"`\$ '# this is a cyan username, @ symbol and host, magenta current working directory and white git branch# it uses the shorter , but visibly more complex, codes for text colours (shorter because the colour code definitions aren't needed)# PS1='\[\033[0;36m\]\u@\h\[\033[01m\]:\[\033[0;35m\]\w\[\033[00m\]\[\033[1;30m\]\[\033[0;37m\]`__git_ps1 " (%s)"`\[\033[00m\]\[\033[0;37m\]\$ '# return the prompt prefix for the second linefunction set_prefix {    BRANCH=`__git_ps1`    if [[ -z $BRANCH ]]; then        echo "${NORMAL}o"    else        echo "${UNDERLINE}+"    fi}# and here's one similar to Paul Irish's famous prompt ... not sure if this is the way he does it, but it works  :)# \033[s = save cursor position# \033[u = restore cursor positionPS1='${MAGENTA}\u${WHITE} in ${GREEN}\w${WHITE}${MAGENTA}`__git_ps1 " on %s"`${WHITE}\r\n`set_prefix`${NORMAL}${CYAN}\033[s\033[60C (`date "+%a, %b %d"`)\033[u${WHITE} '