dotfiles/.config/bashrc.d/10_common.sh

131 lines
2.9 KiB
Bash

#
# ~/.config/bashrc.d/10_common.sh -- Collection of common bash aliases and functions
#
# Shortcut aliases
alias sorth='sort -hr'
alias ps-grep='ps -ef | grep'
alias dmesg='sudo dmesg'
# Alias expansion for sudo
# https://unix.stackexchange.com/questions/148545/why-does-sudo-ignore-aliases/148548#148548
alias sudo='sudo '
# sudo shortcuts for getting root shell or editing files
alias sui='sudo -i'
alias suvim='sudo -e'
# Stop Ctrl-S from doing terminally things in vim/vimdiff
vim() {
local STTYOPTS="$(stty --save)"
stty stop '' -ixoff
command vim "$@"
stty "$STTYOPTS"
}
vimdiff() {
local STTYOPTS="$(stty --save)"
stty stop '' -ixoff
command vimdiff "$@"
stty "$STTYOPTS"
}
# yay without parameters does "pacman -Syu" but I don't want it to do this
yay() {
[[ $# -gt 0 ]] || { echo "error: no operation specified (use -h for help)"; return 1; }
command yay "$@"
}
# Create and cd to temporary directory ~/tmp/tmp.YYYYMMDD.XXXXXX or ~/tmp/tmp.$1.XXXXXX if an argument is given
function cdtmp() {
cd $(mktemp -d -p ~/tmp tmp.${1:-$(date +%Y%m%d)}.XXXXXX)
}
# Make dir and cd into it (from Mara)
function mkcd() {
mkdir -p "$1"
cd "$1"
}
# cd into dir + ll (ls -la)
function cdll() {
cd "$1"
ls -la
}
# Show content of something, `ls` for dirs (and special files), `cat` for files
function lscat() {
[[ $# -eq 0 ]] && ls -la && return
printnewline=""
for f in "$@"; do
[[ $printnewline ]] && echo
printnewline=1
if [[ -f $f ]]; then
[[ $# -gt 1 ]] && echo "-- cat \"$f\""
cat "$f"
else
[[ $# -gt 1 ]] && echo "-- ls \"$f\""
ls -la "$f"
fi
done
}
# Shortcut for lscat (either c for cat or c for "see")
alias c='lscat'
function calc() {
echo "$@" | bc
}
# termbin.com: terminal pastebin
alias termbin='nc termbin.com 9999'
# Dump arguments with newlines and quotes
function dumpargs() {
for a in "$@"; do
echo "\"$a\""
done
}
# Loop sudo to avoid entering password again
function sudoloop() {
# Run once in foreground to enter password
sudo -v || return 1
# Run loop in background
{ while true; do
sudo -v || return 1
sleep 60
done } &
}
# Shortcuts to encode/decode strings with base64
function b64enc() {
echo -n "$1" | base64 -w 0
echo
}
function b64dec() {
echo -n "$1" | base64 -d
echo
}
# Shortcuts for dig with less output
alias digs='dig +short'
alias digrr='dig +noall +answer'
# Resolve a hostname to its IP and back to a hostname via reverse DNS
function dig-reverse() {
dig +short -x $(dig +short $@)
}
# Print all information in lsblk in JSON for better readability
alias lsblkjson='lsblk -O --json'
# Readline macro Ctrl-Alt-L: Append "| less" to current command
bind '"\e\C-l": "\C-e | less"'
# Readline macro Ctrl-Alt-B: Append ">/dev/null 2>&1 &" to current command
bind '"\e\C-b": "\C-e >/dev/null 2>&1 &"'
# Shortcut for shuriken build script (assumes it's in $PWD/tools/shuriken.py)
alias shuriken=./tools/shuriken.py