dotfiles/.config/shell/functions

371 lines
11 KiB
Plaintext
Raw Normal View History

# Shell aliases & functions for Zsh, almost all work for Bash too
# data directory aliases {{{1
if test "$ZSH_NAME" = "zsh"; then
alias -g ___='"$(eval "$(fc -ln -1)" | tail -n 1)"'
alias -g G="| grp"
alias -g X="| xargs"
2020-12-07 20:55:46 +00:00
alias -g X1="| xargs -n 1"
2021-04-22 17:37:32 +00:00
alias -g XC="| xclip -selection clipboard"
2021-05-07 10:31:14 +00:00
alias -g L="--color=always | ${PAGER:-less}"
fi
xdh="$XDG_DATA_HOME"
xch="$XDG_CONFIG_HOME"
xsh="$XDG_STATE_HOME"
2021-01-11 11:25:23 +00:00
if test -d "$DATA"; then
2021-09-27 09:18:37 +00:00
da=$(builtin cd $DATA/_* && pwd)
d1=$(builtin cd $DATA/1* && pwd)
d2=$(builtin cd $DATA/2* && pwd)
d3=$(builtin cd $DATA/3* && pwd)
d4=$(builtin cd $DATA/4* && pwd)
2021-01-11 11:25:23 +00:00
fi 2>/dev/null
ulimit -c unlimited # Enable core dumps
2020-03-19 09:52:15 +00:00
which lsb_release >/dev/null && export DIST=$(lsb_release --id | cut -d' ' -f2) || true
2021-02-15 19:57:17 +00:00
unalias rd 2>/dev/null
2020-11-16 11:29:31 +00:00
2021-10-11 08:02:14 +00:00
# System helpers {{{1
alias h='help'
compdef help=man
alias info='info --vi-keys'
alias v=edit
alias println='printf "\n"'
alias dedup='awk '"'"'!a[$0]++'"'"
# Shows source code for command, resolving nested aliases
2020-11-26 19:32:26 +00:00
wh() {
2021-05-05 06:55:44 +00:00
res=$(which "$@")
if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
then echo "$res" | bat --style=plain --language=sh &&
tool="$(echo "$res" | cut -d' ' -f$(expr 5 '&' "$res" : ".*to \(sudo\|noglob\) " '|' 4) | cut -d'(' -f2)"
wh $(test $tool = $1 && echo "-p") $tool
# use command which for other shells
2021-07-29 14:06:56 +00:00
else test -r "$res" && b --style=header "$res" || echo "$res" | bat --style=plain --language=sh
fi
2020-11-26 19:32:26 +00:00
}
compdef wh=which
pathadd() {
local IFS=":"
local result="$@"
unset IFS
cat /etc/environment | head -1 | cut -d'"' -f2 | tr ":" "\n" | while read v
do [[ " $@ " =~ " $v " ]] || result+=":$v"
done
echo PATH=\"${result}\"\\n$(cat /etc/environment | tail -n +2) | sudo tee /etc/environment
2021-04-19 08:39:08 +00:00
}
# ZSH completion and stuff {{{1
2021-11-16 09:22:56 +00:00
alias rs="reset && source ~/.zprofile && exec $SHELL"
2021-10-22 15:15:09 +00:00
alias hist='print -z $(history | tac | fzf --tiebreak=index --bind='"'"'del:execute(sed "\;$(echo {4..})$d" -i.bak $HISTFILE)'"'"' | sed "s|^ \+||" | cut -d" " -f5-)'
2021-07-06 00:00:56 +00:00
2021-10-31 18:28:46 +00:00
# Edit shell config files
# Exit code 1 - no change, 2 - file not found
CONFIG_SHELL_FUNCTIONS="${BASH_SOURCE[0]:-${(%):-%x}}"
2020-09-04 10:09:08 +00:00
edshell() {
case $1 in
2020-11-13 12:33:44 +00:00
("") file="$CONFIG_SHELL_FUNCTIONS";;
(zsh) file="$CONFIG_ZSH/.zshrc";;
(prof*) file="$HOME/.zprofile";;
(-f) grepfile="$(grep --recursive "\(^$2()\|alias $2=\)" $CONFIG_SHELLS -n -m 1)"
2021-10-31 18:28:46 +00:00
file="$(echo "$grepfile" | cut -d':' -f1)"
line="$(echo "$grepfile" | cut -d':' -f2)";;
(*) file="$(find $CONFIG_SHELLS -name "$1*" | head -1 | grep . || echo "$CONFIG_SHELLS/$1")";;
2020-09-04 10:09:08 +00:00
esac
test -f "$file" || return 2
checksum="$(md5sum "$file")"
$EDITOR "$(test "$line" && echo "+normal! ${line}ggzx" || echo "--")" "${file%:*}"
test "$checksum" != "$(md5sum $file)" && source "$HOME/.zprofile" && exec $SHELL
2020-09-04 10:09:08 +00:00
}
2021-10-31 18:28:46 +00:00
# Edit a file in the PATH
edbin() {
f="$(which $1 2>/dev/null)" && test -f "$f" && edit "$f" ||
edshell -f "$1"; test $? != 2 ||
edit "$HOME/.local/bin/scripts/$1"
2021-10-31 18:28:46 +00:00
}
# Edit zsh completion
edcomp() {
file=$(echo "$1" | sed 's/^\([^_]\)/_\1/')
$EDITOR "${fpath[-1]}/$file"
unfunction "$file" && compinit
}
compdef "_files -W ${fpath[-1]}/" edcomp
2020-09-04 10:09:08 +00:00
writecompletion() {
echo "#compdef $1" > "_$1"
$EDITOR "_$1"
}
# Task management & time tracking {{{1
t() {
2021-05-05 06:55:44 +00:00
if test "$#" -eq 0 && which tn >/dev/null
then tn
else
if test "$1" = "do"
then test "$2" -gt 0 2>/dev/null && task mod sched:today "${@:2}" || task add sched:today "${@:2}"
else task "$@"
fi
fi
}
alias tw='timew'
# Create a temporary timewarrior database for testing
2020-11-26 19:32:26 +00:00
alias twtest='( cp -r "$TIMEWARRIORDB" /tmp/tw-bak && TIMEWARRIORDB=/tmp/timewarriordb-test/$(date +%s) && mkdir -p "$TIMEWARRIORDB"/data && :> "$TIMEWARRIORDB"/timewarrior.cfg && $SHELL )'
2020-09-04 10:09:08 +00:00
# Systemd {{{1
2020-09-04 10:09:08 +00:00
alias syslog='less +F /var/log/syslog'
alias sc='sudo systemctl'
2021-03-01 11:00:43 +00:00
alias sce='sudo systemctl enable --now'
2021-10-31 18:28:46 +00:00
alias scd='sudo systemctl disable --now'
scs() {
2021-10-17 11:34:54 +00:00
systemctl --user status "*$1*" "$1" ||
sudo systemctl status "*$1*" "$1"
}
2020-12-07 20:55:46 +00:00
alias scu='systemctl --user'
2021-03-01 11:00:43 +00:00
alias scue='systemctl --user enable --now'
2021-10-31 18:28:46 +00:00
alias scud='systemctl --user disable --now'
2021-11-02 12:00:02 +00:00
# Reload or restart matching systemctl service
scr() {
systemctl --user reload "$1" ||
sudo systemctl reload "$1" ||
systemctl --user restart "$1" ||
sudo systemctl restart"$1" ||
}
# Restart matching systemctl service with chance for input inbetween
scb() {
2021-05-05 06:55:44 +00:00
systemctl --user stop -T "$1"
2021-05-18 21:40:58 +00:00
sudo systemctl stop -T "$1"
2021-05-05 06:55:44 +00:00
read
systemctl --user start -T "$1"
2021-05-18 21:40:58 +00:00
sudo systemctl start -T "$1"
}
2020-09-04 10:09:08 +00:00
alias jcl='sudo SYSTEMD_LESS=FRKi journalctl --boot --no-hostname -e'
alias jc='jcl -u'
alias jcj='jcl -o json-pretty -u'
# Applications {{{1
alias u='topgrade'
2021-10-11 08:02:14 +00:00
alias st='synct'
alias expr='noglob expr'
alias lst='( last; last -f /var/log/wtmp.1 ) | grep -v "pts/" | tac | less +G'
alias lar='last | tac'
2021-09-20 19:34:00 +00:00
alias calc='rlwrap -a bc -l'
alias logoff="loginctl terminate-user $USER"
blues() { bluedevil-sendfile $(echo "$@" | xargs -n 1 realpath | xargs -n 1 echo -n " --files") }
# Remote
alias delta="sc restart openvpn-client@deltaPeak.service"
alias sshl="lemonade server -allow 127.0.0.1 & ssh -R 2489:127.0.0.1:2489"
# Listen to loopback of mic
alias listen='pactl load-module module-loopback; echo "Press Enter to stop"; read; pactl unload-module module-loopback'
alias sqli='rlwrap sqlite3 -column -header -cmd .tables'
alias usergroups="cat /etc/passwd | cut -d':' -f1 | xargs -n 1 id"
p() { pass "$@" || pass edit "$@"; }
alias omd="(echo '#+OPTIONS: tags:nil'; xclip -o -selection clipboard) | pandoc -f org-auto_identifiers -t markdown --wrap preserve | xclip -filter"
alias mdo="pandoc -f gfm-ascii_identifiers-gfm_auto_identifiers -t org --wrap preserve"
alias mdox="xclip -o -selection clipboard | mdo | xclip -filter"
alias gdiff='git diff --word-diff=color --word-diff-regex=. --no-index'
alias grp='grep --color=auto --line-number --ignore-case --binary-files=without-match --directories=skip'
2021-11-11 19:46:39 +00:00
grpr() { grp --color=always --recursive $(echo $DIRS_IGNORE | sed 's/-x/--exclude-dir/g') "$@" | less -F }
2020-09-04 10:09:08 +00:00
# Recover stray swap files from neovim
2021-11-23 12:03:43 +00:00
alias vrec="ls $XDG_DATA_HOME/nvim/swap | sed 's/\%/\//g' | sed 's|\(.*\)\..*|\1|' | head -1 | xargs --no-run-if-empty nvim"
alias vrecd="ls $XDG_DATA_HOME/nvim/swap | head -1 | xargs -r -i mv {} /tmp"
# I think this was something about recovering backup files
unv() { strings $1 | sed 's/5$//' | dedup }
2020-03-08 19:18:49 +00:00
alias hx='sudo hexedit --maximize --color'
# Paginated hexyl
2021-06-08 18:44:42 +00:00
hex() { hexyl "$@" | "${PAGER:-less}" }
alias dic="cat $XDG_DATA_HOME/dictcc/dict.txt | sed '/#/d;/&/d;/^$/d' | fzf --tiebreak=length --bind='alt-bspace:clear-query'"
2021-09-21 15:45:29 +00:00
alias dict="rlwrap rdictcc -d $XDG_DATA_HOME/dictcc"
alias dictu="dict -i $XDG_DATA_HOME/dictcc/dict.txt"
2020-03-08 19:18:49 +00:00
alias startMinecraftServer='curl https://ipinfo.io/ip | xclip -sel clip && cd ~/daten/games/sharedgames/minecraft/server && java -jar forge-1.12.2-14.23.5.2768-universal.jar -mx 8G'
npm-reinstall() {
rm -rf $TMPDIR/react-*
rm -rf node_modules/
npm cache verify
npm install
}
2020-03-08 19:18:49 +00:00
# Custom tools {{{1
2020-03-08 19:18:49 +00:00
2020-06-03 10:39:13 +00:00
sedcomment() { sed -i 's/$1/#\0/' "${@:2}" }
seduncomment() { sed -i 's/#\($1\)/\0/' "${@:2}" }
2020-03-08 19:18:49 +00:00
updateDeps() {
name="$1"
pattern="$2"
depth="4"
case $name in
2021-05-27 20:04:26 +00:00
(gradle-wrapper.properties) depth=6;;
2020-03-08 19:18:49 +00:00
esac
shift 2
oldversion="[0-9.]\+"
while test $# -gt 1; do
case "$1" in
2021-05-27 20:04:26 +00:00
(--pattern) oldversion="$2";;
(--version) version="$2";;
2020-03-08 19:18:49 +00:00
esac
shift 2
done
echo name $name depth $depth pattern $oldversion version $version
find -maxdepth $depth -type f -name $name | while read f
do highlight $f
2020-03-08 19:18:49 +00:00
sed -i "s/\($pattern\)$oldversion/\1$version/gw /dev/stdout" $f
done
}
2020-09-04 10:09:08 +00:00
alias updateKotlin="updateDeps build.gradle.kts 'kotlin(\"jvm\") version ' --version"
2020-03-08 19:18:49 +00:00
alias updateGradle='updateDeps gradle-wrapper.properties "services.gradle.org\/distributions\/gradle-" --version'
alias updateUtils="updateDeps build.gradle.kts '"'"com.github.Xerus2000.util", ".*", "'" --pattern '[^\"]\+' --version"
2020-03-08 19:18:49 +00:00
2020-06-03 10:39:13 +00:00
# Kill all shell background processes
2020-03-08 19:18:49 +00:00
alias killbg='kill ${${(v)jobstates##*:*:}%=*}'
2020-06-03 10:39:13 +00:00
# Kil all Java processes except IDEA
# Pass "-9" to force-kill and "-q" to not output what has been killed
2020-03-08 19:18:49 +00:00
killJava() {
pgrep -f 'java' | while read id
do if [[ $(ps --no-headers -o cmd $id) != *"idea"* ]]; then
[[ "$@" == "-q" ]] || echo "killing $(ps -p $id -o pid,cmd --width 350 --no-headers)"
if [[ "$@" == "-9" ]]
then kill -9 $id
else kill $id
fi
fi; done
}
# Files {{{1
2020-03-08 19:18:49 +00:00
alias l="ls -l --almost-all --human-readable --group-directories-first --file-type"
2021-09-21 20:32:35 +00:00
alias f="noglob fdd"
fdd() { $(which fd >/dev/null && echo fd || echo fdfind) --color=always --hidden --no-ignore-vcs --one-file-system "$@" | less -F; }
compdef f=fd
#alias f='find -not -path "*.sync*" -and -not \( -name daten -prune \)'
#alias f1='find -mindepth 1 -maxdepth 1'
2020-03-08 19:18:49 +00:00
2021-04-22 17:37:32 +00:00
lowercase_transliterate="y/A-Z /a-z-/"
2021-11-19 11:01:04 +00:00
which perl-rename >/dev/null &&
alias lowercase="perl-rename -iv '$lowercase_transliterate'" ||
alias lowercase="rename -iv '$lowercase_transliterate'"
2020-03-08 19:18:49 +00:00
2021-11-09 04:17:51 +00:00
# rsync directory properly - suffix both dirs with / to act on contents
2021-11-10 08:40:54 +00:00
alias rc='rsync --recursive --info=progress2,remove,symsafe,flist,del --human-readable --links'
alias dsync='rc --delete'
alias dsynca='rc --specials --times --delete'
2021-11-09 04:17:51 +00:00
alias move='rc --remove-source-files'
2021-07-29 14:06:56 +00:00
alias rdiff='rsync --recursive --progress --delete --links --dry-run'
2021-10-13 18:02:37 +00:00
# Swap the names of two files
swap() {
test $# -eq 2 || exit 1
mv -n $1 $1.tmp
mv -n $2 $1
mv -n $1.tmp $2
}
# mv with automatic sudo if neccessary
smv() {
2020-11-13 10:58:10 +00:00
test -w "$1" && test -w "$(dirname $2)" && mv "$@" || sudo mv "$@"
}
2021-11-07 04:39:08 +00:00
# Rename the file inside its directory
mvf() {
smv "$1" "$(dirname $1)/$2"
2020-03-08 19:18:49 +00:00
}
2021-11-10 08:40:54 +00:00
# Move from $1 to $2 and create a relative symlink
2020-03-08 19:18:49 +00:00
mvln() {
file=$(test -f "$1" && echo 1 || echo 0)
if test -d $1; then
mkdir -p "$2"
2021-11-07 04:39:08 +00:00
mv -vT $1 $2
2020-03-08 19:18:49 +00:00
else
mv -v $1 $2
fi
[ $file -gt 0 -a -d $2 ] && 2="$2/$(basename $1)"
ln -vsr "$2" "$1"
2020-03-08 19:18:49 +00:00
}
2021-11-10 08:40:54 +00:00
# Move into an xdg dir (default XDG_DATA_HOME) and create a symlink
mvx() {
mvln "$1" "${2:-$XDG_DATA_HOME}/${1#.}"
2021-11-07 04:39:08 +00:00
yadm add "$1" ".config/$1"
}
# Create directory and move into it
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Other stuff {{{1
2020-03-08 19:18:49 +00:00
2021-11-09 04:17:51 +00:00
umoul() {
2021-11-10 08:40:54 +00:00
arg="${1:-$PWD}"
mountpoint="$(test -d "$arg" && realpath "$arg" || echo "${XDG_RUNTIME_DIR}/mnt/$arg")"
mountpoint "$mountpoint" 2>/dev/null || test -b "$mountpoint" ||
mountpoint="$(mount --show-labels | grep "$arg" | cut -d' ' -f3)"
2021-11-09 04:17:51 +00:00
case "$PWD" in ("$mountpoint"*) cd "$(dirname $mountpoint)";; esac
moul -u "$mountpoint"
}
2020-03-08 19:18:49 +00:00
resetdocker() {
#aptremove docker-ce
kill $(ps -e | grep docker | cut -d' ' -f2)
sudo rm -rf /var/run/docker /var/lib/docker
sudo rm /var/run/docker.*
#aptinstall docker-ce
}
2020-06-03 10:39:13 +00:00
function zipdiff() {
2020-03-08 19:18:49 +00:00
diff -W200 -y <(unzip -vql $1 | sort -k8) <(unzip -vql $2 | sort -k8) #--suppress-common-lines
}
# SWAP
2020-09-04 10:09:08 +00:00
alias memstat='free -h | awk '"'"'NR==2 {printf "Free memory:\t %s/%s\t(%d%)\n",$7,$2,$7*100/$2} NR==3 {if($2 != "0B") printf "Used swap:\t%s/%s\t(%d%)\n",$3,$2,$2*100/$3}'"'"
2020-03-08 19:18:49 +00:00
stopswap() {
2020-09-04 10:09:08 +00:00
memstat
swap_used=$(cat /proc/meminfo | grep SwapFree | awk '{print $2}')
mem_available=$(cat /proc/meminfo | grep MemAvailable | awk '{print $2}')
if test $swap_used = 0
2020-03-08 19:18:49 +00:00
then echo "No swap is in use."
2020-09-04 10:09:08 +00:00
elif test $swap_used + 100000 < $mem_available
2020-03-08 19:18:49 +00:00
then echo "Freeing swap..."
sudo swapoff -a
sudo swapon -a
2020-09-04 10:09:08 +00:00
memstat
2020-03-08 19:18:49 +00:00
else
echo "Not enough free memory!"
fi
}