#!/bin/bash if command -v pikaur &> /dev/null; then alias pacman="sudo pikaur" elif command -v yay &> /dev/null; then alias pacman="yay" else alias pacman="sudo pacman" fi alias _slides="$(which slides 2> /dev/null || echo "echo 'slides not installed'")" function helpdef { if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then echo "$2" return 0 fi return 1 } function wait-for { while ! $@; do sleep 1; done } function slides() { OLD_PATH="$PATH" PATH="$HOME/.slides-bin:$PATH" _slides $@ PATH="$OLD_PATH" } function suckto { local dest_branch="$(echo "$@" | sed 's/ /-/g')" changed_files="$(git status --porcelain=v1 2>/dev/null | wc -l)" git suck git switch -c "$dest_branch" if [[ "$changed_files" != "0" ]]; then git stash pop fi } function integrate { local from="$(realpath $1)" if [[ "$from" == "$f"* ]]; then echo "File is already in $f" return 1 fi local to="${from/${HOME}/${f}}" local sec=3 echo "mv '$from' '$to' in $sec seconds" sleep $sec mv -n "$from" "$to" || return 1 cd $DOT_DIR || return 1 ./copy.sh cd - } function ishelp { [[ "$1" == "help" ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] } function stv { local stash=0 if ishelp $1; then echo "Show the diff for the given stash. Default to 0" return fi if [[ "${1:0:1}" == "^" ]]; then stash=${#1} elif [[ "$1" != "" ]]; then stash="$1" fi git stash show -p stash@{$stash} } function hearch { history 0 | sed 's/[0-9 ]*//' | grep "$@" } function ungrep { helpdef "$1" "grep -v's all arguments from the piped input" && return 0 local output="$(cat)" for arg in "$@"; do output="$(echo "$output" | grep -v "$arg")" done echo "$output" } function kubelog { local pods="$(kubectl get pods | grep "$1")" local selection="1" if [[ "$2" != "" ]]; then selection="$2" fi local pod="$(echo "$pods" | awk '{print $1}' | head -n $selection | tail -n 1)" kubectl logs -f "$pod" } function undelete { local filename="$1" if test -f "$filename"; then echo "File already exists!" exit 1 fi # Get all commit hashes for this file # Presumably, the one that deleted it was the most recent one :D local hashes="$(git log --all --format=format:%H -- "$filename" | head -n 2)" if [[ "$(echo $hashes | wc -l)" != "2" ]]; then echo "Expected at least two commits relating to this file!" exit 1 fi # Checkout the file from the commit before it was deleted local hash="$(echo $hashes | tail -n 1)" git checkout $hash -- "$filename" } alias j='journalctl -xefu' alias hsearch='hearch' alias d='docker' alias diff='diff -u' alias tmux="tmux attach || tmux" alias caddyfile="sudo vim /etc/caddy/Caddyfile" alias cf="sudo vim /etc/caddy/Caddyfile" function download-channel { yt-dlp "$1" --format 'bestvideo[height=1080]+bestaudio/best' --download-archive archive.txt --output '%(uploader)s/%(upload_date)s.%(title)s.%(id)s.%(ext)s' --restrict-filenames --merge-output-format mkv --ignore-errors } function fin { find -iname "*$@*" } function scpace { scp -P 11275 $@ } function gamp { git add * && git commit -am "$1" && git push } function gap { git add * && git commit -a && git push } function gd { git add * && git diff --cached --word-diff=porcelain } function wf { if [[ "$@" != "" ]]; then local fzf_args="-q $@" fi echo ~/wiki/"$(rg --with-filename --no-heading . ~/wiki | sed 's@/home/sage/wiki/@@' | fzf -i $fzf_args -1 --preview='bat ~/wiki/$(echo {} | sed "s/:.*//")' | sed 's/:.*//')" } function ws { local filename="$(wf $@)" if [[ "$filename" != "" ]]; then vim $filename fi } function ww { ws $(rg --no-line-number --no-filename . ~/wiki | sed 's/[^a-zA-Z0-9]\+/\n/g' | grep -v "^$" | sort -f | uniq -ci | sort -hr | fzf -i | cut -c 9-) } if command -v bat &> /dev/null; then alias cat="bat -p" fi export FZF_DEFAULT_COMMAND='fd --type f' alias fzf="fzf --preview 'bat --style=numbers --color=always {}'" function vo { if [[ "$1" == "" ]]; then local f="$(fzf --preview "bat --style=numbers --color=always {}")" if [[ "$f" != "" ]]; then print -s "vim $f" vim $f fi else local f="$(fzf -1 -q $@ | head -n 1)" if [[ "$f" != "" ]]; then print -s "vim $f" vim $f fi fi echo $f } alias yt='yt-dlp' function y { cd ~/.ytdl; yt-dlp $@; cd - } # Mkdir should remind you to use git alias mkdir="echo 'Maybe use git init if this is a new project' && mkdir" # Use `al` to view and edit aliases alias al="vim ~/.shell_aliases && source ~/.shell_aliases && pushdot" # Sudo commands will turn red alias sudo='echo -e "\e[1;31m" && sudo' alias clip="xclip -selection clipboard" alias cim="vim" # Use `vimc` to edit ~/.vimrc alias vimc="vim ~/.vimrc && pushdot" alias cimc="vim ~/.vimrc && pushdot" # Resume session alias vims="vim -S *.vims" # Use `zz` to edit ~/.zshrc alias zz='vim ~/.zshrc && source ~/.zshrc && pushdot' alias termrc='vim ~/.config/xfce4/terminal/terminalrc' # Use `grepr` for a recursive, case-insensitive, and filtered grep # alias grepr="grep -RInsi --color=auto --exclude-dir=.git --exclude=tags" # alias grepr='grep -RInsi --color=auto --exclude-dir=.git --exclude-dir=banana_pi_kernel --exclude-dir={bpi_build_scripts, storm_dox, sam_dox} --exclude=tags --exclude=\*.{ko, o}' alias grepr='grep -RIns --color=auto --exclude-dir=.git --exclude-dir=venv --exclude-dir=node_modules --exclude=tags --exclude-dir=build --exclude-dir=.idea' alias grepi='grepr -i' alias apt="sudo apt" alias apt-get="sudo apt-get" alias eject="sudo eject" alias dpkg="sudo dpkg" alias dpi="sudo dpkg -i" alias systemctl="sudo systemctl" alias python="python3" alias py="python3" # Use `grubdate` to update grub alias grubdate="sudo update-grub" # Use `open` after `grepr` to vim a file with grep text matching $1 alias open=_open alias open2=_open2 # `pp` and `pwoeroff` -> `poweroff` alias pp=poweroff alias pwoeroff=poweroff # Use `ll` to get a detailed list of files, sorted by size alias ll='ls --color=auto -FlrSh -w 80' alias ls='ls --color=auto -F -w 80' # Using du automatically finds the total alias du='du -sh' alias tags='ctags --exclude=node_modules -f newtags -R . && mv newtags tags' if [[ "$@" != "" ]]; then $@ fi