Skip to content

Instantly share code, notes, and snippets.

@evansde77
Created February 25, 2015 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evansde77/5f5d6d2d65a8c6bc0b59 to your computer and use it in GitHub Desktop.
Save evansde77/5f5d6d2d65a8c6bc0b59 to your computer and use it in GitHub Desktop.
Useful Bash prompt/tab settings for showing git info
#
# Added to .bash_profile on a mac, these utils will show the
# current repo in the terminal tab name and will include the current git
# branch name on the shell prompt.
#
function set_terminal_tabname {
# function that sets Terminal Tab name to $1
printf "\e]1;$1\a"
}
function repo_name
{
# function to return the name of the current git repo
# or empty string if not in a repo
if git rev-parse --git-dir > /dev/null 2>&1; then
REPO="$(git rev-parse --show-toplevel)"
if [ ! -z "$REPO" ]; then
echo `basename $REPO`
fi
fi
}
function set_terminal_tab_reponame
{
# Set the terminal to the current repo name
REPONAME=`repo_name`
if [ -z "$REPO" ]; then
set_terminal_tabname $REPONAME
return
fi
return
}
function parse_git_branch ()
{
ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
set_terminal_tab_reponame;
BRANCH="("${ref#refs/heads/}")";
echo "${BRANCH}"
}
export PS1="[\t \W] \$(parse_git_branch) "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment