Last active
September 29, 2019 12:31
-
-
Save RobRuana/f7b0a0fcaa89d164fa2e to your computer and use it in GitHub Desktop.
Customize bash for interactive shells
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-------------------------------------------------------- | |
# Customize bash for interactive shells | |
#-------------------------------------------------------- | |
# Source global definitions (if any) | |
if [ -f /etc/bashrc ]; then | |
. /etc/bashrc | |
fi | |
# Environment variable exports (if any) | |
if [ -f ~/.bash_exports ]; then | |
. ~/.bash_exports | |
fi | |
# Command aliases (if any) | |
if [ -f ~/.bash_aliases ]; then | |
. ~/.bash_aliases | |
fi | |
#-------------------------------------------------------- | |
# Automatically set $DISPLAY (if not set already) | |
#-------------------------------------------------------- | |
function get_xserver () | |
{ | |
case $TERM in | |
xterm* ) | |
XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' ) | |
XSERVER=${XSERVER%%:*} | |
;; | |
aterm | rxvt) | |
;; | |
esac | |
} | |
if [ -z ${DISPLAY:=""} ]; then | |
get_xserver | |
if [[ -z ${XSERVER} || ${XSERVER} == $(hostname) || ${XSERVER} == "unix" ]]; then | |
DISPLAY=":0.0" # Display on local host | |
else | |
DISPLAY=${XSERVER}:0.0 # Display on remote host | |
fi | |
fi | |
export DISPLAY | |
#-------------------------------------------------------- | |
# Settings | |
#-------------------------------------------------------- | |
ulimit -S -c 0 # Don't want any coredumps | |
set -o notify | |
set -o noclobber | |
set -o ignoreeof | |
set -o nounset | |
#set -o xtrace # useful for debuging | |
# Enable options: | |
shopt -s cdspell | |
shopt -s cdable_vars | |
shopt -s checkhash | |
shopt -s checkwinsize | |
shopt -s sourcepath | |
shopt -s no_empty_cmd_completion | |
shopt -s cmdhist | |
shopt -s histappend histreedit histverify | |
# Disable options: | |
shopt -u mailwarn | |
unset MAILCHECK # I don't want my shell to warn me of incoming mail | |
#-------------------------------------------------------- | |
# Greeting displayed when you start an interactive shell | |
#-------------------------------------------------------- | |
green='\033[0;92m' | |
GREEN='\033[1;92m' | |
red='\033[0;91m' | |
RED='\033[1;91m' | |
blue='\033[0;94m' | |
BLUE='\033[1;94m' | |
cyan='\033[0;96m' | |
CYAN='\033[1;96m' | |
NC='\033[0m' # No Color | |
# Don't display the welcome banner if this is a subshell | |
if [ -z "${PROMPT_PREFIX:=}" ]; then | |
if [ ! -z "${PS1:-}" ]; then | |
echo -e "${blue}This is BASH ${red}${BASH_VERSION%.*}${blue} - DISPLAY on ${red}${DISPLAY}${NC}" | |
date +"%B %e, %Y - %a %r %Z" | |
fi | |
# function to run upon exit of shell | |
function _exit() | |
{ | |
echo -e "${red}This place is dead anyway.${NC}" | |
} | |
trap _exit EXIT | |
fi | |
#-------------------------------------------------------- | |
# Customize the prompt | |
#-------------------------------------------------------- | |
# Test connection type | |
if [ -n "${SSH_CONNECTION:=}" ]; then | |
CNX=${cyan} # remote machine via ssh | |
elif [[ "${DISPLAY}" == "/private/tmp/com.apple.launchd."*"/org.macosforge.xquartz:0" ]]; then | |
CNX=${green} # local machine via unix domain socket | |
elif [[ "${DISPLAY}" == ":0.0" ]]; then | |
CNX=${green} # local machine | |
elif [[ "${DISPLAY%%??:0.0}" == "" ]]; then | |
CNX=${green} # local machine | |
elif [[ "${DISPLAY%%:0*}" != "" ]]; then | |
CNX=${red} # remote machine NOT via ssh | |
else | |
CNX=${GREEN} # catch all, probably a local machine | |
fi | |
# Test user type | |
if [[ ${USER} == "root" ]]; then | |
SU=${red} # User is root | |
elif [[ ${USER} != $(logname) ]]; then | |
SU=${cyan} # User is not login user | |
else | |
SU=${blue} # User is normal | |
fi | |
# Replace \W with \w for full path name | |
unset PROMPT_COMMAND | |
case "$TERM" in | |
*term | rxvt* | linux | xterm-256color ) | |
PS1="\[${CNX}\]\u@\h\[${NC}\]:\[${SU}\]\w\[${NC}\]\$ " | |
;; | |
*) | |
PS1="\u@\h:\w\$ " | |
;; | |
esac | |
# Add the prompt prefix if it exists | |
if [ -n "${PROMPT_PREFIX:=}" ]; then | |
PS1=${PROMPT_PREFIX}${PS1} | |
fi | |
#-------------------------------------------------------- | |
# Functions | |
#-------------------------------------------------------- | |
# swap two files | |
function swap() | |
{ | |
local TMPFILE=tmp.$$ | |
mv "$1" $TMPFILE | |
mv "$2" "$1" | |
mv $TMPFILE "$2" | |
} | |
# clean python build files | |
pyclean () { | |
find . -name "*.py[co]" -type f -delete | |
find . -name "__pycache__" -type d | xargs rm -rf | |
find . -name ".pytest_cache" -type d | xargs rm -rf | |
} | |
# clean tox environments | |
toxclean () { | |
find . -name ".tox" -type d | xargs rm -rf | |
} | |
#-------------------------------------------------------- | |
# Programmable completion | |
#-------------------------------------------------------- | |
shopt -s extglob # necessary | |
set +o nounset # otherwise some completions will fail | |
complete -A hostname rsh rcp telnet rlogin r ftp ping disk ssh scp | |
complete -A export printenv | |
complete -A variable export local readonly unset | |
complete -A enabled builtin | |
complete -A alias alias unalias | |
complete -A function function | |
complete -A user su mail finger | |
complete -A helptopic help # currently same as builtins | |
complete -A shopt shopt | |
complete -A stopped -P '%' bg | |
complete -A job -P '%' fg jobs disown | |
complete -A directory mkdir rmdir | |
complete -A directory -o default cd | |
# Compression | |
complete -f -o default -X '*.+(zip|ZIP)' zip | |
complete -f -o default -X '!*.+(zip|ZIP)' unzip | |
complete -f -o default -X '*.+(z|Z)' compress | |
complete -f -o default -X '!*.+(z|Z)' uncompress | |
complete -f -o default -X '*.+(gz|GZ)' gzip | |
complete -f -o default -X '!*.+(gz|GZ)' gunzip | |
complete -f -o default -X '*.+(bz2|BZ2)' bzip2 | |
complete -f -o default -X '!*.+(bz2|BZ2)' bunzip2 | |
if [ -f $(brew --prefix)/etc/bash_completion ]; then | |
. $(brew --prefix)/etc/bash_completion | |
fi | |
_killall () | |
{ | |
local cur prev | |
COMPREPLY=() | |
cur=${COMP_WORDS[COMP_CWORD]} | |
# get a list of processes (the first sed evaluation | |
# takes care of swapped out processes, the second | |
# takes care of getting the basename of the process) | |
COMPREPLY=( $( ps -u $USER -o comm | \ | |
sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \ | |
awk '{if ($0 ~ /^'$cur'/) print $0}' )) | |
return 0 | |
} | |
complete -F _killall killall killps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment