Skip to content

Instantly share code, notes, and snippets.

@crvdgc
Created June 9, 2022 14:51
Show Gist options
  • Save crvdgc/32d3328a7d6656c7990da6265168dcb8 to your computer and use it in GitHub Desktop.
Save crvdgc/32d3328a7d6656c7990da6265168dcb8 to your computer and use it in GitHub Desktop.
Set up bash prompt to show vi-mode status, nix-shell, (colored) git status, and exit status. Excerpt from https://github.com/crvdgc/home-manager
# download git-prompt.sh from
# https://github.com/git/git/raw/master/contrib/completion/git-prompt.sh
# put it into ~/.git-prompt.sh
source ~/.git-prompt.sh
# Colors
RESET_COLOR='\[\e[0m\]'
BRIGHT_BLACK='\[\e[30;1m\]'
RED='\[\e[31m\]'
# GREEN='\[\e[32m\]'
# YELLOW='\[\e[33m\]'
# BLUE='\[\e[34m\]'
DIRBLUE='\[\e[34;1m\]' # blue as is used by dircolors for dirs
CYAN='\[\e[36m\]'
GRAY='\[\e[37;0m\]'
function color() {
echo "$*${RESET_COLOR}"
}
function set_bash_prompt() {
declare -ri exit_status="$?"
declare -r username='\u'
declare -r hostname='\h'
declare -r working_dir='\w'
declare exit_status_pr
if [[ "${exit_status}" -ne 0 ]]; then
exit_status_pr="[$(color "${RED}${exit_status}")]"
else
exit_status_pr=''
fi
declare username_pr
if [[ -n "$IN_NIX_SHELL" ]]; then
username_pr="$(color "${BRIGHT_BLACK}[nix-shell]")"
else
username_pr="$(color "${CYAN}${username}")"
fi
declare hostname_pr
hostname_pr="$(color "${CYAN}${hostname}")"
declare working_dir_pr
working_dir_pr="$(color "${DIRBLUE}${working_dir}")"
export GIT_PS1_DESCRIBE_STYLE='branch'
export GIT_PS1_SHOWCOLORHINTS='y'
export GIT_PS1_SHOWDIRTYSTATE='y'
# export GIT_PS1_SHOWSTASHSTATE='y'
export GIT_PS1_SHOWUNTRACKEDFILES='y'
export GIT_PS1_SHOWUPSTREAM='auto'
__git_ps1 "${username_pr}@${hostname_pr}:${working_dir_pr}" "${exit_status_pr} $ "
}
PROMPT_COMMAND=set_bash_prompt
# https://wiki.archlinux.org/index.php/Readline
set editing-mode vi
set show-mode-in-prompt on
set vi-ins-mode-string \1\e[34;1m\2[I]\1\e[0m\2
set vi-cmd-mode-string \1\e[33;1m\2[N]\1\e[0m\2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment