Skip to content

Instantly share code, notes, and snippets.

@rgraham717
Created June 28, 2025 13:04
Show Gist options
  • Select an option

  • Save rgraham717/df4f1ece33a651b24a1bf6485dc26b33 to your computer and use it in GitHub Desktop.

Select an option

Save rgraham717/df4f1ece33a651b24a1bf6485dc26b33 to your computer and use it in GitHub Desktop.
Colorful bash prompt including pyenv virtualenv and git branch status. just source this at the end of your .bashrc or .profile
# Add this to your ~/.bashrc or ~/.bash_profile
# Color definitions
RED='\[\033[0;31m\]'
GREEN='\[\033[0;32m\]'
YELLOW='\[\033[0;33m\]'
BLUE='\[\033[0;34m\]'
PURPLE='\[\033[0;35m\]'
CYAN='\[\033[0;36m\]'
WHITE='\[\033[0;37m\]'
BOLD='\[\033[1m\]'
RESET='\[\033[0m\]'
# Function to get git branch
git_branch() {
local branch
if branch=$(git symbolic-ref --short HEAD 2>/dev/null); then
echo " (${branch})"
elif branch=$(git describe --exact-match HEAD 2>/dev/null); then
echo " (${branch})"
elif branch=$(git rev-parse --short HEAD 2>/dev/null); then
echo " (${branch})"
fi
}
# Function to check if git repo has uncommitted changes
git_has_changes() {
if git rev-parse --git-dir > /dev/null 2>&1; then
local status=$(git status --porcelain 2>/dev/null)
if [[ -n $status ]]; then
return 0 # Has changes
else
return 1 # No changes
fi
fi
return 1 # Not a git repo
}
# Function to get pyenv virtualenv
pyenv_virtualenv() {
if command -v pyenv >/dev/null 2>&1; then
local venv=$(pyenv version-name 2>/dev/null)
if [[ $venv != "system" ]]; then
echo " [${venv}]"
fi
fi
}
# Set the prompt
set_prompt() {
local last_exit_code=$?
# User and host
PS1="${BOLD}${GREEN}\u@\h${RESET}"
# Current directory
PS1+=" ${BOLD}${BLUE}\w${RESET}"
# Pyenv virtualenv (in purple)
local venv_info=$(pyenv_virtualenv)
if [[ -n $venv_info ]]; then
PS1+="${PURPLE}${venv_info}${RESET}"
fi
# Git branch (red if changes, green if clean)
local branch_info=$(git_branch)
if [[ -n $branch_info ]]; then
if git_has_changes; then
PS1+="${RED}${branch_info}${RESET}"
else
PS1+="${GREEN}${branch_info}${RESET}"
fi
fi
# Exit code indicator (red if non-zero)
if [[ $last_exit_code -ne 0 ]]; then
PS1+=" ${RED}[${last_exit_code}]${RESET}"
fi
# Prompt symbol
PS1+="\n${BOLD}${WHITE}\$ ${RESET}"
}
# Set PROMPT_COMMAND to update prompt before each command
PROMPT_COMMAND=set_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment