Skip to content

Instantly share code, notes, and snippets.

@donwilson
Created March 16, 2018 20:46
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 donwilson/a90ac3288ac660684de3b3885ce4ca10 to your computer and use it in GitHub Desktop.
Save donwilson/a90ac3288ac660684de3b3885ce4ca10 to your computer and use it in GitHub Desktop.
Add git repo and branch names to colorized bash prompt
function parse_git_repo {
local repo_path=$(git rev-parse --show-toplevel 2>/dev/null)
[[ ! -z "$repo_path" ]] && basename $repo_path
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function parse_git_info {
local repo_name=$(parse_git_repo)
local branch_name=$(parse_git_branch)
[[ ! -z "${repo_name// }" && ! -z "${branch_name// }" ]] && printf "{\[$(tput sgr0)\]\[\033[38;5;37m\]$repo_name\[$(tput sgr0)\]\[\033[38;5;15m\]/\[$(tput bold)\]\[$(tput sgr0)\]\[\033[38;5;111m\]$branch_name\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]} "
}
function set_bash_prompt {
local git_info=$(parse_git_info)
PS1="\[\033[38;5;7m\][\[$(tput sgr0)\]\[\033[38;5;229m\]\u\[$(tput sgr0)\]\[\033[38;5;249m\]@\[$(tput sgr0)\]\[\033[38;5;51m\]\h\[$(tput sgr0)\]\[\033[38;5;7m\]]\[$(tput sgr0)\]\[\033[38;5;15m\] $git_info\[$(tput bold)\]\[$(tput sgr0)\]\[\033[38;5;39m\]\w\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]\n\[$(tput sgr0)\]\[\033[38;5;245m\]\\$\[$(tput sgr0)\]\[\033[38;5;15m\] \[$(tput sgr0)\]"
}
# http://bashrcgenerator.com/
PROMPT_COMMAND=set_bash_prompt
@donwilson
Copy link
Author

bash

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