23

I'm trying to get my iTerm prompt set up the same way as Paul Irish

So far I have the following in ~/.profile:

# Add git branch name to prompt
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*\(.*\)/ on \1/'
}

PS1='\n\[\033[0:35m\]\u\[\033[0;32m\]\w\[033[0m\]$(parse_git_branch)\n\$\[\033[0m\] '

I don't know how to make just the branch appear in a different colour and not the preceding "on"

As well as this there are other features such as:

  • Displaying an "o" at the prompt when not in a git branch
  • Displaying a "±" when in a branch
  • Displaying the date at the end of the line

Any help would be appreciated

1

6 Answers 6

23

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.

3
  • 1
    Thanks, this is by far the easiest way to go about it and should be higher up.
    – Casper
    Jun 28, 2013 at 14:22
  • Had some troubles using the other solutions, this is indeed the easiest one. Thanks Jan 23, 2019 at 9:51
  • i also had the issue where the prompt would not change when i changed branches within a repo, this fixed it. also, incredibly easy to setup/configure. thank you for sharing. bill
    – bill
    Apr 15, 2019 at 18:58
9

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 Prompt
TITLEBAR='\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.

8

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 running
alias hb='sudo ps -aef | grep HandBrakeCLI'

# alias for quick DNS cache flushing
alias fc='sudo dscacheutil -flushcache'

# enable the git bash completion commands
source ~/.git-completion

# enable git unstaged indicators - set to a non-empty value
GIT_PS1_SHOWDIRTYSTATE="."

# enable showing of untracked files - set to a non-empty value
GIT_PS1_SHOWUNTRACKEDFILES="."

# enable stash checking - set to a non-empty value
GIT_PS1_SHOWSTASHSTATE="."

# enable showing of HEAD vs its upstream
GIT_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 line
function 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 position

PS1='${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} '
5
  • I'm new to Stack Overflow so just wondering ... is it protocol to include code in a response instead of a link? I was hoping people would read the article instead of getting the code here as there are a couple of things you need to do before the code above will work.
    – user743873
    May 11, 2011 at 3:30
  • 3
    @SiegeX care to mention the original source?
    – pablasso
    Sep 3, 2012 at 18:08
  • 3
    @pablasso yea, me :) Looking back at this I have overreacted a bit. Still would have been nice to get a mention that the color codes were from me but the whole plagiarism is over the top and I take that back.
    – SiegeX
    Sep 4, 2012 at 16:52
  • 1
    I had line wrapping issues with iTerm2 and the date at the end of the line so have shuffled it to after the directory/branch info codePS1='${MAGENTA}\u${WHITE} in ${GREEN}\w${WHITE}${MAGENTA}__git_ps1 " on %s"${WHITE} (date "+%a, %b %d")\r\nset_prefix${NORMAL}${CYAN}\033[s\033[60C \033[u${WHITE} 'code
    – Simon
    Feb 5, 2013 at 11:30
  • Here's Paul Irish's actual configuration: github.com/paulirish/dotfiles/blob/master/.bash_prompt It is sourced from his .bash_profile: github.com/paulirish/dotfiles/blob/master/.bash_profile Apr 8, 2014 at 14:00
7

Add this to your ~/.bashrc or ~/.profile

PS1="\u@\h:\w on\e[0;35m$(__git_ps1)\e[m\$ "

Where,

$(__git_ps1) is used for printing the branch name

\e defines the start of the color scheme

[0;35m represent the purple color

\e[m defines the end of the scheme

Also, I fixed your current prompt:

PS1='\n\[\033[0;35m\]\u\[\033[0;32m\]\w\[\033[0m\]$(__git_ps1)\n\$\[\033[0m\] '
3
  • I recommend reading this page for coloring the prompt, since it can be applied to Mac too: cyberciti.biz/faq/… Mar 31, 2011 at 20:02
  • If you really like colors: PS1="\[\033[01;32m\]\u\[\033[36m\]@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\e[1;35m$(__git_ps1)\e[m \$ " Mar 31, 2011 at 20:07
  • I'm pretty sure you should be using single quotes here, not double quotes.
    – Flimm
    Oct 7, 2016 at 12:57
6

As mentioned above, I also use git-aware-prompt.

Run this to quickly install:

mkdir ~/.bash
cd ~/.bash
git clone git://github.com/jimeh/git-aware-prompt.git

Add this to the top of your ~/.bash_profile:

export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"

In the same file ~/.bash_profile here is the prompt I use:

export PS1="\n\[$txtpur\]\u\[$bldwht\]@\h\[$bldgrn\]:\[$bldblu\] \w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ \[$txtwht\] "

export SUDO_PS1="\[$bakred\]\u@\h\[$txtrst\] \w\$ "

you can change the colors to your liking

Here is what some of the symbols in PS1 mean:
\u - username
@ - cool symbol
\h - hostname
: - cool symbol to separate things
\w - full path, use \W for short path
\git_branch - name of current branch
\git_dirty - show * when there is a change in branch
$ - cool symbol to signify, enter command

1
  • This is the best answer. Jul 16, 2022 at 9:53
0

A very feature rich and broad solution (not only for iterm shell but also for Vim and others) is Powerline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.