Gravatar

Geoff Evason

Posts Tagged ‘.bash_profile’

My .bash_profile for git & Rails

Tuesday, August 19th, 2008

I’ve converted all of my projects from svn to git.  If you haven’t done so already, and can do it, I would recommend not waiting anymore.  It does make things better.  

I used to really like the visual representations available on svn (such as tortoise svn) and rarely used the command line for repository related things.  The way git works (in particular, not having to have a special ’svn folder within each folder) makes managing files way easier.  The best way to learn is to check out the git peepcode. I borrowed some of the aliases below from the screencast.

Now that I’m on the command line more, I’ve created a .bash_profile to make my life easier.  It is full of nice goodies that I collected.  I figured that I’d share it with the world in case others find it helpful.  It includes:

  • Updating the OSX Leopard terminal title bar (and potentially the tabs) to include the working directory
  • Updating the command prompt to include the current directory and the current git branch (if you’re in a git repo)
  • Lots of aliases to save myself keystrokes.

# Get the name of the current git branc
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

# Update the command prompt to be <user>:<current_directory>(git_branch) >
# Note that the git branch is given a special color
function set_my_prompt {
  PS1="\u:\w\e[1;34m\$(parse_git_branch)\e[m > "
}

# Update the title for the terminal window to be the full working dir
function set_term_title
{
    local title="$1"
    if [[ -z "$title" ]]; then
        title="root"
    fi

    local tmpdir=~/Library/Caches/${FUNCNAME}_temp
    local cmdfile="$tmpdir/$title"

    # Set window title
    #echo -n -e "\e]0;${title}\a"
    echo -n -e "\e]0;${PWD#*/}\a"

   # Set tab title
   # This works by creating a process with the name of the working dir.  
   # So, the tab name doesn't stick if you start running a mongrel server :-(
    if [[ -n ${CURRENT_TAB_TITLE_PID:+1} ]]; then
        kill $CURRENT_TAB_TITLE_PID
    fi
    mkdir -p $tmpdir
    ln /bin/sleep "$cmdfile"
    "$cmdfile" 10 &
    CURRENT_TAB_TITLE_PID=$(jobs -x echo %+)
    disown %+
    kill -STOP $CURRENT_TAB_TITLE_PID
    command rm -f "$cmdfile"
}

set_my_prompt
PROMPT_COMMAND='set_term_title "${PWD##*/}"'

# Some aliases I find useful
alias gclb="git checkout -b"
alias gb="git branch"
alias gba="git branch -a"
alias gs="git status"
alias gca="git commit -a"
alias gcm="git commit -m"
alias gk="gitk --all &"
alias ss="script/server"
alias ssp="script/server -p"
alias sr="script/runner"

Big credit goes Christopher Stawarz to the the tab & title thing: l