Splitting up my .bashrc for simplicity's sake

This commit is contained in:
David Thurstenson 2019-05-15 22:45:06 -05:00
parent e444b00e73
commit 54ebcdd409
9 changed files with 179 additions and 192 deletions

202
.bashrc
View File

@ -6,14 +6,16 @@
# #
# Reference: https://wiki.thurstylark.com/Bashrc.html # Reference: https://wiki.thurstylark.com/Bashrc.html
### MISC ### ### GENERAL CONFIG ###
# If not running interactively, don't do anything # If not running interactively, don't do anything
[[ $- != *i* ]] && return [[ $- != *i* ]] && return
HISTCONTROL=ignoreboth # Don't put duplicate lines or lines starting with space in the history. # Don't put duplicate lines or lines starting with space in the history.
HISTCONTROL=ignoreboth
shopt -s autocd # If a directory is given without any command, CD into it. # If a directory is given without any command, CD into it.
shopt -s autocd
# Some programs, such as tee(1), like to use this variable # Some programs, such as tee(1), like to use this variable
export LS_COLORS='di=34;40:ln=35;40:so=32;40:pi=33;40:ex=31;40:bd=34;46:cd=34;43:su=0;41:sg=0;46:tw=0;42:ow=0;43:' export LS_COLORS='di=34;40:ln=35;40:so=32;40:pi=33;40:ex=31;40:bd=34;46:cd=34;43:su=0;41:sg=0;46:tw=0;42:ow=0;43:'
@ -25,197 +27,13 @@ export LS_COLORS='di=34;40:ln=35;40:so=32;40:pi=33;40:ex=31;40:bd=34;46:cd=34;43
[[ -s /usr/share/doc/pkgfile/command-not-found.bash ]] && source /usr/share/doc/pkgfile/command-not-found.bash [[ -s /usr/share/doc/pkgfile/command-not-found.bash ]] && source /usr/share/doc/pkgfile/command-not-found.bash
# Enables colored Man pages: ### UTILITIES ###
man() {
env LESS_TERMCAP_mb=$'\E[01;31m' \
LESS_TERMCAP_md=$'\E[01;38;5;74m' \
LESS_TERMCAP_me=$'\E[0m' \
LESS_TERMCAP_se=$'\E[0m' \
LESS_TERMCAP_so=$'\E[38;5;246m' \
LESS_TERMCAP_ue=$'\E[0m' \
LESS_TERMCAP_us=$'\E[04;38;5;146m' \
man "$@"
}
# Do the same for awman # Source all *.sh files in ~/.bashrc.d/
awman() { # This way all my useful little utilities are easier to reference and fix
env LESS_TERMCAP_mb=$'\E[01;31m' \ for f in ~/.bashrc.d/*.sh; do source $f; done
LESS_TERMCAP_md=$'\E[01;38;5;74m' \
LESS_TERMCAP_me=$'\E[0m' \
LESS_TERMCAP_se=$'\E[0m' \
LESS_TERMCAP_so=$'\E[38;5;246m' \
LESS_TERMCAP_ue=$'\E[0m' \
LESS_TERMCAP_us=$'\E[04;38;5;146m' \
awman "$@"
}
### PROMPT ### # Source a local bashrc if it exists. This gives the ability to insert untracked
promptsetup() {
# Color definitions for prompt
local fg_brightred='\[$(tput setaf 9)\]'
local fg_blue='\[$(tput setaf 4)\]'
local fg_magenta='\[$(tput setaf 13)\]'
local fg_cyan='\[$(tput setaf 6)\]'
local fg_brightcyan='\[$(tput setaf 14)\]'
local fg_green='\[$(tput setaf 2)\]'
local reset='\[$(tput sgr0)\]'
local hostname='\h'
local mixin
# [hh:mm][username@hostname pwd]$
# Remotely, hostname is red.
[ -n "$SSH_CLIENT" ] && hostname="${fg_brightred}\h${reset}"
# If in a python venv, add venv name in green.
[ -n "$VIRTUAL_ENV" ] && mixin=" ${fg_green}$(basename $VIRTUAL_ENV)${reset}"
# If in a vcsh repo env, add repo name in magenta.
[ -n "$VCSH_REPO_NAME" ] && mixin=" ${fg_magenta}$VCSH_REPO_NAME${reset}"
PS1="${fg_blue}[\A]${fg_cyan}[${fg_brightcyan}\u${fg_cyan}@${hostname}${mixin} ${fg_cyan}\W]${reset}\$ "
}
promptsetup
### ALIASES ###
# Colorize all `ls` output:
alias ls='ls -AF --color=auto'
# Map "la" to `ls -la`
alias la='ls -laFh --color=auto'
# Colorize `grep` output
alias grep='grep --color=auto'
# Change layout of lsblk to include FSTYPE and remove MAJ:MIN, RM, and RO collumns.
alias lsblk='lsblk -o NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT'
# Always use sudo when using nmap.
alias nmap='sudo -E nmap'
# Switch $TERM temporarily (for logging into machines that don't have tmux-256color terminfo)
alias screenterm='TERM=screen-256color'
# I'm tired of managing weechat configs and multiple nicks
# Reference: https://wiki.thurstylark.com/Weechat.html
# If you don't have weechat installed, connect to the existing tmux session through mosh
[[ ! -s /usr/bin/weechat ]] && alias weechat='mosh vps -- tmux attach -dt weechat'
# If you are thurstylark-vps, connect to the existing tmux session locally
[[ "$HOSTNAME" = "thurstylark-vps" ]] && alias weechat='tmux attach -dt weechat'
### FUNCTIONS ###
# Print Dell Service Tag
getdst() {
if [[ "$1" = "-l" ]]; then
printf "http://www.dell.com/support/home/us/en/04/product-support/servicetag/%s/configuration\n" "$(getdst)"
else
sudo dmidecode -s system-serial-number
fi
}
countdown() {
local secs="$1"
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
}
# Screenshot utils
shot() {
# Usage: shot XY
local destdir="$HOME/Pictures/screenshots"
local fname="shot-$(date +%F-%T).png"
local pb="fb"
local paste msgt msgd opts
if [ -z "$1" ]; then
printf "Usage: shot XY
X: Target
Y: Destination
Valid Targets:
w Active Window
a All displays
s Mouse Selection
Valid Destinations:
f Save to file (defined in function)
p Upload to a pastebin (defined in function)
"
return
fi
# X: What to capture
case ${1:0:1} in
# Active window
w) printf "Focus target window now...\n"
opts="-i $(xdotool getactivewindow)"
msgt="active window"
;;
# All
a) msgt="all displays"
;;
# Mouse selection
s) opts="-s --noopengl"
msgt="mouse selection"
;;
*) printf "Unknown target: %s\n" "${1:0:1}"
return
;;
esac
# Y: Where to put the result
case ${1:1:1} in
# Save to file
f) msgd="file: $destdir/$fname"
;;
# Post to a pastebin
p) destdir=$destdir/pasted
msgd="pastebin"
paste=1;;
*) printf "Unknown destination: %s\n" "${1:1:1}"
return
;;
esac
# Make sure destination directory will exist
[[ ! -d "$destdir" ]] && mkdir -p "$destdir"
local fpath="${destdir}/${fname}"
# If target is active window, give a 5 second countdown before running maim
[[ "$msgt" = "active window" ]] && countdown 5
maim $opts "$fpath"
printf "Captured %s -> %s\n" "$msgt" "$msgd"
# If destination is a pastebin, do the needful
[[ "$paste" ]] && $pb "$fpath"
}
fontfind() { if [[ -z $1 || ${#1} -gt 1 ]]; then
printf "E: only one character accepted"
fi
local glyph=$1
FC_DEBUG=4 pango-view -qt "$glyph" 2>&1 | awk -F \" '/family: / { m = $2 } END { print m }'
}
vactivate() {
local path=~/.venv/$1
if [[ ! -d $path ]]; then
python -m venv --prompt "venv: $1" --system-site-packages $path
fi
source $path/bin/activate; bash; deactivate
}
# Append a local bashrc if it exists. This gives the ability to insert untracked
# modifications to this .bashrc # modifications to this .bashrc
[[ -s ~/.local.bashrc ]] && source ~/.local.bashrc [[ -s ~/.local.bashrc ]] && source ~/.local.bashrc

19
.bashrc.d/alias.sh Normal file
View File

@ -0,0 +1,19 @@
### ALIASES ###
# Colorize all `ls` output:
alias ls='ls -AF --color=auto'
# Map "la" to `ls -la`
alias la='ls -laFh --color=auto'
# Colorize `grep` output
alias grep='grep --color=auto'
# Change layout of lsblk to include FSTYPE and remove MAJ:MIN, RM, and RO collumns.
alias lsblk='lsblk -o NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT'
# Always use sudo when using nmap.
alias nmap='sudo -E nmap'
# Switch $TERM temporarily (for logging into machines that don't have tmux-256color terminfo)
alias screenterm='TERM=screen-256color'

View File

@ -0,0 +1,23 @@
# Enables colored Man pages:
man() {
env LESS_TERMCAP_mb=$'\E[01;31m' \
LESS_TERMCAP_md=$'\E[01;38;5;74m' \
LESS_TERMCAP_me=$'\E[0m' \
LESS_TERMCAP_se=$'\E[0m' \
LESS_TERMCAP_so=$'\E[38;5;246m' \
LESS_TERMCAP_ue=$'\E[0m' \
LESS_TERMCAP_us=$'\E[04;38;5;146m' \
man "$@"
}
# Do the same for awman
awman() {
env LESS_TERMCAP_mb=$'\E[01;31m' \
LESS_TERMCAP_md=$'\E[01;38;5;74m' \
LESS_TERMCAP_me=$'\E[0m' \
LESS_TERMCAP_se=$'\E[0m' \
LESS_TERMCAP_so=$'\E[38;5;246m' \
LESS_TERMCAP_ue=$'\E[0m' \
LESS_TERMCAP_us=$'\E[04;38;5;146m' \
awman "$@"
}

8
.bashrc.d/countdown.sh Normal file
View File

@ -0,0 +1,8 @@
countdown() {
local secs="$1"
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
}

6
.bashrc.d/fontfind.sh Normal file
View File

@ -0,0 +1,6 @@
fontfind() { if [[ -z $1 || ${#1} -gt 1 ]]; then
printf "E: only one character accepted"
fi
local glyph=$1
FC_DEBUG=4 pango-view -qt "$glyph" 2>&1 | awk -F \" '/family: / { m = $2 } END { print m }'
}

8
.bashrc.d/getdst.sh Normal file
View File

@ -0,0 +1,8 @@
# Print Dell Service Tag
getdst() {
if [[ "$1" = "-l" ]]; then
printf "http://www.dell.com/support/home/us/en/04/product-support/servicetag/%s/configuration\n" "$(getdst)"
else
sudo dmidecode -s system-serial-number
fi
}

27
.bashrc.d/prompt.sh Normal file
View File

@ -0,0 +1,27 @@
promptsetup() {
# Color definitions for prompt
local fg_brightred='\[$(tput setaf 9)\]'
local fg_blue='\[$(tput setaf 4)\]'
local fg_magenta='\[$(tput setaf 13)\]'
local fg_cyan='\[$(tput setaf 6)\]'
local fg_brightcyan='\[$(tput setaf 14)\]'
local fg_green='\[$(tput setaf 2)\]'
local reset='\[$(tput sgr0)\]'
local hostname='\h'
local mixin
# [hh:mm][username@hostname pwd]$
# Remotely, hostname is red.
[ -n "$SSH_CLIENT" ] && hostname="${fg_brightred}\h${reset}"
# If in a python venv, add venv name in green.
[ -n "$VIRTUAL_ENV" ] && mixin=" ${fg_green}$(basename $VIRTUAL_ENV)${reset}"
# If in a vcsh repo env, add repo name in magenta.
[ -n "$VCSH_REPO_NAME" ] && mixin=" ${fg_magenta}$VCSH_REPO_NAME${reset}"
PS1="${fg_blue}[\A]${fg_cyan}[${fg_brightcyan}\u${fg_cyan}@${hostname}${mixin} ${fg_cyan}\W]${reset}\$ "
}
promptsetup

70
.bashrc.d/shot.sh Normal file
View File

@ -0,0 +1,70 @@
# Screenshot utils
shot() {
# Usage: shot XY
local destdir="$HOME/Pictures/screenshots"
local fname="shot-$(date +%F-%T).png"
local pb="fb"
local paste msgt msgd opts
if [ -z "$1" ]; then
printf "Usage: shot XY
X: Target
Y: Destination
Valid Targets:
w Active Window
a All displays
s Mouse Selection
Valid Destinations:
f Save to file (defined in function)
p Upload to a pastebin (defined in function)
"
return
fi
# X: What to capture
case ${1:0:1} in
# Active window
w) printf "Focus target window now...\n"
opts="-i $(xdotool getactivewindow)"
msgt="active window"
;;
# All
a) msgt="all displays"
;;
# Mouse selection
s) opts="-s --noopengl"
msgt="mouse selection"
;;
*) printf "Unknown target: %s\n" "${1:0:1}"
return
;;
esac
# Y: Where to put the result
case ${1:1:1} in
# Save to file
f) msgd="file: $destdir/$fname"
;;
# Post to a pastebin
p) destdir=$destdir/pasted
msgd="pastebin"
paste=1;;
*) printf "Unknown destination: %s\n" "${1:1:1}"
return
;;
esac
# Make sure destination directory will exist
[[ ! -d "$destdir" ]] && mkdir -p "$destdir"
local fpath="${destdir}/${fname}"
# If target is active window, give a 5 second countdown before running maim
[[ "$msgt" = "active window" ]] && countdown 5
maim $opts "$fpath"
printf "Captured %s -> %s\n" "$msgt" "$msgd"
# If destination is a pastebin, do the needful
[[ "$paste" ]] && $pb "$fpath"
}

8
.bashrc.d/vactivate.sh Normal file
View File

@ -0,0 +1,8 @@
vactivate() {
local path=~/.venv/$1
if [[ ! -d $path ]]; then
python -m venv --prompt "venv: $1" --system-site-packages $path
fi
source $path/bin/activate; bash; deactivate
}