dotfiles/.bashrc

103 lines
4.8 KiB
Bash

# Make bash a little more pleasent - these are valid for all versions.
shopt -s cdspell checkhash checkwinsize cmdhist histappend no_empty_cmd_completion
# Exit the shell on a Ctl+D.
IGNOREEOF=0
# History control.
HISTCONTROL="ignoredups"
HISTFILE="$HOME/.bash_history-${HOSTNAME%%.*}"
HISTFILESIZE=100000
HISTIGNORE="bg:bg *:fg:fg *:jobs:exit:clear:history"
HISTSIZE=-1
HISTTIMEFORMAT="%d/%m/%y %H:%M:%S "
history -r
# Determine the colour of the username in the prompt.
if (( $(id -u) == 0 )); then
COLOUR=1 # Red
else
COLOUR=2 # Green
fi
# Version specific set up.
if (( ${BASH_VERSINFO[0]} >= 4 )); then
# Add to the shopts.
shopt -s checkjobs dirspell
# Set the prompts.
PROMPT_DIRTRIM=2
# No colour:
# PS1="[\u@\h] \w ->"
# Coloured username:
# PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\h] \w ->"
# Coloured username + host:
# PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \w ->"
# Coloured username + host + directory:
PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\w\[$(tput sgr0)\] ->"
else
# Set the prompts.
# No colour:
# PS1="[\u@\h] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->"
# Coloured username:
# PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\h] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->"
# Coloured username + host:
# PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->"
# Coloured username + host + directory:
PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}')\[$(tput sgr0)\] ->"
fi
unset COLOUR
# Set the debugger prompt.
# PS4="+(\\\$? = \$?) \${BASH_SOURCE##*/}\${FUNCNAME:+(\$FUNCNAME)}:\$LINENO: "
# PS4="+(\[\e[1;33;40m\]\\\$? = \$?\[$(tput sgr0)\]) \[$(tput bold)$(tput setaf 4)\]\${BASH_SOURCE##*/}\[$(tput sgr0)\]\${FUNCNAME:+(\[$(tput bold)$(tput setaf 2)\]\$FUNCNAME\[$(tput sgr0)\])}:\[$(tput bold)$(tput setaf 1)\]\$LINENO\[$(tput sgr0)\]: "
export PS4="+(\[\e[1;33;40m\]\$?\[$(tput sgr0)\]) \[$(tput bold)$(tput setaf 4)\]\${BASH_SOURCE##*/}\[$(tput sgr0)\]\${FUNCNAME:+(\[$(tput bold)$(tput setaf 2)\]\$FUNCNAME\[$(tput sgr0)\])}:\[$(tput bold)$(tput setaf 1)\]\$LINENO\[$(tput sgr0)\]: "
# The commands to execute before the prompt is displayed.
# PROMPT_COMMAND=""
# Platform specific set up.
PLATFORM="$(uname -s)"
if [[ "$PLATFORM" = "Linux" ]]; then
# Linux specific functions.
psgrep() {
[[ -z "$1" ]] && { printf "%s: %s\\n" "Usage" "$FUNCNAME <something>" >&2; return 1; }
ps -auwwx | command grep -E --color=always -- "(.*RSS.*|$1)" | grep -F -v "(.*RSS.*|"
}
# Linux specific aliases.
hash ftpwho >/dev/null 2>&1 && alias ftpwho='ftpwho -v'
hash iftop >/dev/null 2>&1 && alias iftop='TERM=vt100 iftop'
hash ip >/dev/null 2>&1 && alias ip='ip -color=auto'
hash last less >/dev/null 2>&1 && alias laston='last -a | less'
hash ls >/dev/null 2>&1 && alias ls='ls -bFv --color=auto'
hash minicom >/dev/null 2>&1 && alias minicom='minicom -m -c on'
hash mkpasswd >/dev/null 2>&1 && alias mkpasswd='mkpasswd -m sha512crypt'
hash mkpasswd >/dev/null 2>&1 && alias pwgen='mkpasswd -m sha512crypt'
hash pinfo >/dev/null 2>&1 && alias info='pinfo'
hash ping >/dev/null 2>&1 && alias ping='ping -b'
elif [[ "$PLATFORM" = "Darwin" ]]; then
# Darwin specific aliases.
hash df >/dev/null 2>&1 && alias df='df -P'
hash ls >/dev/null 2>&1 && alias ls='ls -bFG'
hash top >/dev/null 2>&1 && alias top='top -o cpu -S'
else
echo "${BASH_SOURCE##*/}: unsupported platform: $PLATFORM" >&2
fi
unset PLATFORM
# Common functions.
imagebin() {
[[ -z "$1" ]] && { printf "%s: %s\\n" "Usage" "$FUNCNAME <filename>" >&2; return 1; }
curl -F file="@${1:-}" https://imagebin.ca/upload.php | grep ^url: | cut -d: -f2-
}
# Common aliases.
hash bc >/dev/null 2>&1 && alias bc='bc -lq'
hash diff >/dev/null 2>&1 && alias diff='diff --color=auto -u'
hash grep >/dev/null 2>&1 && alias egrep='grep -E --color=auto'
hash grep >/dev/null 2>&1 && alias fgrep='grep -E --color=auto'
hash grep >/dev/null 2>&1 && alias grep='grep --color=auto'
hash nc >/dev/null 2>&1 && alias pastebin='nc termbin.com 9999'
hash screen >/dev/null 2>&1 && alias screen='screen -Ua'
hash shellcheck >/dev/null 2>&1 && alias shellcheck='shellcheck -x'