Skip to content

Instantly share code, notes, and snippets.

@ryanpitts
Last active October 13, 2021 12:24
Show Gist options
  • Save ryanpitts/7900407 to your computer and use it in GitHub Desktop.
Save ryanpitts/7900407 to your computer and use it in GitHub Desktop.
This is a SUPER handy bash_prompt update. Source this at the end of your .bash_profile, and your command line while in git repo directories adds some awesome information. Example: http://cl.ly/image/1A0c1D3R2H1R Adapted from https://github.com/necolas/dotfiles/blob/master/shell/bash_prompt
#!/bin/bash
# bash_prompt
# Example:
# nicolas@host: ~/.dotfiles on master[+!?$]
# $
# Screenshot: http://i.imgur.com/DSJ1G.png
# iTerm2 prefs: import Solarized theme (disable bright colors for bold text)
# Color ref: http://vim.wikia.com/wiki/Xterm256_color_names_for_console_Vim
# More tips: http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
export TERM=gnome-256color
elif infocmp xterm-256color >/dev/null 2>&1; then
export TERM=xterm-256color
fi
tput sgr 0 0
# Base styles and color palette
# Solarized colors
# https://github.com/altercation/solarized/tree/master/iterm2-colors-solarized
BOLD=$(tput bold)
RESET=$(tput sgr0)
SOLAR_YELLOW=$(tput setaf 136)
SOLAR_ORANGE=$(tput setaf 166)
SOLAR_RED=$(tput setaf 124)
SOLAR_MAGENTA=$(tput setaf 125)
SOLAR_VIOLET=$(tput setaf 61)
SOLAR_BLUE=$(tput setaf 33)
SOLAR_CYAN=$(tput setaf 37)
SOLAR_GREEN=$(tput setaf 64)
SOLAR_WHITE=$(tput setaf 254)
GRAY=$(tput setaf 252)
style_user="\[${RESET}${SOLAR_GREEN}\]"
style_host="\[${RESET}${SOLAR_GREEN}\]"
style_path="\[${RESET}${SOLAR_GREEN}\]"
style_chars="\[${RESET}${GRAY}\]"
style_branch="${SOLAR_CYAN}"
if [[ "$SSH_TTY" ]]; then
# connected via ssh
style_host="\[${BOLD}${SOLAR_RED}\]"
elif [[ "$USER" == "root" ]]; then
# logged in as root
style_user="\[${BOLD}${SOLAR_RED}\]"
fi
is_git_repo() {
$(git rev-parse --is-inside-work-tree &> /dev/null)
}
is_git_dir() {
$(git rev-parse --is-inside-git-dir 2> /dev/null)
}
get_git_branch() {
local branch_name
branch_name=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) ||
# Otherwise, just give up
branch_name="(unknown)"
printf $branch_name
}
# Git status information
prompt_git() {
local git_info git_state uc us ut st
if ! is_git_repo || is_git_dir; then
return 1
fi
git_info=$(get_git_branch)
# Check for uncommitted changes in the index
if ! $(git diff --quiet --ignore-submodules --cached); then
uc="${SOLAR_GREEN}+"
fi
# Check for unstaged changes
if ! $(git diff-files --quiet --ignore-submodules --); then
us="${SOLAR_RED}!"
fi
# Check for untracked files
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
ut="${SOLAR_ORANGE}?"
fi
# Check for stashed files
if $(git rev-parse --verify refs/stash &>/dev/null); then
st="${SOLAR_YELLOW}$"
fi
git_state=$uc$us$ut$st
# Combine the branch name and state information
if [[ $git_state ]]; then
git_info="$git_info${SOLAR_CYAN}[$git_state${SOLAR_CYAN}]"
fi
# You know you’re on `gh-pages`, right? *Right*?
if [[ $git_info == "gh-pages" ]]; then
style_branch="${SOLAR_BLUE}"
fi
# Don’t screw up `stable`.
if [[ $git_info == *stable* ]]; then
style_branch="${SOLAR_RED}"
fi
printf "${SOLAR_WHITE} on ${style_branch}${git_info}"
}
# Set the terminal title to the current working directory
PS1="\[\033]0;\w\007\]"
# Build the prompt
PS1+="\n" # Newline
PS1+="${style_user}\u" # Username
PS1+="${style_chars}@" # @
PS1+="${style_host}\h" # Host
PS1+="${style_chars}: " # :
PS1+="${style_path}\w" # Working directory
PS1+="\$(prompt_git)" # Git details
PS1+="\n" # Newline
PS1+="${style_chars}\$ \[${RESET}\]" # $ (and reset color)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment