-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
663 lines (587 loc) · 20.6 KB
/
functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# Shell aliases & functions for Zsh, almost all work for Bash too
# zsh helpers {{{1
if test "$ZSH_NAME" = "zsh"
then
alias -g ___='"$(eval "$(fc -ln -1)" | tail -n 1)"'
alias -g B="| xargs bat"
alias -g G="| grp"
alias -g X="| xargs -d '\n' -L 1"
alias -g X1="| xargs -d '\n' -n 1"
alias -g XC="| xclip -selection clipboard"
alias -g L="--color=always | ${PAGER:-less}"
alias -g T=" | tree -C --fromfile . | less -F"
# Edit zsh completion
edit-completion() {
local file=$(echo "$1" | sed 's/^\([^_]\)/_\1/')
filepath="${fpath[-1]}/$file"
test -f "$filepath" ||
echo "#compdef $1" >"$filepath"
$EDITOR "$filepath"
unfunction "$file" && compinit
}
compdef "_files -W ${fpath[-1]}/" edit-completion
alias comp='edit-completion'
else
# So bash does not error out on zsh completion definitions
compdef() { true; }
fi
# data directory aliases {{{1
xdh="$XDG_DATA_HOME"
xch="$XDG_CONFIG_HOME"
xsh="$XDG_STATE_HOME"
alias xdh="cd $XDG_DATA_HOME"
alias xch="cd $XDG_CONFIG_HOME"
alias xsh="cd $XDG_STATE_HOME"
if test -d "$DATA"; then
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)
d5=$(builtin cd $DATA/5* && pwd)
fi 2>/dev/null
ulimit -c unlimited # Enable core dumps
which lsb_release >/dev/null &&
export DIST=$(lsb_release --id | cut -d' ' -f2)
# Weird alias to broken color-diff
unalias diff 2>/dev/null
# Alias on some systems?
unalias rd 2>/dev/null
# System help {{{1
compdef tldr=man
compdef help=man
compdef h=help
alias info='info --vi-keys'
sudos() {
cmd=$1
shift
sudo "$(which $cmd)" "$@"
}
# Outputs the resolved alias if it is a simple one-word alias
resolvealias() {
alias "$1" 2>/dev/null | sed -n "s|$1='\?\(noglob \)\?\([-A-z]\+\)'\?\$|\2|p" | grep -m 1 . || echo $1
}
h() {
arg="$1"
local alias=$(resolvealias "$arg")
shift
help "$alias" "$@" || wh "$arg" "$@"
# TODO call 'wh' on scripts rather than passing potentially hazardous args (as in clean)
}
xtrace() {
set -x
"$@"
set +x
}
retry() {
while ! "$@"; do sleep 2; done
}
# Shows source for given command, resolving nested aliases
wh() {
local res=$(which "$@" 2>/dev/null || type "$@") || return $?
# only works in zsh, not bash
if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
then echo "$res" | bat --style=plain --language=sh &&
tool="$(echo "$res" | head -1 | 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
else test -r "$res" && b -- --language=sh "$res" || echo "$res" | bat --style=plain --language=sh
fi
}
compdef wh=which
# Find where a function was defined
def() {
autoload +X "$@"
type "$@"
}
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
}
# ZSH completion and stuff {{{1
alias rs="reset && source $HOME/.zshenv && exec $SHELL"
alias hist='print -z $(history | grep -v -e "killm " -e "netkeeper " | tac | fzf --tiebreak=index --bind='"'"'del:execute(sed "\;$(echo {4..})$d" -i.bak $HISTFILE)'"'"' | sed "s|^ \+||" | cut -d" " -f5-)'
alias es='edit-shell'
alias ec='edit-config'
alias eb='edit-bin'
CONFIG_SHELL_FUNCTIONS="${BASH_SOURCE[0]:-${(%):-%x}}"
# Fuzzy find and edit shell config files
# Exit code: 1 - no change, 2 - file not found
edit-shell() {
local file
case $1 in
("") file="$CONFIG_SHELL_FUNCTIONS";;
(zsh) file="$CONFIG_ZSH/.zshrc";;
(env) file="$HOME/.zshenv";;
(-f) term=$2
grepfile="$(grep --recursive "^ *\($term()\|alias[^=]* $term=\)" $CONFIG_SHELLS -n -m 1)"
file="$(echo "$grepfile" | cut -d':' -f1)"
line="$(echo "$grepfile" | cut -d':' -f2)"
test -f "$file" || return 2;;
(*) file="$(find $CONFIG_SHELLS -name "$1*" | head -1 | grep . || echo "$CONFIG_SHELLS/$1")";;
esac
test -f "$file" && checksum="$(md5sum "$file")"
$EDITOR "$(test "$line" && case "$EDITOR" in (nvim) echo "+normal! ${line}ggzx";; (*vi*) echo "+$line";; (emacs*|*/emacs*) echo "+${line}";; esac || echo "--")" "${file%:*}"
test -s "$file" || return 1
# Reload shell config upon change
test "$checksum" != "$(md5sum $file)" && rs
}
# Edit an executable in the PATH
edit-bin() {
local toedit="$(resolvealias "$1")"
#case "$toedit" in (*\ text*);; (*) toedit="$1";; esac
if f="$(which "$toedit" 2>/dev/null)" && test -f "$f"
then edit "$f"
else edit-shell -f "$toedit"
if test $? = 2
then local script="$HOME/.local/bin/${2:-scripts}/$1"
edit "$script" &&
chmod +x "$script"
fi
fi
}
# Task management & time tracking {{{1
t() {
if test "$#" -eq 0 && which tn >/dev/null
then tn
else
if test "$1" = "do"
then shift && test "$1" -gt 0 2>/dev/null && task mod sched:today "$@" || task add sched:today "$@"
else task "$@"
fi
fi
}
alias tw='timew'
# Create a temporary timewarrior database for testing
alias twtest='( cp -r "$TIMEWARRIORDB" /tmp/tw-bak && TIMEWARRIORDB=/tmp/timewarriordb-test/$(date +%s) && mkdir -p "$TIMEWARRIORDB"/data && :> "$TIMEWARRIORDB"/timewarrior.cfg && $SHELL )'
# Systemd {{{1
alias syslog='less +F /var/log/syslog'
alias sc='sudo systemctl'
alias sced="EDITOR=nvim sudo --preserve-env=EDITOR systemctl edit"
alias scd='sudo systemctl disable --now'
unalias scs sce 2>/dev/null
sce() {
sudo systemctl enable --now "$@" ||
sudo systemctl status "$@"
}
scs() {
(
export SYSTEMD_COLORS=true
systemctl --user status "$1" 2>/dev/null ||
systemctl --user status "*$1*"
sudo -E systemctl status "$1" 2>/dev/null ||
sudo -E systemctl status "*$1*"
) # | less -RF
}
alias scu='systemctl --user'
alias scue='systemctl --user enable --now'
alias scud='systemctl --user disable --now'
# Reload or restart matching systemctl service
unalias scr 2>/dev/null
scr() {
echo -n "User: " && systemctl --user daemon-reload && systemctl --user reload-or-restart "$@" ||
{ echo -n "System: " && sudo systemctl daemon-reload && sudo systemctl reload-or-restart "$@"; } &&
echo "Successful reload" ||
scs "$@"
}
# Restart matching systemctl service with time for adjustments inbetween
scb() {
systemctl --user stop --show-transaction "$@"
sudo systemctl stop --show-transaction "$@"
read
systemctl --user start --show-transaction "$@"
sudo systemctl start --show-transaction "$@"
}
alias jch='sudo SYSTEMD_LESS=FRKiM journalctl --no-hostname'
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
# Applications {{{1
# Shorthands
alias v='edit'
alias st='synct'
command -v dtrx >/dev/null && alias ex='dtrx'
alias expr='noglob expr'
alias get='noglob ='
alias bi='b .*ignore*'
# Shortcuts
alias kc='kdeconnect-cli --refresh && kdeconnect-cli --list-devices'
alias logoff="loginctl terminate-user $USER"
alias calb='rlwrap -a bc -l'
alias dt='python -c "import time;print(time.asctime(time.localtime()))"'
# Process
alias println='printf "\n"'
alias dedup-lines='awk '"'"'!a[$0]++'"'"
alias lar='last | tac'
alias lst='( last; last -f /var/log/wtmp.1 ) | grep -v "pts/" | tac | less +G'
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"
alias myip='curl ifconfig.me && println && curl icanhazip.com'
alias ips='ip -br address && ip route'
alias rm='rm -I'
del() {
# TODO use current partition and clean on reboot via cron
trash=/tmp/trash/
#mkdir $trash
m "$@" $trash
}
curlh() {
curl -v --location "$@" >/dev/null
}
unalias u 2>/dev/null # for bash
u() {
# Line below handy for users of netkeeper
#sudo nft list ruleset | grep -q outall && echo "Suspending netkeeper" >&2 | echo 'pausing netkeeper for sysupgrade' | netkeeper 30 2>/dev/null
if command -v pacman >/dev/null; then
if test "$(stat /etc/pacman.d/mirrorlist --printf=%y | cut -d'-' -f1-2)" != "$(date +%Y-%m)"
then
# TODO also run if location changed significantly or mirror is slow
if command -v pacman-mirrors >/dev/null
then sudo pacman-mirrors --geoip
else sudo touch /etc/pacman.d/mirrorlist
fi
sudo pacman -Syy --needed base gnupg archlinux-keyring
sudo pacman-key --populate
sudo pacman-key --refresh-keys
fi
fi
if command -v topgrade >/dev/null
then
nice -10 topgrade --disable node emacs remotes
if test -d "$XDG_CONFIG_HOME/emacs"
then
builtin cd $XDG_CONFIG_HOME/emacs
git pull --rebase
if ! true | doom sync -u
then echo "Rebuild doom from scratch?"
read result
test "$result" = "y" && rm -rf "$DOOMLOCALDIR/straight" && doom sync -u
fi
pgrep emacsclient && echo -n "^ Emacs client processes. Press enter to restart the emacs server." && read
pkill --full "emacs --daemon"
tmux new-session -d -s emacs-daemon emacs --daemon
fi
else yadm l
sudo apt update && sudo apt upgrade
fi
}
# Networking
dns() {
local dig="drill -Q"
which drill >/dev/null || dig="dig +short"
local server=1.1.1.1
# TODO allow changing DNS server with @
# TODO implement rdns via -x
for arg; do
local trimmed="${arg##*//}"
local cut="${trimmed%%/*}"
if which host >/dev/null 2>&1
then host "$cut" $server
else
$(echo $dig) A @$server "$cut"
$(echo $dig) AAAA @$server "$cut"
fi
done
}
alias sshk="$(case $TERM in (*-kitty) echo 'TERM=xterm-256color kitty +kitten';; esac) ssh"
sshl() {
test "$1" = "-a" && shift && local all=true
lemonade server -allow 127.0.0.1 &
local authcache="/var/tmp/ssh-keys"
mkdir -p "$authcache"
local file="$authcache/$1"
ssh -G "$1" | grep --silent "^user root$" &&
! [[ "$1" =~ "pve.*" ]] &&
! [[ "$1" =~ "encee.*" ]] &&
! [[ "$1" =~ "tmtgw" ]] &&
! [[ "$1" =~ "delta*" ]] &&
! [[ "$1" =~ "nc-*" ]] &&
! [[ "$1" =~ "ddns*" ]] &&
local pass=pass
test "$all" &&
find $XDG_CONFIG_HOME/bash/ \( -name aliases.bash -o -name "${1%[0-9a-z]}*.bash" \) -exec cat {} + | $pass ssh "$@" 'cat > .bash_aliases && grep -q .bash_aliases .bashrc || echo "source ~/.bash_aliases" | tee -a .bashrc' &&
echo 'Updated .bash_aliases!'
#$pass ssh "$1" 'grep -q ".bash_aliases" .bashrc || echo "source ~/.bash_aliases" >>.bashrc'
if test -n "$pass"
then pass ssh "$@"
else
test ! -e "$file" &&
ssh-copy-id -i "$(ssh -G "$1" |
grep --max-count 1 "^identityfile " |
cut -d " " -f2- |
sed "s|^~|$HOME|")" "$@" &&
touch "$file"
#TERM=xterm-256color
sshk -R 2489:127.0.0.1:2489 "$@"
fi
}
alias delta='sudo systemctl restart openvpn-client@deltaPeak.service || jcl --unit openvpn-client@deltaPeak.service'
# Listen to loopback of mic
alias listen='pactl load-module module-loopback; echo "Press Enter to stop"; read; pactl unload-module module-loopback'
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'
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-auto_identifiers --wrap preserve'
alias mdox='xclip -o -selection clipboard | mdo | xclip -filter'
alias pmd='pandoc -t markdown_strict-raw_html'
alias clr='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'
alias f='fossil'
alias fl='fossil timeline --format "[%d] %h %c <%a>"'
alias fs='fossil status'
alias fc='fossil commit -v'
fdf() {
fossil diff "$@" | clr
}
fuu() {
rm .fslckout
fossil open *.fossil --keep
}
alias gdiff='git diff --word-diff=color --word-diff-regex=. --no-index'
# Default grep with some convenience flags
alias grpc='grep --color=auto --line-number --binary-files=without-match --directories=skip'
# Default grep with some niceties and ignoring case
alias grp='grpc --ignore-case'
# Grep recursively and paginate
# TODO remove some prefixes \([^ ]*/\)\?
grpr() { grp --color=always --recursive '--exclude=*.sql' $(echo $DIRS_IGNORE | sed 's|-x |--exclude-dir |g') "$@" | cut -c-$(expr $COLUMNS \* 3) | less -FX; }
# Grep in shell config files
grsh() {
local search=$1
shift
case $1 in (-*|"");; (*) local filter=$1; shift;; esac
grpr --no-ignore-case "$search" "$@" $(test -z "$filter" &&
echo $HOME/.local/bin $CONFIG_SHELLS $CONFIG_ZSH $HOME/.{ba,z}sh* /{etc,usr/share}/{ba,z}sh* /etc/profile* ||
compgen -G "$XDG_CONFIG_HOME/$filter" -G "/usr/share/$filter" -G "/etc/$filter*" -G "$HOME/.$filter*")
}
# Recover stray swap files from neovim
vrec() {
find "$XDG_STATE_HOME/nvim/swap" -name "*$1*" -printf '%f%n' |
sed 's/\%/\//g' | sed 's|\(.*\)\..*|\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-lines; }
alias hx='sudo hexedit --maximize --color'
# Paginated hexyl
hex() { hexyl "$@" | "${PAGER:-less}"; }
export DICT="$XDG_DATA_HOME/dictcc"
dic() {
result=$(cat $DICT/dict.txt | sed '/#/d;/&/d;/^$/d' | FZF_DEFAULT_OPTS=${FZF_DEFAULT_OPTS#--exit-0} fzf --tiebreak=chunk,length --bind='alt-bspace:clear-query' --print-query --query="$1") &&
dic "$(echo "$result" | tail -1 | sed 's/\t/\n/g' | head -2 | grep --fixed-strings --invert-match --ignore-case "$(echo "$result" | head -1)" --max-count 1 || echo "$result" | head -1 | sed 's/ \W.*//')"
# Could be slightly improved using fzf -f to reproduce selection
}
#fzf --tiebreak=length --bind='alt-bspace:clear-query'
alias dict="rlwrap rdictcc --directory $DICT"
dict_update() {
local dictfile="$DICT/dict.txt"
test $# -gt 0 && case "$1" in
(*.zip) dir=$(mktemp -d) && unzip "$1" -d "$dir" && mv -v $dir/* "$dictfile" && rm -v "$1";;
(*) mv -v "$1" "$dictfile";;
esac
echo "Reading in $dictfile..."
unbuffer rdictcc --directory $DICT -i "$dictfile" | rewrite
}
npm-reinstall() {
rm -rf $TMPDIR/react-*
rm -rf node_modules/
npm cache verify
npm install
}
# 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'
alias pdfcon='TEXMF="" pdfjam -o $(date +%F).pdf'
alias pdfwor='TEXMF="" pdfjam -o $(test -d "collections" && echo "collections/")$(date +%F)_Akkorde.pdf'
# Custom tools {{{1
sedcomment() { sed -i "s/$1/#\0/" "${@:2}"; }
seduncomment() { sed -i "s/#\($1\)/\0/" "${@:2}"; }
updateDeps() {
name="$1"
pattern="$2"
depth="4"
#test $# -gt 2 || echo "Please specify a new version!"
case $name in
(gradle-wrapper.properties) depth=6;;
esac
shift 2
oldversion="[0-9.]\+"
while test $# -gt 1; do
case "$1" in
(--pattern) oldversion="$2";;
(--version) version="$2";;
esac
shift 2
done
echo name "'$name'" depth "'$depth'" pattern "'$oldversion'" version "'$version'" >&2
find -maxdepth $depth -not \( -name "*forks" -prune \) -type f -name $name |
while read f
do highlight $f
sed -i "s/\($pattern\)\($oldversion\)/\1${version:-\2}/gw /dev/stdout" $f
done
}
alias updateKotlin="updateDeps build.gradle.kts 'kotlin(\"jvm\") version \"' --version"
alias updateGradle='updateDeps gradle-wrapper.properties "services.gradle.org\/distributions\/gradle-" --version'
alias updateUtils="updateDeps build.gradle.kts '"'"com.github.Xerus2000.util", ".*", "'" --pattern '[^\"]\+' --version"
updateKarma() { rpl "karma-runner.github.io/[^/]\+/" "karma-runner.github.io/$1/"; }
# Kill all shell background processes
alias killbg='kill -9 ${${(v)jobstates##*:*:}%=*}'
# Kill all processes that match given term
killm() {
ps ax | grep "\b$1 " | grep '?' | grep -v grep | sed 's/\([^ ]\) .*/\1/' |
xargs --no-run-if-empty kill --verbose "${@:2}"
}
# Files {{{1
which exa >/dev/null &&
alias l='exa --icons --group-directories-first' &&
alias ll='l --long --git --all'
if which fd >/dev/null 2>&1
then fn() { $(command -v 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
compdef fn=fd
else
alias ff='find -not -path "*.sync*" -and -not \( -name daten -prune \) -iname'
alias f1='find -mindepth 1 -maxdepth 1'
fi
# TODO replace cp by rsync, automatically use compression for remote transfers
# rsync directory properly - suffix both dirs with / to act on contents
alias rcn='rsync -v --recursive --human-readable --links --dry-run'
rcd() { rcn --size-only "$@" | tail +2 | tree --fromfile . | less -F; }
compdef rcd=rsync
# TODO do not sync times to FAT
alias rc='rcs --links --hard-links --times'
alias rcu='rc --existing --size-only'
alias rcr='rcs --compress --size-only --times'
alias rcs='rsync --recursive --info=progress2,remove,symsafe,flist,del --human-readable'
alias dsync='rc --delete --specials'
alias move='rc --remove-source-files'
alias rdiff='rsync --recursive --checksum --delete --links --dry-run'
alias rdiffe='rdiff --existing --size-only'
# 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
}
compdef swap=mv
# mv with automatic sudo if neccessary
smv() {
test -w "$1" && test -w "$(dirname $2)" && mv "$@" || sudo mv "$@"
}
compdef smv=mv
# Rename the file inside its directory
mvf() {
smv "$1" "$(dirname $1)/$2"
}
# Move from $1 to $2 and create a relative symlink
mvln() {
local file=$(test -f "$1" && echo 1 || echo 0)
if test -d $1; then
mkdir -p "$2"
mv -vT $1 $2
else
m $1 $2
fi
[ $file -gt 0 -a -d $2 ] && 2="$2/$(basename $1)"
echo -n "Linking: " && ln -vsr "$2" "$1"
}
compdef mvln=mv
# Move the given file into an xdg dir (default XDG_DATA_HOME) and create a symlink
mvx() {
mvln "$1" "${2:-$XDG_DATA_HOME}/${1#.}"
yadm add "$1" ".config/$1" 2>/dev/null
}
# Create directory and move into it
mkcd() {
mkdir -p "$1" && cd "$1"
}
compdef mkcd=mkdir
# Other stuff {{{1
alias unmount=umoul
# This is a function rather than a script as it needs to cd up
# if the current directory is to be unmounted
umoul() {
local arg
local mnt="${MNT:-${XDG_RUNTIME_DIR}/mnt}"
# get the last arg
for arg; do true; done
case "$arg" in
(-a|--all)
mountpoints="$(mount | grep "$mnt" | cut -d' ' -f3)";;
("")
mountpoints="$(pwd | grep -v /home || echo /)"
while test "$mountpoints" != "/"; do
mountpoint "$mountpoints" >/dev/null && break
mountpoints="$(dirname "$mountpoints")"
done
test "$(dirname "$mountpoints")" != "/" ||
mountpoints="$(mount |
grep --invert-match -e " /[^m][^/]*\(/[^/]*\)\? " -e "/sys" -e "/run/user" -e "/run/docker" -e "/home" |
fzf --exit-0 | awk '{print $3}')" ||
return $?
;;
(*)
mountpoints="$(test -d "$arg" && realpath "$arg" || echo "$mnt/$arg")"
mountpoint "$mountpoints" 2>/dev/null || test -b "$mountpoints" ||
mountpoints="$(mount --show-labels | grep "$arg" | cut -d' ' -f3)"
test "$mountpoints" || return 1
;;
esac
# pass on all args except last
for mountpoint in $mountpoints; do
while true; do
case "$PWD" in
("$mountpoint"*) popd || builtin cd "$(dirname "$mountpoint")";;
(*) break;;
esac
done
moul -u "${@:1:$(((# > 1) ? #-1 : 0))}" "$mountpoint"
done
}
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
}
function zipdiff() {
diff -W200 -y <(unzip -vql $1 | sort -k8) <(unzip -vql $2 | sort -k8) #--suppress-common-lines
}
# SWAP
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}'"'"
stopswap() {
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
then echo "No swap is in use."
elif test $swap_used + 100000 < $mem_available
then echo "Freeing swap..."
sudo swapoff -a
sudo swapon -a
memstat
else
echo "Not enough free memory!"
fi
}
extrac() {
test "$#" -lt 1 && echo "$0 <archive.vma[.lzo]>..." && return 2
for var; do
arg=${var%.lzo}
out=$(basename ${arg%.vma})
lzop -x $arg.lzo
vma.py $arg $out ||
vma.py $(basename $arg) $out &&
( cd $out && find drive-* -exec qemu-img convert {} -O qcow2 $out-{}.qcow2 \; )
done
}