.bashrc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
#
# .bashrc
# (Note: this _should_ be _somewhat_ OS-agnostic, at least, enough for me)
#
# uncomment below to profile bashrc startup in /tmp/bashstart.SEC.NANOSEC
#PS4='+ $(date "+%s.%N")\011 '
#exec 3>&2 2>/tmp/bashstart.$$.log
#set -x
# 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
#shopt -s expand_aliases # //
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
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)"
# 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:/usr/local/games:/usr/games" # base
PATH="$PATH:$HOME/bin" # secret sauce
PATH="$PATH:$HOME/.local/bin" # mostly for pip
PATH="$PATH:$HOME/.emacs.d/bin" # DOOM baby!
PATH="$PATH:$HOME/.cargo/bin" # rust
if [ "$hostname" = "planetx" ]; then
PATH="$PATH:$HOME/opt/riscv/bin" # RISC-V Newlib cross-compiler
elif [ "$hostname" = "shorefall" ]; then
PATH="$PATH:$HOME/bin/jdk-11.0.10+9/bin" # JavaTM
PATH="$PATH:$HOME/bin/android-studio/bin" # Android Studio
#PATH="$PATH:/var/lib/snapd/snap/bin" # snapd
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:/Library/Apple/usr/bin"
fi
# apply work settings
[ -f "$HOME"/.bash_work_stuff ] && source "$HOME"/.bash_work_stuff
export PATH
#export GDK_DPI_SCALE=1.5 # for scaling GTK applications, may sometimes want
export BROWSER="firefox"
if [[ "$operating_system" = "macOS" || "$operating_system" = "BSD" ]] ; then
export CLICOLOR=1
fi
# use 'nvim' if it exists on system
if [ command -v nvim &> /dev/null ]; then
export EDITOR='vim'
else
export EDITOR='nvim'
alias vim='nvim '
fi
# 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
############################## user functions ##############################
# 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
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
}
# 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
}
# prune_bash_history - remove certain frequently-used/"clutter" command entries
# from .bash_history
prune_bash_history() {
sed 's/^l$//g ; s/^ll$//g ; s/^la$//g ; s/^lla$//g ; s/^cl$//g ; s/^range$//g ; s/^e$//g ; s/^q$//g ; /^$/d ' \
"$HOME"/.bash_history > \
"$HOME"/.bash_history-BAK
if [ -f "$HOME"/.bash_history-BAK ]; then
mv "$HOME"/.bash_history-BAK "$HOME"/.bash_history
else
echo "[ERROR] .bash_history-BAK was not created. Aborting..."
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
}
sync-src-to-dest() {
if [ ! "$#" = 2 ]; then
echo "A useful rsync helper, semi-unsafe if used thoughtlessly"
echo "usage: $0 [src-dir/] [dest-dir/]"
return 1
fi
rsync -avhcz -e ssh --stats --progress --delete --force "$1" "$2"
}
# STILL has one edge case (if argument contains '..' AND goes through symlink)
mkcd() {
case "$1" in /*) :;; *) set -- "./$1";; esac # for weird directories "-xx/"
mkdir "$1" && cd "$1"
}
# create executable file containing 'sh' shebang
touch-script() {
if [ "$#" = 0 ]; then
echo "usage: $0 [path(s) to one or more script file(s) to init...] "
return 1
else
touch "$@"
chmod +x "$@"
for f in "$@"; do
printf '#!/bin/sh\n\n' > "$f"
done
fi
}
# pick file path with fzf, open in $EDITOR
fzvim() {
file_path="$(fzf)"
if [ -e "$file_path" ]; then
"$EDITOR" "$file_path"
elif [ "$file_path" = "" ]; then # no entries chosen from fzf
: # do nothing
else
echo "$file_path not found."
fi
}
############################## aliases ##############################
# 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
alias df='df -h '
alias fucking='sudo '
alias goddammit=' '
alias bottom='top '
alias bc='bc -l ' # makes bc use 'scale=20' by default
alias rot13="tr 'A-Za-z' 'N-ZA-Mn-za-m' "
alias iso-date='date --iso-8601=s '
alias now='date +%s '
alias gdb='gdb -q ' # muzzle gdb
alias local_webserver='python -m http.server 8001 ' # can change port number
alias hx='/usr/lib/helix/hx '
# config editing aliases
alias bashrc="$EDITOR ~/.bashrc"
alias vimrc="$EDITOR ~/.vimrc"
alias init_lua="$EDITOR ~/.config/nvim/init.lua"
alias helix_config="$EDITOR ~/.config/helix/config.toml"
alias nanorc="nano ~/.config/nano/nanorc" # just for fun!
alias i3config="$EDITOR ~/.config/i3/config && i3-msg reload && i3-msg restart"
# TODO: sway config
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
[ "$operating_system" = "linux" ] && alias spellcheck='aspell check '
case "$operating_system" in
macOS) color_flag='-C ' ;;
BSD) color_flag='--color=auto ' ;;
linux) color_flag='--color=auto ' ;;
*) color_flag='--color=auto ' ;; # assume GNU ls
esac
alias ls="ls $color_flag "
alias l="ls $color_flag "
alias ll="ls -lh $color_flag "
alias la="ls -A $color_flag "
alias lla="ls -lhA $color_flag "
if [[ "$operating_system" = linux || "$operating_system" = other ]]; then
alias grep='grep --color=auto '
alias egrep='egrep --color=auto '
alias fgrep='fgrep --color=auto '
fi
############################## bash completion ##############################
# 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
. "$HOME/.cargo/env" # should already be in .bash_profile, but just in case
# uncomment below to profile bashrc startup in /tmp/bashstart.SEC.NANOSEC
#set -x
#exec 2>&3 3>&-
|