Skip to content

Instantly share code, notes, and snippets.

@gojimmypi
Last active December 28, 2020 05:02
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 gojimmypi/3b92f3a33d51252252976fba7bae407b to your computer and use it in GitHub Desktop.
Save gojimmypi/3b92f3a33d51252252976fba7bae407b to your computer and use it in GitHub Desktop.
show user, host, current path, git upstream origin, branch, and last exit code as bash prompt
#!/bin/bash
# intially generated from http://bashrcgenerator.com/ with additional modificationas
#
# creates prompt like this with username and directory path on one line, then most recent error code number and $ on next line:
# gojimmypi@ubuntu : ~
# 0 $
#
# prior version, no GitHub branch
# export PS1="\[\033[38;5;2m\]\u@\h\[$(tput sgr0)\]\[\033[38;5;15m\] : \[$(tput sgr0)\]\[\033[38;5;11m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\]\n\$? \\$ \[$(tput sgr0)\]"
# prior version, only show branch
# export PS1='\[\033[38;5;2m\]\u@\h\[$(tput sgr0)\]\[\033[38;5;15m\](WSL): \[$(tput sgr0)\]\[\033[38;5;11m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\] ($(__git_ps1 "%s")) \n\n'"$?"' \\$ \[$(tput sgr0)\]'
## current prompt example:
#
# gojimmypi@DESKTOP(WSL): /mnt/c/workspace-vs/fujprog/build/x86-debug (Visual-Studio-Build * u=) gojimmypi/fujprog
#
# 0 $
#
# thanks:
# http://bashrcgenerator.com/
# https://stackoverflow.com/questions/15883416/adding-git-branch-on-the-bash-command-prompt
# https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
source ~/.git-prompt.sh
GIT_PS1_SHOWUPSTREAM="bit"
GIT_PS1_SHOWCOLORHINTS="true"
GIT_PS1_SHOWDIRTYSTATE="true"
GIT_PS1_SHOWUPSTREAM="verbose"
# string '
# color \[\033[38;5;2m\] \[$(tput sgr0)\]
# User \u
# literal @
# host \h
# literal (WSL):
# color \[$(tput sgr0)\]\[\033[38;5;11m\] \[$(tput sgr0)\]\[\033[38;5;15m\]
# working directory \w
# literal (
# git branch $(__git_ps1 "%s")
# show remote origin git config --get remote.origin.url
# pipe results of above show |
# replace "https://github.com/" with empty strig sed -e "s/^https:\/\/github.com\///"
# replace ".git" with empty strig sed -e "s/.git//"
# literal )
# new line \n
# new line (blank) \n
# prior command error code '"$?"'
# literal (space)
# literal bash prompt $ \\$
# space
export PS1='\[\033[38;5;2m\]\u@\h\[$(tput sgr0)\]\[\033[38;5;15m\](WSL): \[$(tput sgr0)\]\[\033[38;5;11m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\] ($(__git_ps1 "%s")) $(git config --get remote.origin.url | sed -e "s/^https:\/\/github.com\///" | sed -e "s/.git//" ) \n\n'"$?"' \\$ \[$(tput sgr0)\] '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment