Skip to content

Instantly share code, notes, and snippets.

@ghthor
Created October 28, 2011 14:47
Show Gist options
  • Save ghthor/1322445 to your computer and use it in GitHub Desktop.
Save ghthor/1322445 to your computer and use it in GitHub Desktop.
Bash Prompt Upgrades
# -------------------------------------------------------
# Prompt / Xterm
# -------------------------------------------------------
# Prompt colors
_txt_col="\e[00m" # Std text (white)
_bld_col="\e[01;37m" # Bold text (white)
_wrn_col="\e[01;31m" # Warning
_sep_col=$_txt_col # Separators
_usr_col="\e[01;32m" # Username
_cwd_col=$_txt_col # Current directory
_hst_col="\e[0;32m" # Host
_env_col="\e[0;36m" # Prompt environment
_git_col="\e[01;36m" # Git branch
# Returns the current git branch (returns nothing if not a git repository)
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
parse_git_dirty() {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "±"
}
# Returns the current ruby version.
parse_ruby_version() {
if (which ruby | grep -q ruby); then
ruby -v | cut -d ' ' -f2
fi
}
# Set the prompt string (PS1)
# Looks like this:
# user@computer ~/src/ubuntu_config [master|1.8.7]$
# (Prompt strings need '\['s around colors.)
set_ps1() {
user_str="\[$_usr_col\]\u\[$_hst_col\]@\h\[$_txt_col\]"
dir_str="\[$_cwd_col\]\w"
git_branch=`parse_git_branch`
git_dirty=`parse_git_dirty`
ruby=`parse_ruby_version`
git_str="\[$_git_col\]$git_branch\[$_wrn_col\]$git_dirty"
# Git repo & ruby version
if [ -n "$git_branch" ] && [ -n "$ruby" ]; then
env_str="\[$_env_col\][$git_str\[$_env_col\]|$ruby]"
# Just git repo
elif [ -n "$git_branch" ]; then
env_str="\[$_env_col\][$git_str\[$_env_col\]]"
# Just ruby version
elif [ -n "$ruby" ]; then
env_str="\[$_env_col\][$ruby]"
else
unset env_str
fi
# < username >@< hostname > < current directory > [< git branch >|< ruby version >]
PS1="${debian_chroot:+($debian_chroot)}$user_str $dir_str $env_str\[$_sep_col\]$ \[$_txt_col\]"
}
# Set custom prompt
PROMPT_COMMAND='set_ps1;'
# Set GREP highlight color
export GREP_COLOR='1;32'
# Custom Xterm/RXVT Title
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND+='echo -ne "\e]0;${USER}@${HOSTNAME}: ${PWD}\007";'
;;
*)
;;
esac
# -------------------------------------------------------
# Prompt / Xterm
# -------------------------------------------------------
@ghthor
Copy link
Author

ghthor commented Oct 28, 2011 via email

@kotnik
Copy link

kotnik commented Oct 24, 2012

Since git 1.8.0, string "nothing to commit (working directory clean)" changed to "nothing to commit, working directory clean".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment