2021-09-18 21:04:50 +00:00
|
|
|
|
# Shell aliases & functions for Zsh, almost all work for Bash too
|
2021-12-11 01:22:58 +00:00
|
|
|
|
# zsh helpers {{{1
|
2021-12-11 01:20:05 +00:00
|
|
|
|
if test "$ZSH_NAME" = "zsh"
|
|
|
|
|
then
|
|
|
|
|
alias -g ___='"$(eval "$(fc -ln -1)" | tail -n 1)"'
|
2022-07-05 09:40:11 +00:00
|
|
|
|
alias -g B="| xargs bat"
|
2021-12-11 01:20:05 +00:00
|
|
|
|
alias -g G="| grp"
|
2022-04-28 16:15:51 +00:00
|
|
|
|
alias -g X="| xargs -d '\n' -L 1"
|
2021-12-11 01:20:05 +00:00
|
|
|
|
alias -g X1="| xargs -d '\n' -n 1"
|
|
|
|
|
alias -g XC="| xclip -selection clipboard"
|
|
|
|
|
alias -g L="--color=always | ${PAGER:-less}"
|
2022-05-10 13:23:01 +00:00
|
|
|
|
alias -g T=" | tree -C --fromfile . | less -F"
|
2021-12-11 01:22:58 +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
|
|
|
|
|
|
|
|
|
|
writecompletion() {
|
|
|
|
|
echo "#compdef $1" > "_$1"
|
|
|
|
|
$EDITOR "_$1"
|
|
|
|
|
}
|
2021-12-11 01:20:05 +00:00
|
|
|
|
else
|
2021-12-11 01:22:58 +00:00
|
|
|
|
compdef() { true; }
|
2020-10-14 12:43:50 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2021-12-11 01:22:58 +00:00
|
|
|
|
# data directory aliases {{{1
|
2020-12-23 12:43:15 +00:00
|
|
|
|
xdh="$XDG_DATA_HOME"
|
|
|
|
|
xch="$XDG_CONFIG_HOME"
|
2021-11-07 05:00:29 +00:00
|
|
|
|
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)
|
2022-05-10 07:25:01 +00:00
|
|
|
|
d5=$(builtin cd $DATA/5* && pwd)
|
2021-01-11 11:25:23 +00:00
|
|
|
|
fi 2>/dev/null
|
2020-12-23 12:43:15 +00:00
|
|
|
|
|
2020-10-07 19:45:01 +00:00
|
|
|
|
ulimit -c unlimited # Enable core dumps
|
2020-03-19 09:52:15 +00:00
|
|
|
|
|
2020-10-14 12:43:50 +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-11-25 12:57:02 +00:00
|
|
|
|
# System help {{{1
|
2021-09-18 21:04:50 +00:00
|
|
|
|
|
|
|
|
|
compdef help=man
|
|
|
|
|
alias info='info --vi-keys'
|
|
|
|
|
|
2022-03-25 08:14:30 +00:00
|
|
|
|
h() {
|
|
|
|
|
help "$@"
|
|
|
|
|
# TODO resolve aliases in 'h' alias
|
|
|
|
|
# TODO call 'wh' on scripts instead
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 01:01:01 +00:00
|
|
|
|
xtrace() {
|
|
|
|
|
set -x
|
|
|
|
|
"$@"
|
|
|
|
|
set +x
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# Shows source code for command, resolving nested aliases
|
2020-11-26 19:32:26 +00:00
|
|
|
|
wh() {
|
2022-06-27 11:01:19 +00:00
|
|
|
|
res=$(which "$@") || return $?
|
2021-09-16 12:18:26 +00:00
|
|
|
|
if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
|
|
|
|
|
then echo "$res" | bat --style=plain --language=sh &&
|
2022-05-31 23:04:05 +00:00
|
|
|
|
tool="$(echo "$res" | head -1 | cut -d' ' -f$(expr 5 '&' "$res" : ".*to \(sudo\|noglob\) " '|' 4) | cut -d'(' -f2)"
|
2021-11-04 11:24:23 +00:00
|
|
|
|
wh $(test $tool = $1 && echo "-p") $tool
|
2021-07-06 22:48:35 +00:00
|
|
|
|
# use command which for other shells
|
2022-06-27 20:21:33 +00:00
|
|
|
|
else test -r "$res" && b -- --language=sh "$res" || echo "$res" | bat --style=plain --language=sh
|
2021-07-06 22:48:35 +00:00
|
|
|
|
fi
|
2020-11-26 19:32:26 +00:00
|
|
|
|
}
|
2021-07-02 16:32:09 +00:00
|
|
|
|
compdef wh=which
|
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# ZSH completion and stuff {{{1
|
2021-12-15 11:34:32 +00:00
|
|
|
|
alias rs="reset && source $HOME/.zshenv && exec $SHELL"
|
2022-05-03 09:28:22 +00:00
|
|
|
|
alias hist='print -z $(history | grep -v "killm " | 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
|
2021-11-02 13:54:45 +00:00
|
|
|
|
# Exit code 1 - no change, 2 - file not found
|
2020-06-08 09:41:03 +00:00
|
|
|
|
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";;
|
2021-12-14 18:46:28 +00:00
|
|
|
|
(env) file="$HOME/.zshenv";;
|
2022-03-15 16:24:43 +00:00
|
|
|
|
(-f) term=$2
|
|
|
|
|
grepfile="$(grep --recursive "^ *\($term()\|alias[^=]* $term=\)" $CONFIG_SHELLS -n -m 1)"
|
2021-10-31 18:28:46 +00:00
|
|
|
|
file="$(echo "$grepfile" | cut -d':' -f1)"
|
2021-12-02 22:47:44 +00:00
|
|
|
|
line="$(echo "$grepfile" | cut -d':' -f2)"
|
|
|
|
|
test -f "$file" || return 2;;
|
2021-10-27 08:07:27 +00:00
|
|
|
|
(*) file="$(find $CONFIG_SHELLS -name "$1*" | head -1 | grep . || echo "$CONFIG_SHELLS/$1")";;
|
2020-09-04 10:09:08 +00:00
|
|
|
|
esac
|
2021-12-02 22:47:44 +00:00
|
|
|
|
test -f "$file" && checksum="$(md5sum "$file")"
|
2022-06-27 11:01:19 +00:00
|
|
|
|
$EDITOR "$(test "$line" && case "$EDITOR" in (nvim) echo "+normal! ${line}ggzx";; (*vi*) echo "+$line";; (emacs*|*/emacs*) echo "+${line}";; esac || echo "--")" "${file%:*}"
|
2021-12-14 18:46:28 +00:00
|
|
|
|
test -s "$file" || return 1
|
|
|
|
|
test "$checksum" != "$(md5sum $file)" && rs
|
2020-09-04 10:09:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 18:28:46 +00:00
|
|
|
|
# Edit a file in the PATH
|
|
|
|
|
edbin() {
|
2022-04-12 16:59:02 +00:00
|
|
|
|
if f="$(which $1 2>/dev/null)" && test -f "$f"
|
|
|
|
|
then edit "$f"
|
|
|
|
|
else edshell -f "$1"
|
|
|
|
|
test $? != 2 || edit "$HOME/.local/bin/scripts/$1"
|
|
|
|
|
fi
|
2021-10-31 18:28:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# Task management & time tracking {{{1
|
2020-10-23 12:00:41 +00:00
|
|
|
|
t() {
|
2021-05-05 06:55:44 +00:00
|
|
|
|
if test "$#" -eq 0 && which tn >/dev/null
|
|
|
|
|
then tn
|
|
|
|
|
else
|
|
|
|
|
if test "$1" = "do"
|
2021-12-11 01:18:11 +00:00
|
|
|
|
then shift && test "$1" -gt 0 2>/dev/null && task mod sched:today "$@" || task add sched:today "$@"
|
2021-05-05 06:55:44 +00:00
|
|
|
|
else task "$@"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2020-10-23 12:00:41 +00:00
|
|
|
|
}
|
2020-06-08 09:41:03 +00:00
|
|
|
|
alias tw='timew'
|
2021-09-16 12:18:26 +00:00
|
|
|
|
# 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
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# Systemd {{{1
|
2020-09-04 10:09:08 +00:00
|
|
|
|
|
|
|
|
|
alias syslog='less +F /var/log/syslog'
|
|
|
|
|
|
2020-06-08 09:41:03 +00:00
|
|
|
|
alias sc='sudo systemctl'
|
2022-03-25 08:14:30 +00:00
|
|
|
|
alias sce='sudo systemctl enable --now' # TODO or show log
|
2021-10-31 18:28:46 +00:00
|
|
|
|
alias scd='sudo systemctl disable --now'
|
2021-05-02 18:32:06 +00:00
|
|
|
|
scs() {
|
2022-03-15 16:24:43 +00:00
|
|
|
|
(
|
|
|
|
|
export SYSTEMD_COLORS=true
|
2022-01-31 13:38:37 +00:00
|
|
|
|
systemctl --user status "$1" 2>/dev/null ||
|
|
|
|
|
systemctl --user status "*$1*"
|
2022-03-15 16:24:43 +00:00
|
|
|
|
sudo -E systemctl status "$1" 2>/dev/null ||
|
|
|
|
|
sudo -E systemctl status "*$1*"
|
|
|
|
|
) | less -F
|
2021-05-02 18:32:06 +00:00
|
|
|
|
}
|
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-05-02 18:32:06 +00:00
|
|
|
|
|
2021-11-02 12:00:02 +00:00
|
|
|
|
# Reload or restart matching systemctl service
|
2022-05-16 21:47:19 +00:00
|
|
|
|
unalias scr 2>/dev/null
|
2021-11-02 12:00:02 +00:00
|
|
|
|
scr() {
|
2022-01-17 19:52:51 +00:00
|
|
|
|
echo -n "User: "
|
|
|
|
|
systemctl --user reload-or-restart "$1" ||
|
|
|
|
|
{ echo -n "System: " && sudo systemctl reload-or-restart "$1"; } &&
|
|
|
|
|
echo "Successful reload"
|
2021-11-02 12:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 19:04:26 +00:00
|
|
|
|
# Restart matching systemctl service with time for adjustments inbetween
|
2021-05-02 18:32:06 +00:00
|
|
|
|
scb() {
|
2021-12-23 19:04:26 +00:00
|
|
|
|
systemctl --user stop --show-transaction "$1"
|
|
|
|
|
sudo systemctl stop --show-transaction "$1"
|
2021-05-05 06:55:44 +00:00
|
|
|
|
read
|
2021-12-23 19:04:26 +00:00
|
|
|
|
systemctl --user start --show-transaction "$1"
|
|
|
|
|
sudo systemctl start --show-transaction "$1"
|
2021-05-02 18:32:06 +00:00
|
|
|
|
}
|
2020-09-04 10:09:08 +00:00
|
|
|
|
|
2022-02-07 10:12:20 +00:00
|
|
|
|
alias jch='sudo SYSTEMD_LESS=FRKiM journalctl --no-hostname'
|
2022-01-25 12:55:33 +00:00
|
|
|
|
alias jcl='jch --boot --lines 15' # list - quick overview
|
|
|
|
|
alias jce='jch --pager-end' # end - all logs
|
|
|
|
|
alias jcf='jch --follow' # follow - monitor logs
|
|
|
|
|
alias jc='jcl --unit' # list unit - quick overview
|
|
|
|
|
alias jcj='jce -o json-pretty --unit' # JSON View
|
2021-01-18 19:57:59 +00:00
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# Applications {{{1
|
|
|
|
|
|
2021-11-25 12:57:02 +00:00
|
|
|
|
# Shorthands
|
|
|
|
|
alias v='edit'
|
2021-10-11 08:02:14 +00:00
|
|
|
|
alias st='synct'
|
2022-07-06 06:59:22 +00:00
|
|
|
|
alias ex='dtrx'
|
2021-10-08 11:05:58 +00:00
|
|
|
|
alias expr='noglob expr'
|
2022-01-08 22:09:29 +00:00
|
|
|
|
alias get='noglob ='
|
2021-10-08 11:05:58 +00:00
|
|
|
|
|
2021-11-25 12:57:02 +00:00
|
|
|
|
# Shortcuts
|
2022-07-06 06:59:22 +00:00
|
|
|
|
alias kc='kdeconnect-cli --refresh && kdeconnect-cli --list-devices'
|
2021-09-18 21:04:50 +00:00
|
|
|
|
alias logoff="loginctl terminate-user $USER"
|
2021-12-11 01:01:01 +00:00
|
|
|
|
alias calb='rlwrap -a bc -l'
|
2021-12-02 22:25:53 +00:00
|
|
|
|
alias dt='python -c "import time;print(time.asctime(time.localtime()))"'
|
2021-11-25 12:57:02 +00:00
|
|
|
|
|
|
|
|
|
# Process
|
|
|
|
|
alias println='printf "\n"'
|
2022-01-30 11:33:09 +00:00
|
|
|
|
alias myip="curl ifconfig.me && println && curl icanhazip.com"
|
2021-11-25 12:57:02 +00:00
|
|
|
|
alias dedup='awk '"'"'!a[$0]++'"'"
|
|
|
|
|
alias lar='last | tac'
|
|
|
|
|
alias lst='( last; last -f /var/log/wtmp.1 ) | grep -v "pts/" | tac | less +G'
|
2022-03-15 16:24:43 +00:00
|
|
|
|
alias hedgedoc="tmux kill-session -t hedgedoc; builtin cd '$d4/dev/_forks/hedgedoc' && tmux new-session -s hedgedoc -d 'yarn run dev' \; split-window -h 'nodemon --watch app.js --watch lib --watch locales --watch config.json app.js' \; ls"
|
2022-01-31 13:38:37 +00:00
|
|
|
|
|
|
|
|
|
alias rm='rm -I'
|
|
|
|
|
del() {
|
2022-07-06 08:21:14 +00:00
|
|
|
|
# TODO use current partition and clean on reboot via cron
|
|
|
|
|
trash=/tmp/trash/
|
2022-01-31 13:38:37 +00:00
|
|
|
|
#mkdir $trash
|
|
|
|
|
m "$@" $trash
|
|
|
|
|
}
|
2021-09-18 21:04:50 +00:00
|
|
|
|
|
2022-01-08 02:00:01 +00:00
|
|
|
|
u() {
|
|
|
|
|
which pacman-mirrors >/dev/null &&
|
|
|
|
|
sudo pacman-mirrors --geoip &&
|
|
|
|
|
sudo pacman -Syy
|
2022-05-10 13:23:01 +00:00
|
|
|
|
if which topgrade >/dev/null
|
|
|
|
|
then
|
2022-07-06 06:59:22 +00:00
|
|
|
|
topgrade
|
|
|
|
|
builtin cd $XDG_CONFIG_HOME/emacs && git pull --rebase && doom sync -u
|
|
|
|
|
# TODO autodetect failure in emacs and rebuild it completely
|
|
|
|
|
else yadm l
|
|
|
|
|
sudo apt update && sudo apt upgrade
|
2022-05-10 13:23:01 +00:00
|
|
|
|
fi
|
2022-01-08 02:00:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 16:24:43 +00:00
|
|
|
|
# Networking
|
2022-05-10 08:11:46 +00:00
|
|
|
|
dns() {
|
|
|
|
|
drill A @8.8.8.8 -Q "$@"
|
|
|
|
|
drill AAAA @8.8.8.8 -Q "$@"
|
|
|
|
|
}
|
2021-12-11 01:01:01 +00:00
|
|
|
|
sshl() {
|
2022-05-03 07:38:55 +00:00
|
|
|
|
test "$1" = "-a" && shift && local all=true
|
2021-12-11 01:01:01 +00:00
|
|
|
|
lemonade server -allow 127.0.0.1 &
|
2022-05-03 07:38:55 +00:00
|
|
|
|
local authcache="/var/tmp/ssh-keys"
|
2022-03-15 16:24:43 +00:00
|
|
|
|
mkdir -p "$authcache"
|
|
|
|
|
file="$authcache/$1"
|
2022-07-05 09:40:11 +00:00
|
|
|
|
test "$all" && pass scp ~/.bash_aliases "$@:"
|
|
|
|
|
if ssh -G "$1" | grep --silent "^user root$"
|
|
|
|
|
then TERM=xterm-256color pass ssh "$@"
|
|
|
|
|
else
|
|
|
|
|
test ! -e "$file" &&
|
|
|
|
|
ssh-copy-id -i "$(ssh -G "$1" |
|
|
|
|
|
grep --max-count 1 "^identityfile " |
|
|
|
|
|
cut -d " " -f2- |
|
|
|
|
|
sed "s|^~|$HOME|")" "$1" &&
|
|
|
|
|
touch "$file"
|
2021-12-23 20:39:55 +00:00
|
|
|
|
TERM=xterm-256color kitty +kitten ssh -R 2489:127.0.0.1:2489 "$@"
|
2022-07-05 09:40:11 +00:00
|
|
|
|
return $?
|
2021-12-14 18:46:28 +00:00
|
|
|
|
fi
|
2021-12-11 01:01:01 +00:00
|
|
|
|
}
|
2021-10-08 11:05:58 +00:00
|
|
|
|
|
2022-07-05 09:40:11 +00:00
|
|
|
|
alias delta="sudo systemctl restart openvpn-client@deltaPeak.service || jcl --unit openvpn-client@deltaPeak.service"
|
2022-06-13 13:49:14 +00:00
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# Listen to loopback of mic
|
|
|
|
|
alias listen='pactl load-module module-loopback; echo "Press Enter to stop"; read; pactl unload-module module-loopback'
|
|
|
|
|
|
2022-07-06 06:59:22 +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'
|
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
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"
|
2022-01-05 16:53:06 +00:00
|
|
|
|
alias mdo="pandoc -f gfm-ascii_identifiers-gfm_auto_identifiers -t org-auto_identifiers --wrap preserve"
|
2021-09-18 21:04:50 +00:00
|
|
|
|
alias mdox="xclip -o -selection clipboard | mdo | xclip -filter"
|
|
|
|
|
|
2022-05-10 08:11:46 +00:00
|
|
|
|
alias f="fossil"
|
2022-05-12 09:32:00 +00:00
|
|
|
|
alias fs="fossil status"
|
|
|
|
|
alias fc="fossil commit -v"
|
2022-05-16 21:47:19 +00:00
|
|
|
|
fdf() {
|
|
|
|
|
fossil diff "$@" | diffr --colors refine-added:none:background:0x33,0x66,0x33:bold --colors added:none:background:0x33,0x44,0x33 --colors refine-removed:none:background:0x66,0x33,0x33:bold --colors removed:none:background:0x44,0x33,0x33 | less -F
|
|
|
|
|
}
|
2022-05-10 08:11:46 +00:00
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
alias gdiff='git diff --word-diff=color --word-diff-regex=. --no-index'
|
|
|
|
|
|
2022-05-12 09:32:00 +00:00
|
|
|
|
# Default grep with some convenience flags
|
2022-01-07 12:18:35 +00:00
|
|
|
|
alias grpc='grep --color=auto --line-number --binary-files=without-match --directories=skip'
|
2022-04-04 10:26:13 +00:00
|
|
|
|
# Default grep with some niceties and ignoring case
|
|
|
|
|
alias grp='grpc --ignore-case'
|
2022-01-07 12:18:35 +00:00
|
|
|
|
# Grep recursively and paginate
|
2022-05-26 06:53:35 +00:00
|
|
|
|
# TODO remove some prefixes \([^ ]*/\)\?
|
|
|
|
|
grpr() { grp --color=always --recursive $(echo $DIRS_IGNORE | sed 's|-x |--exclude-dir |g') "$@" | less -FX; }
|
2022-01-07 12:18:35 +00:00
|
|
|
|
# Grep in shell config files
|
2022-03-15 16:24:43 +00:00
|
|
|
|
grsh() { grpr --no-ignore-case "$@" $HOME/.{ba,z}sh* $HOME/.local/bin $CONFIG_SHELLS $CONFIG_ZSH; }
|
2020-10-14 12:16:36 +00:00
|
|
|
|
|
2020-09-04 10:09:08 +00:00
|
|
|
|
# Recover stray swap files from neovim
|
2022-06-13 13:49:14 +00:00
|
|
|
|
vrec() {
|
|
|
|
|
ls "$XDG_DATA_HOME/nvim/swap/$1" | sed 's/\%/\//g' | sed 's|\(.*\)\..*|\1|' | head -1 | xargs --no-run-if-empty nvim
|
|
|
|
|
}
|
2020-11-14 01:26:30 +00:00
|
|
|
|
alias vrecd="ls $XDG_DATA_HOME/nvim/swap | head -1 | xargs -r -i mv {} /tmp"
|
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# I think this was something about recovering backup files
|
2021-12-11 01:18:11 +00:00
|
|
|
|
unv() { strings $1 | sed 's/5$//' | dedup; }
|
2020-03-08 19:18:49 +00:00
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
alias hx='sudo hexedit --maximize --color'
|
|
|
|
|
# Paginated hexyl
|
2021-12-11 01:18:11 +00:00
|
|
|
|
hex() { hexyl "$@" | "${PAGER:-less}"; }
|
2021-06-08 18:44:42 +00:00
|
|
|
|
|
2022-06-27 20:21:33 +00:00
|
|
|
|
export DICT="$XDG_DATA_HOME/dictcc"
|
|
|
|
|
alias dic="cat $DICT/dict.txt | sed '/#/d;/&/d;/^$/d' | fzy"
|
2022-06-13 20:56:05 +00:00
|
|
|
|
#fzf --tiebreak=length --bind='alt-bspace:clear-query'
|
2022-06-27 20:21:33 +00:00
|
|
|
|
alias dict="rlwrap rdictcc --directory $DICT"
|
|
|
|
|
alias dict_update="dict -i $DICT/dict.txt"
|
2020-03-08 19:18:49 +00:00
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
npm-reinstall() {
|
|
|
|
|
rm -rf $TMPDIR/react-*
|
|
|
|
|
rm -rf node_modules/
|
|
|
|
|
npm cache verify
|
|
|
|
|
npm install
|
|
|
|
|
}
|
2020-03-08 19:18:49 +00:00
|
|
|
|
|
2022-07-06 07:10:13 +00:00
|
|
|
|
# Reconnect to ONKYO since it is buggy
|
|
|
|
|
alias onkyo='bluetoothctl disconnect 00:09:B0:1D:DC:98 && sleep 1 && bluetoothctl connect 00:09:B0:1D:DC:98'
|
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# Custom tools {{{1
|
2020-03-08 19:18:49 +00:00
|
|
|
|
|
2021-12-11 01:18:11 +00:00
|
|
|
|
sedcomment() { sed -i "s/$1/#\0/" "${@:2}"; }
|
|
|
|
|
seduncomment() { sed -i "s/#\($1\)/\0/" "${@:2}"; }
|
2020-06-03 10:39:13 +00:00
|
|
|
|
|
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
|
2021-09-24 18:20:29 +00:00
|
|
|
|
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'
|
2021-07-25 19:54:27 +00:00
|
|
|
|
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##*:*:}%=*}'
|
|
|
|
|
|
2022-01-15 21:03:56 +00:00
|
|
|
|
# Kill all processes that match
|
|
|
|
|
killm() {
|
2022-03-15 16:24:43 +00:00
|
|
|
|
ps ax | grep "$1" | grep -v grep | sed 's/\([^ ]\) .*/\1/' |
|
|
|
|
|
xargs --no-run-if-empty kill --verbose "${@:2}"
|
2022-01-15 21:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# Files {{{1
|
2020-03-08 19:18:49 +00:00
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
alias l="ls -l --almost-all --human-readable --group-directories-first --file-type"
|
2020-11-12 08:54:00 +00:00
|
|
|
|
|
2021-12-02 22:25:53 +00:00
|
|
|
|
if which fd >/dev/null
|
2022-05-10 08:11:46 +00:00
|
|
|
|
then fn() { $(which fd >/dev/null && echo fd || echo fdfind) --hidden --no-ignore-vcs --one-file-system "$@" | less -F; } # [F]ind [n]o ignore
|
|
|
|
|
alias ff="noglob fn --color=always " # [F]ind [F]ile
|
|
|
|
|
compdef ff=fd
|
2022-03-15 16:24:43 +00:00
|
|
|
|
compdef fn=fd
|
2021-12-02 22:25:53 +00:00
|
|
|
|
else
|
2022-05-10 08:11:46 +00:00
|
|
|
|
alias ff='find -not -path "*.sync*" -and -not \( -name daten -prune \)'
|
2021-12-02 22:25:53 +00:00
|
|
|
|
alias f1='find -mindepth 1 -maxdepth 1'
|
|
|
|
|
fi
|
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 &&
|
2021-12-11 01:01:01 +00:00
|
|
|
|
alias lowercase="perl-rename -iv '$lowercase_transliterate'" ||
|
|
|
|
|
alias lowercase="rename -iv '$lowercase_transliterate'"
|
2020-03-08 19:18:49 +00:00
|
|
|
|
|
2021-12-14 18:46:28 +00:00
|
|
|
|
# TODO replace cp by rsync, automatically use compression for remote transfers
|
2021-11-09 04:17:51 +00:00
|
|
|
|
# rsync directory properly - suffix both dirs with / to act on contents
|
2021-12-02 22:25:53 +00:00
|
|
|
|
alias rcn='rsync -v --recursive --human-readable --links --dry-run'
|
2021-12-12 19:24:56 +00:00
|
|
|
|
rcd() { rcn --size-only "$@" | tail +2 | tree --fromfile . | less -F; }
|
2021-12-02 22:25:53 +00:00
|
|
|
|
alias rc='rcs --links --times'
|
|
|
|
|
alias rcu='rc --existing --size-only'
|
|
|
|
|
alias rcs='rsync --recursive --info=progress2,remove,symsafe,flist,del --human-readable'
|
|
|
|
|
alias dsync='rc --delete --specials'
|
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'
|
2022-05-11 10:18:47 +00:00
|
|
|
|
alias rdiffe='rdiff --existing --size-only'
|
2022-05-24 07:24:51 +00:00
|
|
|
|
compdef rcd=rsync
|
2021-07-29 14:06:56 +00:00
|
|
|
|
|
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
|
|
|
|
|
}
|
2022-05-24 07:24:51 +00:00
|
|
|
|
compdef swap=mv
|
2021-10-13 18:02:37 +00:00
|
|
|
|
|
2020-10-14 17:00:08 +00:00
|
|
|
|
# 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 "$@"
|
2020-10-14 17:00:08 +00:00
|
|
|
|
}
|
2022-05-24 07:24:51 +00:00
|
|
|
|
compdef smv=mv
|
2020-10-14 17:00:08 +00:00
|
|
|
|
|
2021-11-07 04:39:08 +00:00
|
|
|
|
# Rename the file inside its directory
|
2020-10-14 17:00:08 +00:00
|
|
|
|
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
|
2022-01-24 16:41:55 +00:00
|
|
|
|
m $1 $2
|
2020-03-08 19:18:49 +00:00
|
|
|
|
fi
|
|
|
|
|
[ $file -gt 0 -a -d $2 ] && 2="$2/$(basename $1)"
|
2022-01-24 16:41:55 +00:00
|
|
|
|
echo -n "Linking: " && ln -vsr "$2" "$1"
|
2020-03-08 19:18:49 +00:00
|
|
|
|
}
|
2022-05-24 07:24:51 +00:00
|
|
|
|
compdef mvln=mv
|
2020-03-08 19:18:49 +00:00
|
|
|
|
|
2022-05-24 07:24:51 +00:00
|
|
|
|
# Move the given file into an xdg dir (default XDG_DATA_HOME) and create a symlink
|
2021-11-07 02:50:04 +00:00
|
|
|
|
mvx() {
|
|
|
|
|
mvln "$1" "${2:-$XDG_DATA_HOME}/${1#.}"
|
2021-11-25 12:57:02 +00:00
|
|
|
|
yadm add "$1" ".config/$1" 2>/dev/null
|
2021-11-07 02:50:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 12:31:08 +00:00
|
|
|
|
# Create directory and move into it
|
|
|
|
|
mkcd() {
|
|
|
|
|
mkdir -p "$1" && cd "$1"
|
|
|
|
|
}
|
2022-05-24 07:24:51 +00:00
|
|
|
|
compdef mkcd=mkdir
|
2021-10-15 12:31:08 +00:00
|
|
|
|
|
2021-09-18 21:04:50 +00:00
|
|
|
|
# Other stuff {{{1
|
2020-03-08 19:18:49 +00:00
|
|
|
|
|
2022-07-05 09:40:11 +00:00
|
|
|
|
# This is a function rather than a script as it potentially needs to cd out of the directory
|
2021-11-09 04:17:51 +00:00
|
|
|
|
umoul() {
|
2022-03-15 16:24:43 +00:00
|
|
|
|
local arg
|
2022-07-05 09:40:11 +00:00
|
|
|
|
# get the last arg
|
2022-03-15 16:24:43 +00:00
|
|
|
|
for arg; do true; done
|
|
|
|
|
if test "$arg"
|
|
|
|
|
then mountpoint="$(test -d "$arg" && realpath "$arg" || echo "${MNT:-${XDG_RUNTIME_DIR}/mnt}/$arg")"
|
2022-01-08 02:00:01 +00:00
|
|
|
|
mountpoint "$mountpoint" 2>/dev/null || test -b "$mountpoint" ||
|
|
|
|
|
mountpoint="$(mount --show-labels | grep "$arg" | cut -d' ' -f3)"
|
2022-01-08 20:47:30 +00:00
|
|
|
|
test "$mountpoint" || return 1
|
2022-01-08 02:00:01 +00:00
|
|
|
|
else mountpoint="$PWD"
|
|
|
|
|
while test "$mountpoint" != "/"; do
|
|
|
|
|
mountpoint "$mountpoint" >/dev/null && break
|
|
|
|
|
mountpoint="$(dirname "$mountpoint")"
|
|
|
|
|
done
|
|
|
|
|
test "$(dirname "$mountpoint")" = "/" && return 1
|
|
|
|
|
fi
|
|
|
|
|
case "$PWD" in
|
2022-07-05 09:40:11 +00:00
|
|
|
|
("$mountpoint"*) builtin cd "$(dirname $mountpoint)";;
|
2022-01-08 02:00:01 +00:00
|
|
|
|
esac
|
2022-03-15 16:24:43 +00:00
|
|
|
|
# pass on all args except last
|
2022-07-05 09:40:11 +00:00
|
|
|
|
moul -u "${@:1:$(((# > 1) ? #-1 : 0))}" "$mountpoint"
|
2021-11-09 04:17:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|