Skip to content

Instantly share code, notes, and snippets.

@jstrachan
Last active August 29, 2015 14:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jstrachan/f960917b4861f30a8a45 to your computer and use it in GitHub Desktop.
Save jstrachan/f960917b4861f30a8a45 to your computer and use it in GitHub Desktop.
names your iTerm2 tab after the current working directory, shows the path in the prompt and the git branch
# thanks to this gist for the iTerm2 tab naming stuff: https://gist.github.com/phette23/5270658
# can't remember where I cribbed the rest of this from!
# hacked it a bit to work on OS X
_bold=$(tput bold)
_normal=$(tput sgr0)
__vcs_dir() {
local vcs base_dir sub_dir ref
sub_dir() {
local sub_dir
sub_dir=$(stat -f "${PWD}")
sub_dir=${sub_dir#$1}
echo ${sub_dir#/}
}
git_dir() {
#base_dir=$(git-rev-parse --show-cdup 2>/dev/null) || return 1
if [ -n "$base_dir" ]; then
base_dir=`cd $base_dir; pwd`
else
base_dir=$PWD
fi
#base_dir="$(basename "${base_dir}")"
vcs="git"
ref=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'`
#ref=$(git-symbolic-ref -q HEAD || git-name-rev --name-only HEAD 2>/dev/null)
#ref=${ref#refs/heads/}
#sub_dir=$(git-rev-parse --show-prefix)
sub_dir="/${sub_dir%/}"
}
svn_dir() {
[ -d ".svn" ] || return 1
base_dir="."
while [ -d "$base_dir/../.svn" ]; do base_dir="$base_dir/.."; done
base_dir=`cd $base_dir; pwd`
sub_dir="/$(sub_dir "${base_dir}")"
ref=$(svn info "$base_dir" | awk '/^URL/ { sub(".*/","",$0); r=$0 } /^Revision/ { sub("[^0-9]*","",$0); print r":"$0 }')
ref="[$ref]"
vcs="svn"
base_dir="$(basename "${base_dir}")"
}
git_dir || svn_dir
if [ -n "$vcs" ]; then
# I don't use svn much so happy to hide the vcs kind
#__vcs_prefix="($vcs)"
__vcs_prefix=""
__vcs_base_dir="${base_dir/$HOME/~}"
__vcs_ref="$ref"
__vcs_sub_dir="${sub_dir}"
else
__vcs_prefix=''
__vcs_base_dir="${PWD/$HOME/~}"
__vcs_ref=''
__vcs_sub_dir=''
fi
echo -ne "\033];${PWD##*/}\007"
}
export PROMPT_COMMAND=__vcs_dir
export PS1='\u$__vcs_ref:\[${_bold}\]${__vcs_base_dir}\[${_normal}\]${__vcs_refix}\[${_bold}\]${__vcs_sub_dir}\[${_normal}\]\$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment