rwxrob-dot/.bashrc

387 lines
11 KiB
Bash
Raw Normal View History

2022-02-09 07:38:10 +00:00
#!bash
2022-02-09 07:29:05 +00:00
# shellcheck disable=SC1090
case $- in
*i*) ;; # interactive
*) return ;;
2022-02-09 07:29:05 +00:00
esac
# ------------------------- distro detection -------------------------
export DISTRO
[[ $(uname -r) =~ Microsoft ]] && DISTRO=WSL2 #TODO distinguish WSL1
#TODO add the rest
# ---------------------- local utility functions ---------------------
_have() { type "$1" &>/dev/null; }
2022-02-09 07:29:05 +00:00
_source_if() { [[ -r "$1" ]] && source "$1"; }
# ----------------------- environment variables ----------------------
# (also see envx)
export LANG=en_US.UTF-8 # assuming apt install language-pack-en done
export USER="${USER:-$(whoami)}"
2022-02-09 07:29:05 +00:00
export GITUSER="$USER"
2023-05-25 17:02:44 +00:00
export FTP=242
export WEIGHT=83.7
export HEIGHT=174
export TZ=America/New_York
2022-02-09 07:29:05 +00:00
export REPOS="$HOME/Repos"
export GHREPOS="$REPOS/github.com/$GITUSER"
export DOTFILES="$GHREPOS/dot"
export SNIPPETS="$DOTFILES/snippets"
export HELP_BROWSER=lynx
export DESKTOP="$HOME/Desktop"
export DOCUMENTS="$HOME/Documents"
export DOWNLOADS="$HOME/Downloads"
export TEMPLATES="$HOME/Templates"
2024-01-03 18:05:30 +00:00
export SCRIPTS="$HOME/Scripts"
2022-02-09 07:29:05 +00:00
export PUBLIC="$HOME/Public"
export PRIVATE="$HOME/Private"
export PICTURES="$HOME/Pictures"
export MUSIC="$HOME/Music"
export VIDEOS="$HOME/Videos"
2022-02-20 00:05:55 +00:00
export PDFS="$HOME/usb/pdfs"
2022-02-09 07:29:05 +00:00
export VIRTUALMACHINES="$HOME/VirtualMachines"
export WORKSPACES="$HOME/Workspaces" # container home dirs for mounting
export ZETDIR="$GHREPOS/zet"
export ZETTELCASTS="$VIDEOS/ZettelCasts"
export CLIP_DIR="$VIDEOS/Clips"
export CLIP_DATA="$GHREPOS/cmd-clip/data"
export CLIP_VOLUME=0
export CLIP_SCREEN=0
export TERM=xterm-256color
export HRULEWIDTH=73
export EDITOR=vi
export VISUAL=vi
export EDITOR_PREFIX=vi
export GOPRIVATE="github.com/$GITUSER/*,gitlab.com/$GITUSER/*"
2023-07-09 00:42:14 +00:00
#export GOPATH="$HOME/.local/go"
2022-02-09 07:29:05 +00:00
export GOBIN="$HOME/.local/bin"
export GOPROXY=direct
export CGO_ENABLED=0
2022-08-03 20:30:32 +00:00
export PYTHONDONTWRITEBYTECODE=2
2022-02-09 07:29:05 +00:00
export LC_COLLATE=C
export CFLAGS="-Wall -Wextra -Werror -O0 -g -fsanitize=address -fno-omit-frame-pointer -finstrument-functions"
2022-05-26 02:45:11 +00:00
2022-11-27 18:26:47 +00:00
export LESS="-FXR"
2022-02-09 07:29:05 +00:00
export LESS_TERMCAP_mb="" # magenta
export LESS_TERMCAP_md="" # yellow
export LESS_TERMCAP_me="" # "0m"
export LESS_TERMCAP_se="" # "0m"
2022-02-09 07:29:05 +00:00
export LESS_TERMCAP_so="" # blue
export LESS_TERMCAP_ue="" # "0m"
2022-02-09 07:29:05 +00:00
export LESS_TERMCAP_us="" # underline
2022-05-26 02:45:11 +00:00
2022-10-08 07:27:01 +00:00
export ANSIBLE_CONFIG="$HOME/.config/ansible/config.ini"
export ANSIBLE_INVENTORY="$HOME/.config/ansible/inventory.yaml"
export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
#export ANSIBLE_STDOUT_CALLBACK=json
2024-04-07 16:18:23 +00:00
export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock
2022-02-09 07:29:05 +00:00
[[ -d /.vim/spell ]] && export VIMSPELL=("$HOME/.vim/spell/*.add")
2023-07-28 19:01:09 +00:00
# ----------------------------- PostgreSQL ----------------------------
export PGDATABASE=cowork
2022-02-09 07:29:05 +00:00
# -------------------------------- gpg -------------------------------
2022-11-10 08:44:36 +00:00
export GPG_TTY=$(tty)
2022-02-09 07:29:05 +00:00
# ------------------------------- pager ------------------------------
if [[ -x /usr/bin/lesspipe ]]; then
export LESSOPEN="| /usr/bin/lesspipe %s"
export LESSCLOSE="/usr/bin/lesspipe %s %s"
2022-02-09 07:29:05 +00:00
fi
# ----------------------------- dircolors ----------------------------
if _have dircolors; then
if [[ -r "$HOME/.dircolors" ]]; then
eval "$(dircolors -b "$HOME/.dircolors")"
else
eval "$(dircolors -b)"
fi
2022-02-09 07:29:05 +00:00
fi
# ------------------------------- path -------------------------------
pathappend() {
declare arg
for arg in "$@"; do
test -d "$arg" || continue
PATH=${PATH//":$arg:"/:}
PATH=${PATH/#"$arg:"/}
PATH=${PATH/%":$arg"/}
export PATH="${PATH:+"$PATH:"}$arg"
done
2022-08-15 15:52:41 +00:00
} && export -f pathappend
2022-02-09 07:29:05 +00:00
pathprepend() {
for arg in "$@"; do
test -d "$arg" || continue
PATH=${PATH//:"$arg:"/:}
PATH=${PATH/#"$arg:"/}
PATH=${PATH/%":$arg"/}
export PATH="$arg${PATH:+":${PATH}"}"
done
2022-08-15 15:52:41 +00:00
} && export -f pathprepend
2022-02-09 07:29:05 +00:00
# remember last arg will be first in path
pathprepend \
"$HOME/.local/bin" \
"$HOME/.local/go/bin" \
"$HOME/.nimble/bin" \
"$GHREPOS/cmd-"* \
/usr/local/go/bin \
/usr/local/opt/openjdk/bin \
/usr/local/bin \
"$SCRIPTS"
2022-02-09 07:29:05 +00:00
pathappend \
/usr/local/opt/coreutils/libexec/gnubin \
'/mnt/c/Windows' \
'/mnt/c/Program Files (x86)/VMware/VMware Workstation' \
/mingw64/bin \
/usr/local/bin \
/usr/local/sbin \
/usr/local/games \
/usr/games \
/usr/sbin \
/usr/bin \
/snap/bin \
/sbin \
/bin
2022-02-09 07:29:05 +00:00
# ------------------------------ cdpath ------------------------------
2022-02-11 17:05:13 +00:00
export CDPATH=".:$GHREPOS:$DOTFILES:$REPOS:/media/$USER:$HOME"
2022-02-09 07:29:05 +00:00
# ------------------------ bash shell options ------------------------
2022-08-15 15:52:41 +00:00
# shopt is for BASHOPTS, set is for SHELLOPTS
shopt -s checkwinsize # enables $COLUMNS and $ROWS
2022-02-09 07:29:05 +00:00
shopt -s expand_aliases
shopt -s globstar
shopt -s dotglob
shopt -s extglob
#shopt -s nullglob # bug kills completion for some
#set -o noclobber
2022-02-15 04:36:21 +00:00
# -------------------------- stty annoyances -------------------------
2023-07-01 02:41:00 +00:00
#stty stop undef # disable control-s accidental terminal stops
stty -ixon # disable control-s/control-q tty flow control
2022-02-15 04:36:21 +00:00
2022-02-09 07:29:05 +00:00
# ------------------------------ history -----------------------------
export HISTCONTROL=ignoreboth
export HISTSIZE=5000
export HISTFILESIZE=10000
set -o vi
shopt -s histappend
# --------------------------- smart prompt ---------------------------
# (keeping in bashrc for portability)
PROMPT_LONG=20
PROMPT_MAX=95
PROMPT_AT=@
__ps1() {
local P='$' dir="${PWD##*/}" B countme short long double \
r='\[\e[31m\]' g='\[\e[30m\]' h='\[\e[34m\]' \
u='\[\e[33m\]' p='\[\e[34m\]' w='\[\e[35m\]' \
b='\[\e[36m\]' x='\[\e[0m\]'
[[ $EUID == 0 ]] && P='#' && u=$r && p=$u # root
[[ $PWD = / ]] && dir=/
[[ $PWD = "$HOME" ]] && dir='~'
B=$(git branch --show-current 2>/dev/null)
[[ $dir = "$B" ]] && B=.
countme="$USER$PROMPT_AT$(hostname):$dir($B)\$ "
[[ $B == master || $B == main ]] && b="$r"
[[ -n "$B" ]] && B="$g($b$B$g)"
short="$u\u$g$PROMPT_AT$h\h$g:$w$dir$B$p$P$x "
long="$g$u\u$g$PROMPT_AT$h\h$g:$w$dir$B\n$g$p$P$x "
double="$g$u\u$g$PROMPT_AT$h\h$g:$w$dir\n$g$B\n$g$p$P$x "
if ((${#countme} > PROMPT_MAX)); then
PS1="$double"
elif ((${#countme} > PROMPT_LONG)); then
PS1="$long"
else
PS1="$short"
fi
2022-02-09 07:29:05 +00:00
}
PROMPT_COMMAND="__ps1"
# ----------------------------- keyboard -----------------------------
2022-08-15 15:52:41 +00:00
# only works if you have X and are using graphic Linux desktop
_have setxkbmap && test -n "$DISPLAY" &&
setxkbmap -option caps:escape &>/dev/null
2022-02-09 07:29:05 +00:00
# ------------------------------ aliases -----------------------------
# (use exec scripts instead, which work from vim and subprocs)
unalias -a
2023-09-11 19:31:33 +00:00
alias todo='vi ~/.todo'
2022-11-05 12:55:34 +00:00
alias ip='ip -c'
2023-07-18 03:10:05 +00:00
alias '?'=duck
alias '??'=gpt
2023-07-18 03:08:26 +00:00
alias '???'=google
2022-02-09 07:29:05 +00:00
alias dot='cd $DOTFILES'
alias scripts='cd $SCRIPTS'
alias snippets='cd $SNIPPETS'
alias ls='ls -h --color=auto'
alias free='free -h'
2022-08-03 20:30:32 +00:00
alias tree='tree -a'
2022-02-09 07:29:05 +00:00
alias df='df -h'
alias chmox='chmod +x'
2022-05-26 02:45:11 +00:00
alias diff='diff --color'
2022-02-09 07:29:05 +00:00
alias sshh='sshpass -f $HOME/.sshpass ssh '
alias temp='cd $(mktemp -d)'
alias view='vi -R' # which is usually linked to vim
alias clear='printf "\e[H\e[2J"'
2022-02-15 04:36:21 +00:00
alias c='printf "\e[H\e[2J"'
2022-02-09 07:29:05 +00:00
alias coin="clip '(yes|no)'"
alias iam=live
2022-02-20 00:06:23 +00:00
alias neo="neo -D -c gold"
2022-11-24 04:40:58 +00:00
alias more="less"
alias disclaimer="clear; now; zet view disclaimer"
2023-08-23 08:41:35 +00:00
alias main="obs scene Main"
alias tight="obs scene Closeup"
2023-09-26 16:40:28 +00:00
alias pixel="scrcpy -t -s 1A141FDF600AJ4"
2022-02-09 07:29:05 +00:00
_have vim && alias vi=vim
# ----------------------------- functions ----------------------------
2022-05-26 02:45:11 +00:00
# lesscoloroff() {
# while IFS= read -r line; do
# unset ${line%%=*}
# done < <(env | grep LESS_TERM)
# } && export -f lesscoloroff
2022-02-09 07:29:05 +00:00
envx() {
local envfile="${1:-"$HOME/.env"}"
[[ ! -e "$envfile" ]] && echo "$envfile not found" && return 1
while IFS= read -r line; do
name=${line%%=*}
value=${line#*=}
[[ -z "${name}" || $name =~ ^# ]] && continue
export "$name"="$value"
done <"$envfile"
2022-02-09 07:29:05 +00:00
} && export -f envx
[[ -e "$HOME/.env" ]] && envx "$HOME/.env"
new-from() {
local template="$1"
local name="$2"
! _have gh && echo "gh command not found" && return 1
[[ -z "$name" ]] && echo "usage: $0 <name>" && return 1
[[ -z "$GHREPOS" ]] && echo "GHREPOS not set" && return 1
[[ ! -d "$GHREPOS" ]] && echo "Not found: $GHREPOS" && return 1
cd "$GHREPOS" || return 1
[[ -e "$name" ]] && echo "exists: $name" && return 1
gh repo create -p "$template" --public "$name"
gh repo clone "$name"
cd "$name" || return 1
2022-02-09 07:29:05 +00:00
}
2022-05-26 02:45:11 +00:00
new-bonzai() { new-from rwxrob/bonzai-example "$1"; }
2022-02-09 07:29:05 +00:00
new-cmd() { new-from rwxrob/template-bash-command "cmd-$1"; }
cdz() { cd $(zet get "$@"); }
2022-02-09 07:29:05 +00:00
2022-02-20 00:07:36 +00:00
export -f new-from new-bonzai new-cmd
2022-02-09 07:29:05 +00:00
clone() {
local repo="$1" user
local repo="${repo#https://github.com/}"
local repo="${repo#git@github.com:}"
if [[ $repo =~ / ]]; then
user="${repo%%/*}"
else
user="$GITUSER"
[[ -z "$user" ]] && user="$USER"
fi
local name="${repo##*/}"
local userd="$REPOS/github.com/$user"
local path="$userd/$name"
[[ -d "$path" ]] && cd "$path" && return
mkdir -p "$userd"
cd "$userd"
echo gh repo clone "$user/$name" -- --recurse-submodule
gh repo clone "$user/$name" -- --recurse-submodule
cd "$name"
2022-02-09 07:29:05 +00:00
} && export -f clone
# ------------- source external dependencies / completion ------------
2022-09-07 18:34:57 +00:00
# for mac
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
2022-02-09 07:29:05 +00:00
owncomp=(
pdf zet keg kn yt gl auth pomo config live iam sshkey ws x clip
./build build b ./k8sapp k8sapp ./setup ./cmd run ./run
2023-08-23 08:41:35 +00:00
foo ./foo cmds ./cmds z bonzai openapi obs
2022-02-09 07:29:05 +00:00
)
for i in "${owncomp[@]}"; do complete -C "$i" "$i"; done
_have gh && . <(gh completion -s bash)
_have z && . <(z completion bash)
2023-07-16 05:35:28 +00:00
_have glow && . <(glow completion bash)
2023-07-04 18:48:02 +00:00
_have goreleaser && . <(goreleaser completion bash 2>/dev/null)
2023-07-09 00:42:14 +00:00
_have klogin && . <(klogin completion bash 2>/dev/null)
2022-02-09 07:29:05 +00:00
_have pandoc && . <(pandoc --bash-completion)
_have kubectl && . <(kubectl completion bash 2>/dev/null)
2024-01-12 22:32:54 +00:00
_have k && complete -o default -F __start_kubectl k
2023-09-26 16:40:28 +00:00
_have istioctl && . <(istioctl completion bash 2>/dev/null)
2022-02-09 07:29:05 +00:00
#_have clusterctl && . <(clusterctl completion bash)
_have kind && . <(kind completion bash)
2024-01-12 22:32:54 +00:00
_have cobra && . <(cobra completion bash)
2022-02-09 07:29:05 +00:00
_have kompose && . <(kompose completion bash)
_have helm && . <(helm completion bash)
_have minikube && . <(minikube completion bash)
_have conftest && . <(conftest completion bash)
_have mk && complete -o default -F __start_minikube mk
_have podman && _source_if "$HOME/.local/share/podman/completion" # d
_have docker && _source_if "$HOME/.local/share/docker/completion" # d
_have docker-compose && complete -F _docker_compose dc # dc
2022-02-09 07:29:05 +00:00
2022-10-08 07:27:01 +00:00
_have ansible && . <(register-python-argcomplete3 ansible)
_have ansible-config && . <(register-python-argcomplete3 ansible-config)
_have ansible-console && . <(register-python-argcomplete3 ansible-console)
_have ansible-doc && . <(register-python-argcomplete3 ansible-doc)
_have ansible-galaxy && . <(register-python-argcomplete3 ansible-galaxy)
_have ansible-inventory && . <(register-python-argcomplete3 ansible-inventory)
_have ansible-playbook && . <(register-python-argcomplete3 ansible-playbook)
_have ansible-pull && . <(register-python-argcomplete3 ansible-pull)
_have ansible-vault && . <(register-python-argcomplete3 ansible-vault)
#_have ssh-agent && test -z "$SSH_AGENT_PID" && . <(ssh-agent)
2022-10-08 07:27:01 +00:00
2022-02-09 07:29:05 +00:00
# -------------------- personalized configuration --------------------
2022-08-15 15:52:41 +00:00
2022-02-09 07:29:05 +00:00
_source_if "$HOME/.bash_personal"
_source_if "$HOME/.bash_private"
_source_if "$HOME/.bash_work"
2022-07-01 17:30:54 +00:00
_have terraform && complete -C /usr/bin/terraform terraform
_have terraform && complete -C /usr/bin/terraform tf