Nullcreations.net | Ramblings & Photography of Jerrett Taylor

ZSH Prompt to show git branch

February 15th, 2008 | 1 comment

After using git for a little bit I started tinkering with ZSH, to get the current branch in my prompt.

What I ended up with is this:

git-zsh

Turns out it's pretty easy - somewhere in your .zshrc you need to put the following:

1
2
3
4
5
6
7
8
9
10
11
# get the name of the branch we are on
git_prompt_info() {
  ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
  echo "(${ref#refs/heads/})"
}

autoload -U colors
colors
setopt prompt_subst

PROMPT='%{$fg_bold[green]%}%n@%m %{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)%{$fg[blue]%} %% %{$reset_color%}'

The git_prompt_info function just asks git what branch we are in (and returns if we're not in a git repo), and then strips out regs/heads from the output. The colors bit is just so you can use colors like "green" instead of this awesomeness. prompt_subst is needed to tell zsh we want the git_prompt_info substituted with it's results, otherwise it will just show it as a string.

Thats pretty much it!

I initially had a right hand prompt for the branch, which while looks nicer, makes it less likely to be seen... My original prompt looked like this:

1
2
PROMPT='%{$fg_bold[green]%}%m %{$fg[blue]%}%c %% %{$reset_color%}'
RPROMPT='%{$fg_bold[yellow]%}$(git_prompt_info)%{$reset_color%}'

1 Comment

Posted by Roy van der Meij about 1 month ago.

Nice one!
I think I'll implement this later this day.
I still have to move all projects to git though.

Musings

04.14.08

Trying to decide where to travel to? Wikitravel.org Random Page has the answers!

02.23.08

sd's embedded_actions has a new home on github

02.18.08

Very cool.

02.17.08

Open Source Food? - looks like a nice recipe site!

02.15.08

For anyone using capistrano with more than one target for deployment (staging, production), check out cap multistage

02.15.08

Wow, I just realized how bad my code blocks break my blog in safari. I should fix that!

01.27.08

Mike has created another nice netbeans theme - go check it out, if you use netbeans!

01.21.08

A handy tool, Rubular is a web based regex editor

01.15.08

Dan put together a sweet code editor plugin for mce with syntax highlighting etc.

12.13.07

On OSX? Add RubyImporter to Spotlight and search your source!

12.04.07

sd has released a new version of embedded_actions

11.27.07

RejectConf videos are online thanks to confreaks!

11.19.07

Good news - we upgraded a project at work to rails 2.0 rc2 and the speed to run specs dropped from 30-35 seconds to 10-15 seconds!

11.08.07

Eigenclass.org has a great big list of Ruby 1.9 changes that is being kept up to date!

11.06.07

Rein is working on a trac replacement in ruby called eskort , and it looks cool. More info on his goals