Skip to content

Instantly share code, notes, and snippets.

@AlexMoffat
Last active December 14, 2015 23:38
Show Gist options
  • Save AlexMoffat/5166794 to your computer and use it in GitHub Desktop.
Save AlexMoffat/5166794 to your computer and use it in GitHub Desktop.
Set the bash prompt, terminal tab and window names on OS X. Based on this https://gist.github.com/henrik/31631 with additional comments and using PROMPT_COMMAND instead of directly setting PS1. Add to your .bashrc file. Format of prompt is "last_part_of_directory_path $" - If not in git managed directory "last_part_of_directory_path [branch_name…
# Set the bash prompt, terminal tab and window names on OS X.
#
# Return * if changes, nothing otherwise.
# You could add "--untracked-files=no" to the git status command if you
# don't want untracked files to mark the directory as dirty.
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
# Return branch name with dirty flag (* after name if dirty) or nothing if not in git directory.
# First sed expression matches line starting with *. Second sed expression extracts the rest of
# the line as the branch name.
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
# Set the prompt, tab and window names.
function set_prompt {
# Terminal tab name.
last_part_of_path=${PWD##*/};
printf "\e]1;${last_part_of_path}\a";
# Terminal window name.
hostname=`hostname`
printf "\e]2;${hostname}\a";# Prompt character
# Emoji. Find the hex for the character you're interested in by using the character viewer,
# right clicking on the character and choosing "Copy Character Info". Pasting into a text
# doc will show you the hex code. I'm adding an extra space to the end to allow enough
# space for the prompt.
prompt_char=$'\xF0\x9F\x8D\x99 '
# Simple dollar sign.
#prompt_char='$'
# Command prompt set by setting PS1.
export PS1='\[\033[1;33m\]${last_part_of_path}\[\033[0m\] \[\033[1;35m\]$(parse_git_branch)\[\033[0m\]${prompt_char} '
}
# Before displaying prompt execute this command.
export PROMPT_COMMAND=set_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment