dotfiles/.bashrc

311 lines
10 KiB
Bash
Raw Normal View History

2021-04-15 22:45:15 +00:00
#
2021-09-17 05:46:44 +00:00
# .bashrc
2021-04-15 22:45:15 +00:00
# (Note: this _should_ be _somewhat_ OS-agnostic, at least, enough for me)
#
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
# source global bashrc settings (turned off by default, uncomment otherwise)
# [ -f /etc/bashrc ] && source /etc/bashrc
shopt -s checkwinsize # check window size after cmd, update LINES and COLUMNS
shopt -s globstar # pattern '**' matches all files, >= 0 dir/subdirs
2021-09-17 05:46:44 +00:00
#shopt -s expand_aliases # //
2021-04-15 22:45:15 +00:00
2021-09-17 05:46:44 +00:00
2021-04-15 22:45:15 +00:00
HISTSIZE=48000 # history lines stored in memory (while using bash)
HISTFILESIZE=48000 # history lines stored in .bash_history after session
HISTCONTROL=ignoredups # ignore duplicate commands or if start with a space
shopt -s histappend # append to history file instead of overwriting
2021-04-15 22:45:15 +00:00
case "$(uname -s)" in # for OS-specific configuration
Linux) operating_system='linux' ;;
Darwin) operating_system='macOS' ;;
FreeBSD|OpenBSD|NetBSD) operating_system='BSD' ;;
CYGWIN*|MINGW32*|MSYS*|MINGW*) operating_system='windows' ;;
*) operating_system='other' ;;
esac
[ -f /etc/hostname ] && hostname="$(cat /etc/hostname)"
# apply "secret" settings (these are just settings I don't want in git)
[ -f "$HOME"/.bashrc_secrets ] && source "$HOME"/.bashrc_secrets
# apply work settings
[ -f "$HOME"/.bash_work_stuff ] && source "$HOME"/.bash_work_stuff
2021-04-15 22:45:15 +00:00
# set PATH to something tolerable
# NOTE: in Arch, everything's already in /usr/bin, /usr/sbin anyway
if [ "$operating_system" = "linux" ]; then
PATH="/usr/bin:/usr/local/bin:/bin:/usr/sbin:/usr/local/sbin:/sbin" # base
PATH="$PATH:/var/lib/snapd/snap/bin" # snapd
PATH="$PATH:/usr/local/games:/usr/games" # base
if [ "$hostname" = "shorefall" ]; then
PATH="$PATH:$HOME/bin" # secret sauce
PATH="$PATH:$HOME/bin/jdk-11.0.10+9/bin" # JavaTM
PATH="$PATH:$HOME/bin/android-studio/bin" # Android Studio
PATH="$PATH:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl"
fi
elif [ "$operating_system" = "macOS" ]; then
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
PATH="$PATH:/Applications/Vmware Fusion.app/Contents/Public"
PATH="$PATH:/Library/Apple/usr/bin"
work_set_path "$PATH"
2021-04-15 22:45:15 +00:00
fi
export PATH
2021-09-17 05:46:44 +00:00
export BROWSER="firefox"
if [[ "$operating_system" = "macOS" || "$operating_system" = "BSD" ]] ; then
export CLICOLOR=1
fi
2021-04-15 22:45:15 +00:00
# use 'nvim' if it exists on system
[ command -v nvim &> /dev/null ] && export EDITOR="nvim" || export EDITOR="vim"
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
# PS1 settings
#Old PS1: PS1="[\u@\h]:\w\$ "
if [[ $(id -u) -eq 0 ]]; then # if current user is root
PS1="\[\033[41m\]\[\033[37m\][\u@\h]\[\033[00m\]:\[\033[01;31m\]\w\[\033[00m\]\$ "
elif [[ -f ~/.bash_prompt ]]; then # not root, but ~/.bash_prompt exists
unset color_prompt force_color_prompt
source ~/.bash_prompt
else # if not root, and no prompt config
PS1="\[\033[1;37m\][\u@\h]\[\033[00m\]:\[\033[01;32m\]\w\[\033[00m\]$ "
fi
# colored GCC warnings and errors
GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01: quote=01'
export GCC_COLORS
2021-09-17 05:46:44 +00:00
############################## user functions ##############################
2021-04-15 22:45:15 +00:00
# find-keyword [directory path] [keyword]: find and list files with keyword in
# the filename
find-keyword() {
if [ "$#" -ne 2 ]; then # if not given 2 arguments
echo "Usage: find-keyword [directory path] [keyword]"
else
find $1 -name "*$2*" -ls
fi
}
# find-string [string] [directory path]: find and list files containing the
# provided string (and list all occurences/line numbers of the string)
find-string() {
if [ "$#" -ne 2 ]; then # if not given 2 arguments
echo "Usage: find-string [string] [directory path]"
else
grep -nR "$1" "$2"
fi
}
# img_loop_here: $1 = seconds until next photo displayed
# BUG: currently, after running this function, the user needs to manually run
# 'reset' to unscrew the terminal
2021-09-17 05:46:44 +00:00
img-loop-here() {
if [ "$1" = "" ]; then
echo "Usage: img_loop_here [seconds until next photo displayed]"
else
while true; do
for g in $(ls .); do
timeout "$1" chafa -c full "$g"
done
done
fi
}
2021-09-17 05:46:44 +00:00
# rainbow-shell: print out a looping rainbow in the shell. Happy pride month!
# For best results, run in full-screened terminal (Mod+f in i3)
rainbow-shell() {
yes "$(seq 231 -1 16)" | while read i; do
printf "\x1b[48;5;${i}m\n"
sleep .02
done
}
# colors - print out current color values set in terminal
colors() {
local fgc bgc vals seq0
printf "Color escapes are %s\n" '\e[${value};...;${value}m'
printf "Values 30..37 are \e[33mforeground colors\e[m\n"
printf "Values 40..47 are \e[43mbackground colors\e[m\n"
printf "Value 1 gives a \e[1mbold-faced look\e[m\n\n"
# foreground colors
for fgc in {30..37}; do
# background colors
for bgc in {40..47}; do
fgc=${fgc#37} # white
bgc=${bgc#40} # black
vals="${fgc:+$fgc;}${bgc}"
vals=${vals%%;}
seq0="${vals:+\e[${vals}m}"
printf " %-9s" "${seq0:-(default)}"
printf " ${seq0}TEXT\e[m"
printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
done
echo; echo
done
}
# term-color-vals: print all 256-bit color values in their associated color
term-color-vals() {
for i in {0..255}; do
printf "\x1b[38;5;${i}mcolor%-5i\x1b[0m" $i
if ! (( ($i + 1 ) % 8 )); then
echo
fi
done
}
# ex - archive extractor
# usage: ex <file>
ex() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# public-ip: fetch current public IP address
public-ip() {
curl 'http://icanhazip.com/'
}
# scan-for-printers: look for printers on the connected LAN
scan-for-printers() {
sudo lpinfo -v
}
# wifi-strength: get some WiFi statistics in real time
wifi-strength() {
watch -n 1 cat /proc/net/wireless
}
############################## aliases ##############################
2021-04-15 22:45:15 +00:00
# Note to self: alias substitution is recursive AS LONG AS each expanded alias
# ends in a space character (allowing for multiple aliases to be used on one
# line). This is a POSIX specification. You do not know how long it took me to
# figure this out.
# miscellaneous aliases
alias cl='clear '
alias e='exit '
alias q='exit '
alias DIE='shutdown now '
alias cp='cp -i ' # confirm before overwriting a file
2021-09-17 05:46:44 +00:00
alias df='df -h '
2021-04-15 22:45:15 +00:00
alias fucking='sudo '
alias goddammit=' '
alias bottom='top '
alias bc='bc -l ' # makes bc use 'scale=20' by default
2021-04-15 22:45:15 +00:00
alias vim="$EDITOR "
alias bashrc="$EDITOR ~/.bashrc"
alias vimrc="$EDITOR ~/.vimrc"
alias nanorc="nano ~/.config/nano/nanorc" # just for fun!
2021-04-15 22:45:15 +00:00
alias rot13="tr 'A-Za-z' 'N-ZA-Mn-za-m' "
2021-07-03 01:39:37 +00:00
2021-04-15 22:45:15 +00:00
not_in_vim="echo -e \"You're not in vim doofus!\""
alias :wq="$not_in_vim" ; alias :q="$not_in_vim" ; alias :x="$not_in_vim"
alias ZZ="$not_in_vim"
alias rager='ranger '; alias anger='ranger '; alias range='ranger '
alias rangre='ranger '; alias rangr='ranger ' # <<<-^^^-: common ranger typos
alias gdb='gdb -q ' # muzzle gdb
if [ "$hostname" = "shorefall" ]; then
alias i3config="$EDITOR ~/.config/i3/config && i3-msg reload && i3-msg restart"
alias spellcheck='aspell check '
alias iso-date='date --iso-8601=s '
fi
2021-04-15 22:45:15 +00:00
if [[ "$operating_system" = "macOS" ]]; then
alias ls='ls -C '
alias l='ls -C '
alias ll='ls -lh '
alias la='ls -A '
alias lla='ls -lhA '
elif [[ "$operating_system" = "BSD" ]]; then
alias ls='ls --color=auto '
alias l='ls --color=auto '
alias ll='ls -lh --color=auto '
alias la='ls -A --color=auto '
alias lla='ls -lhA --color=auto '
else # assume GNU ls (most desktop Linux distros)
2021-04-15 22:45:15 +00:00
alias ls='ls -C --color=auto '
alias l='ls -C --color=auto '
alias ll='ls -lh --color=auto '
alias la='ls -A --color=auto '
alias lla='ls -lhA --color=auto '
alias grep='grep --color=auto '
alias egrep='egrep --color=auto '
alias fgrep='fgrep --color=auto '
2021-04-15 22:45:15 +00:00
fi
2021-09-17 05:46:44 +00:00
############################## bash completion ##############################
2021-04-15 22:45:15 +00:00
# enable programmable completion features (possibly already enabled by
# /etc/bash.bashrc or /etc/profile).
if [ -f /usr/share/bash-completion/bash_completion ]; then
source /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
source /etc/bash_completion
fi
if [ "$operating_system" = "macOS" ]; then
if [ -f /usr/local/etc/bash_completion ]; then # for macOS
source /usr/local/etc/bash_completion
elif [ -d /usr/local/etc/bash_completion.d ]; then # also for macOS
for file in /usr/local/etc/bash_completion.d/* ; do
source "$file"
done
fi
fi
complete -cf sudo # so sudo actually has tab autocompletion
bind 'set completion-ignore-case on' # case-insensitive tab autocompletion