Skip to content

Instantly share code, notes, and snippets.

@wrossmann
Created November 1, 2018 18:12
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 wrossmann/bd0fa6f76697406251119445e057acf4 to your computer and use it in GitHub Desktop.
Save wrossmann/bd0fa6f76697406251119445e057acf4 to your computer and use it in GitHub Desktop.
Simple and full-featured bash prompt with git info
# I've seen a _lot_ of code written to make bash prompts that are just _OK_,
# but there's no reason to invoke python/ruby/etc and parse git output since
# git ships with a pretty outstanding utility.
# 1. Load the library that already ships with git
source /usr/share/doc/${git_doc_path_here}/contrib/completion/git-prompt.sh
# 2. Set your options
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
# 3. Set the PROMPT_COMMAND
export PROMPT_COMMAND='__git_ps1 "\u@\h:\w" " \\\$ "'
# For more information just read `git-prompt.sh` and the other files in
# `doc/${git_doc_path_here}/contrib` as there's a lot of good info in there.
@wrossmann
Copy link
Author

wrossmann commented Nov 8, 2018

I've found that this also unintentionally alters terminal titling if you don't preserve the portion of your previous PS1/PROMPT_COMMAND that is delimited by \033]0; and \007 which is the bit of data that sets your terminal title. Alsom now we need \[ and \] to enclose non-printing prompt chars so that the terminal still renders correctly.

So an updated step 3 would look like:

export PROMPT_COMMAND='__git_ps1 "\[\033]0;\u@\h:\w\007\]\u@\h:\w" " \\\$ "'

And also if you want full-colored goodness:

export PROMPT_COMMAND='__git_ps1 "\[\033]0;\u@\h:\w\007\033[31;1m\]\u\[\033[0m\]@\[\033[34;1m\]\h\[\033[0m\]:\[\033[33m\]\w\[\033[0m\]" " \\\$ "'

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