Files
profile/.rc.d/aliases.sh

213 lines
6.4 KiB
Bash
Executable File

# Helper: define alias only if command doesn't already exist
ensure_alias() {
local name="$1" cmd="$2"
if ! command -v "$name" >/dev/null 2>&1; then
alias "$name"="$cmd"
fi
}
is_macos() { [ "$(uname)" = "Darwin" ]; }
is_linux() { [ "$(uname)" = "Linux" ]; }
is_wsl() {
# Detect WSL1/WSL2/WSLg kernels
case "$(uname -r)" in
*Microsoft*|*microsoft*|*WSL2*|*WSLg*) return 0 ;;
esac
return 1
}
if is_macos; then alias ls='ls -G'
elif is_linux; then alias ls='ls --color=auto'; fi
alias lsd='ls -d'
alias ll='ls -l'
alias lld='ls -l -d'
alias lsg='ls | grep'
alias llg='ls -l | grep'
if is_macos; then alias ln='ln -ivw'
elif is_linux; then alias ln='ln -iv'; fi
if is_linux; then
alias grep='grep --color=auto'
alias fgrep='grep --color=auto -F'
alias egrep='grep --color=auto -E'
else
alias fgrep='grep -F'
alias egrep='grep -E'
fi
alias slash='ls -SlAhr'
alias slasher='ls -SlAh'
alias slashed='ls -SlAhrd'
ds() {
du -h -d 1 "$@" | sort -h | tee "$HOME/dirsize"
}
dud() {
du -h -d 1 "$@" 2>/dev/null | sort -h
}
dudr() {
du -h -d 1 "$@" 2>/dev/null | sort -h -r
}
alias mvn='mv -n'
gmv() { mv "$@" "$HOME/Google Drive/My Drive/"; }
gmvn() { mv -n "$@" "$HOME/Google Drive/My Drive/"; }
omv() { mv "$@" "$HOME/OneDrive/"; }
omvn() { mv -n "$@" "$HOME/OneDrive/"; }
alias mv='mv -i'
alias rm='rm -i'
alias rs='rsync -avhz --progress'
alias rsfat='rsync -avhz --progress --no-perms --no-owner --no-group --no-times'
rscheck() {
rsync -avhzc --itemize-changes --dry-run "$@" |
grep "^>f[^+].*[sc]"
}
alias md=mkdir
alias rd=rmdir
# Git shortcuts
alias it=git
alias rsgit="rsync -avhz --no-perms --exclude='.git' --exclude='.vscode'"
# Bazel shortcuts
alias refresh_bazel='bazel run @hedron_compile_commands//:refresh_all'
alias bb='bazel build //...'
alias bt='bazel test //...'
alias bq='bazel query //...'
alias br='bazel run @hedron_compile_commands//:refresh_all'
alias cbb='clear && bazel build //...'
alias cbt='clear && bazel test //...'
alias cbq='clear && bazel query //...'
# Utilities
alias iwyu='include-what-you-use'
show_android_emulators() {
ps aux | head -n 1
ps aux | grep avd | sed '$d'
}
kill_all_android_emulators() {
adb devices |
awk '/emulator/{print $1}' |
while read -r dev; do adb -s "$dev" emu kill; done
}
alias freespace="df -I -h -T nodevfs | grep -i -v -E '/\.timemachine/|/System/Volumes/|/com\.apple\.TimeMachine\.localsnapshots/' | sed -e 's/ / /g'"
alias fs="df -I -h -T nodevfs | grep -i -v -E '/\.timemachine/|/System/Volumes/|/com\.apple\.TimeMachine\.localsnapshots/' | sed -e 's/ / /g'"
alias hashes='md5deep -r'
# Mac equivalent of lsusb on linux
if is_macos; then alias lsusb='system_profiler SPUSBDataType'; fi
alias tea='tee -a'
find_recent() {
find . "$@" -exec ls -ltr "{}" +
}
mount_nas() {
mkdir -p "$HOME/nas/home"
remote_home=$(ssh deepthought "printf %s \"\$HOME\"")
sshfs "deepthought:$remote_home" "$HOME/nas/home"
}
# pbcopy
if ! command -v pbcopy >/dev/null 2>&1; then
if command -v wl-copy >/dev/null 2>&1; then
ensure_alias pbcopy 'wl-copy'
elif command -v xclip >/dev/null 2>&1; then
ensure_alias pbcopy 'xclip -selection clipboard'
elif command -v xsel >/dev/null 2>&1; then
ensure_alias pbcopy 'xsel --clipboard --input'
elif is_wsl && command -v clip.exe >/dev/null 2>&1; then
ensure_alias pbcopy 'clip.exe'
fi
fi
# pbpaste
if ! command -v pbpaste >/dev/null 2>&1; then
if command -v wl-paste >/dev/null 2>&1; then
ensure_alias pbpaste 'wl-paste'
elif command -v xclip >/dev/null 2>&1; then
ensure_alias pbpaste 'xclip -selection clipboard -o'
elif command -v xsel >/dev/null 2>&1; then
ensure_alias pbpaste 'xsel --clipboard --output'
elif is_wsl && command -v powershell.exe >/dev/null 2>&1; then
ensure_alias pbpaste 'powershell.exe -noprofile Get-Clipboard'
fi
fi
log_and_do() {
printf '# %s\n' "$1" >> "$2"
bash -c "$1"
}
# In Development
# From Bash on Ubuntu
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
#alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alert() {
local last_cmd exit_status
exit_status=$?
# Get last command text
if [[ -n "$ZSH_VERSION" ]]; then
last_cmd=$(fc -ln -1)
else
last_cmd=$(history | tail -n1 | sed 's/^[ ]*[0-9]\+[ ]*//')
fi
# macOS notification
if is_macos; then
terminal-notifier -message "$last_cmd" \
-title "Command finished (exit $exit_status)"
return
fi
# Linux notification
if command -v notify-send >/dev/null 2>&1; then
notify-send "Command finished (exit $exit_status)" "$last_cmd"
fi
}
# SSH-Agent setup
sshgo() {
# Start agent only if this shell doesn't already have one
if [[ -n "$SSH_AUTH_SOCK" && -n "$SSH_AGENT_PID" ]] &&
ps -p "$SSH_AGENT_PID" >/dev/null 2>&1; then
echo "ssh-agent already running for this session."
else
eval "$(ssh-agent -s)"
fi
# Load keys only if not already loaded
for key in "$HOME"/.ssh/id_*; do
[[ -f "$key" ]] || continue
[[ "$key" == *.pub ]] && continue
fingerprint=$(ssh-keygen -lf "$key" | awk '{print $2}')
if ssh-add -l 2>/dev/null | grep -q "$fingerprint"; then
continue
fi
ssh-add "$key"
done
}
install_local_bin() {
sudo install -m 755 -o root -g root "$1" /usr/local/bin
}
install_user_bin() {
mkdir -p "$HOME/bin"
cp "$1" "$HOME/bin/"
}
# TODO: move these and the bazel ones to separate .sh files.
alias build_and_test='cmake -S . -B build && cmake --build build -- -j$(nproc) && ctest --test-dir build --output-on-failure'
alias inspect-video='mediainfo --Inform="Video;Codec: %Format%\nProfile: %Format_Profile%\nResolution: %Width%x%Height%\nBitrate: %BitRate/String%\nBitrate mode: %BitRate_Mode%\nFrame rate: %FrameRate% fps\n"'
alias wget-rip='wget --page-requisites --convert-links --adjust-extension --no-clobber --span-hosts'
alias wget-rip='wget --page-requisites --convert-links --adjust-extension --no-clobber --span-hosts -r -l1'
alias wget-rip-lite='wget --page-requisites --convert-links --adjust-extension --no-clobber --span-hosts -r -l1 -A jpg,jpeg,png,gif,html,css,ttf,otf'
alias mount-nas='mkdir -p ~/nas/tom && sshfs deepthought:/mnt/Data/Private/Homes/tom ~/nas/tom'
alias installlocalbin='function f() { sudo install -m 755 -o root -g root "$1" /usr/local/bin ; unset -f f; } f'
alias installuserbin='function f() { mkdir -p ~/bin ; cp "$1" ~/bin/ ; unset -f f; } f'