dotfiles/bash/bashrc

73 lines
2.4 KiB
Bash

#!/bin/bash
# Set up bash for interactive use (options, prompt, aliases, etc.)
while read -r f; do [ -f "$f" ] && source "$f"; done <<EOL
${XDG_CONFIG_HOME}/shell/aliases
${XDG_CONFIG_HOME}/shell/solarized
EOL
unset f
# Prevent ctrl-s from freezing the terminal.
stty stop undef
shopt -s cdspell checkwinsize globstar histappend nocaseglob
set -o noclobber # Prevent overwriting files with output redirection.
# Eternal bash history (from https://stackoverflow.com/a/19533853)
HISTCONTROL=erasedups
HISTFILESIZE=
HISTSIZE=
HISTTIMEFORMAT="[%F %T] "
HISTFILE="$XDG_DATA_HOME/bash/history"
# Prompt configuration.
Reset="\[$(tput sgr0)\]"
PS1_EXIT="\[$(tput setaf "$Red" )\]" # color for last exit code if non-zero
PS1_ROOT="\[$(tput setaf "$Orange" )\]" # logged in as root
PS1_SSH="\[$(tput setaf "$Yellow" )\]" # hostname for SSH sessions
PS1_PWD="\[$(tput setaf "$Cyan" )\]" # PWD color
PS1_GIT="\[$(tput setaf "$Blue" )\]" # color for git branch
PS1_VENV="\[$(tput setaf "$Violet" )\]" # color for python virtual env
PS1_JOBS="\[$(tput setaf "$Magenta")\]" # color for background jobs
PS1_SEP_COLOR="\[$(tput setaf "$Base01" )\]"
PS1_SEP=" > " # separator between prompt parts
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM=verbose
PROMPT_COMMAND=__ps1_set
PS2="... "
__ps1_set() {
local exit=$?
local prompt=">>>>>>>>>>"
local sep_color="$PS1_SEP_COLOR"
local ps=()
[ $exit -ne 0 ] && ps+=("$PS1_EXIT$exit")
[ $EUID -eq 0 ] && { ps+=("$PS1_ROOT\u"); prompt="##########"; }
[ -n "$SSH_CONNECTION" ] && ps+=("$PS1_SSH\h")
ps+=("$PS1_PWD\w")
type __git_ps1 && __git_ps1 '' '' "$PS1_GIT%s" && [ -n "$PS1" ] && ps+=("$PS1")
[ -n "$VIRTUAL_ENV" ] && ps+=("$PS1_VENV${VIRTUAL_ENV##*/}")
local j="\j" && [ "${j@P}" -gt 0 ] && ps+=("$PS1_JOBS${j@P} bg")
local extra=""
[ ${#ps[@]} -gt 1 ] && printf -v extra "$sep_color$PS1_SEP%s" "${ps[@]:1}"
PS1="${ps[0]}$extra$sep_color ${prompt:0:$SHLVL}$Reset "
} &>/dev/null
# Source extra configurations if available.
_source_extra_configs() {
local configs=(
/usr/local/etc/profile.d/bash_completion.sh
/usr/share/bash-completion/bash_completion
$HOME/.local/etc/bash/*
)
local f
for f in ${configs[@]}; do [ -f "$f" ] && source "$f"; done
}
_source_extra_configs || true