-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zshrc
1332 lines (1123 loc) · 26.4 KB
/
.zshrc
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# compinstall (auto) {{{
########################
zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate
zstyle ':completion:*' expand prefix suffix
zstyle ':completion:*' file-sort name
zstyle ':completion:*' ignore-parents parent pwd
zstyle ':completion:*' insert-unambiguous true
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or start typing%s
zstyle ':completion:*' list-suffixes true
zstyle ':completion:*' matcher-list '' 'm:{[:lower:]}={[:upper:]}' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' 'l:|=* r:|=*'
zstyle ':completion:*' menu select=1
zstyle ':completion:*' original true
zstyle ':completion:*' preserve-prefix '//[^/]##/'
zstyle ':completion:*' select-prompt %SScrolling %p%s
zstyle ':completion:*' squeeze-slashes true
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose false
zstyle :compinstall filename "$HOME/.zshrc"
autoload -Uz compinit
compinit
#####
# }}}
# zsh-newuser-install (auto) {{{
################################
HISTFILE=~/.zsh_history
HISTSIZE=10000000
SAVEHIST=1000000
WORDCHARS=''
setopt appendhistory autocd extendedglob nomatch notify hist_ignore_space
unsetopt beep
bindkey -v
#####
# }}}
# oh-my-zsh {{{
###############
case "$(uname)" in
Darwin)
ZSH="$HOME/.oh-my-zsh"
;;
Linux)
ZSH="/usr/share/oh-my-zsh/"
;;
esac
# Set name of the theme to load. Look in ~/.oh-my-zsh/themes/
ZSH_THEME="emmie"
# Completion settings
CASE_SENSITIVE="false"
# Case sensitive completion must be off. _ and - will be interchangeable.
HYPHEN_INSENSITIVE="true"
# Disable bi-weekly auto-update checks.
DISABLE_AUTO_UPDATE="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
HIST_STAMPS="yyyy-mm-dd"
# Don't yank or put from system clipboard.
# This makes workflows like delete + replace with clipboard rather annoying.
VI_MODE_DISABLE_CLIPBOARD="true"
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
plugins=(jsontools pip safe-paste urltools vi-mode zsh-autosuggestions)
# User configuration
# You may need to manually set your language environment
ZSH_CACHE_DIR="$HOME/.cache/zsh"
ZSH_CUSTOM="$HOME/.oh-my-zsh"
# LS_COLORS is part of oh-my-zsh, so set completion colors here
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# Source oh-my-zsh data
source "$ZSH/oh-my-zsh.sh"
# Remove unwanted aliases
unalias lsa
#####
# }}}
# pyenv {{{
###########
if which pyenv > /dev/null; then
export PYENV_SHELL=zsh
command pyenv rehash 2> /dev/null
pyenv() {
local command
command="${1:-}"
if [[ $# -gt 0 ]]; then
shift
fi
case "$command" in
rehash|shell)
eval "$(pyenv "sh-$command" "$@")"
;;
*)
command pyenv "$command" "$@"
;;
esac
}
# Use bpython when not invoking pyenv
python() { [[ $# -eq 0 ]] && bpython || "$HOME/.pyenv/shims/python" "$@"; }
python2() { [[ $# -eq 0 ]] && bpython2 || env python2 "$@"; }
python3() { [[ $# -eq 0 ]] && bpython3 || "$HOME/.pyenv/shims/python3" "$@"; }
elif which bpython > /dev/null; then
# Use regular bpython
python() { bpython "$@"; }
python2() { bpython2 "$@"; }
python3() { bpython3 "$@"; }
fi
#####
# }}}
# Default Aliases {{{
#####################
alias clear='printf "\033c"'
alias cower='cower --color auto'
alias df='df -kTh'
alias du='du -kh'
alias ed='ed -p: -v'
alias free='free -h'
alias ffmpeg='ffmpeg -hide_banner'
alias ffplay='ffplay -hide_banner'
alias ffprobe='ffprobe -hide_banner'
alias grep='noglob rg'
alias iftop='sudo iftop'
alias iotop='sudo iotop -o'
alias nethogs='sudo nethogs'
alias nomino='noglob nomino -p'
alias pdflatex='pdflatex -halt-on-error'
alias pgrep='pgrep -afl'
alias shred='shred -uv'
alias socat='noglob socat'
alias splat='splat -v'
alias sudo='sudo -EH'
alias view='vim -R'
alias vim='vim -p'
alias which='which -a'
alias xsnow='xsnow -nokeepsnow -notrees'
# Colorize output
alias diff='colordiff'
alias less='less -RMSi'
# Modifiers on file operations
alias cp='cp -v'
alias ln='ln -v'
alias mv='mv -v'
alias rmln='rmln -v'
# Use 2021 syntax version of Rust
alias rustfmt='rustfmt --edition=2021'
# Others
i3-resurrect() {
local command="$1"
shift
env \
i3-resurrect "$command" \
--directory "$HOME/.config/i3/i3-resurrect" \
"$@"
}
#####
# }}}
# Platform-specific aliases {{{
###############################
case "$(uname)" in
Darwin)
alias chmod='chmod -v'
alias df='df -kh'
alias grep='grep -I --color=auto'
alias ls='ls -GFL'
alias rm='rm -v'
alias stat='gstat'
alias vim='env vim -p'
alias ldd='otool -L'
;;
Linux)
alias chmod='chmod -c --preserve-root'
alias ls='ls -hHFNv --color=tty'
alias rm='rm -v --one-file-system'
alias grep='grep -I --color'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias open='xdg-open'
alias pbcopy='xsel -bi'
alias pbpaste='xclip -o'
alias pbclear='xsel -c'
alias rmdir='rmdir -v'
alias livestreamer='streamlink'
;;
esac
if which gls > /dev/null; then
alias ls='gls -hHFN --group-directories-first --color=tty'
fi
#####
# }}}
# Custom aliases {{{
####################
# Derivative
alias cpa='cp -a'
# 'ls' commands
if which exa > /dev/null; then
alias ls='exa'
alias la='exa -a'
alias laa='exa -aa'
alias latr='exa -al --sort=newest'
alias lc='exa -lr --sort=created'
alias lk='exa -l --sort=size'
alias ll='exa -l --git'
alias lla='exa -la --git'
alias llaa='exa -laa --git'
alias lm='exa -l | less'
alias ltr='exa -l --sort=newest'
alias lu='exa -l --sort=accessed'
alias lxb='exa -l --sort=extension'
else
alias la='ls -A'
alias laa='ls -a'
alias latr='ls -Altr'
alias lc='ls -ltcr' # Sort by change time, most recent last
alias lk='ls -lSr' # Sort by size, biggest last
alias ll='ls -l'
alias lla='ls -Al'
alias llaa='ls -al'
alias lm='ll | less'
alias ltr='ls -ltr' # Sort by date, most recent last
alias lu='ls -ltur' # Sort by access time, most recent last
alias lxb='ls -lXB' # Sort by extension
fi
# Bacman was removed, fakepkg does the same thing
alias bacman='fakepkg'
# Custom operations
alias 7x='7z x'
alias allgrp='cut -d: -f1 /etc/group'
alias allusr='cut -d: -f1 /etc/passwd'
alias ctime='date +"%A %B %d, %Y %I:%M:%S %p"'
alias define='sdcv'
alias doc2pdf='libreoffice --headless --invisible --norestore --convert-to pdf'
alias dtail='dmesg | tail'
alias keyart='ssh-keygen -lv -f'
alias lsnet='env ls /sys/class/net'
alias mvn='mv -n'
alias nonetwork='LD_PRELOAD=/usr/local/lib/nonetwork.so'
alias srcrc='source ~/.zshrc'
alias tty-clock='/usr/local/scripts/wm/tty-clock.sh'
alias vii3='vim ~/.config/i3/config'
alias virc='vim ~/.zshrc'
alias vibrc='vim ~/.bashrc'
alias vimrc='vim ~/.vimrc'
alias week='date +%V'
alias ytalb='noglob yt-dlp -x --prefer-free-formats -o "%(autonumber)s - %(title)s.%(ext)s" --autonumber-size 2'
alias ytdl='noglob yt-dlp --no-playlist -i -o "%(title)s.%(ext)s"'
alias ytmp3='noglob yt-dlp --no-playlist -x -o "%(title)s.%(ext)s"'
alias md='mkdir -p'
# Program modifiers
alias jp='LC_ALL=ja_JP.UTF-8'
# SSH shortcuts
alias adam='ssh Adam'
alias titus='ssh Titus'
# Valgrind shortcuts
alias memcheck='valgrind --tool=memcheck'
alias cachegrind='valgrind --tool=cachegrind'
alias callgrind='valgrind --tool=callgrind'
alias helgrind='valgrind --tool=helgrind'
alias drd='valgrind --tool=drd'
alias massif='valgrind --tool=massif'
alias dhat='valgrind --tool=dhat'
alias sgcheck='valgrind --tool=sgcheck'
alias bbv='valgrind --tool=bbv'
alias lackey='valgrind --tool=lackey'
alias nulgrind='valgrind --tool=none'
# File navigation commands
alias largest="find . -printf '%s %p\n' | sort -nr | head"
alias smallest="find . -printf '%s %p\n' | sort -nr | tail"
# For fun
alias chanstamp='date +%s%3N'
alias maze='_maze=╱╲;for((;;)){ printf ${_maze:RANDOM&1:1};sleep .01;};unset _maze'
alias mpvnc='mpv --speed=1.5 --audio-pitch-correction=no --no-video'
alias mpvac='mpv --speed=0.67 --audio-pitch-correction=no --no-video'
#####
# }}}
# Short Functions {{{
#####################
# Shortcut commands
catls() {
[[ -d "$1" ]] \
&& ls "$1" \
|| cat "$1"
}
cdd() {
# Used if the current directory was deleted and recreated.
cd "$PWD"
}
cdls() {
cd "$@" && ls
}
cdmv() {
mv "$1" "$2" && cd "$2"
}
countf() {
/usr/bin/ls -1 "$@" | wc -l
}
define2() {
curl "dict://dict.org/d:$1"
}
digq() {
dig "$@" | grep 'Query time'
}
home() {
cd "$HOME" && clear
}
mkcd() {
mkdir -p "$@" && cd "$@"
}
mvcd() {
mv "$@" && cd "${@: -1}"
}
# Renames all *.jpeg files to *.jpg
mvjpeg() {
nomino -r "$@" '(.+)\.jpeg' '{}.jpg'
}
# Renames to add a prefix to each path
mvpre() {
if [[ $# -lt 2 ]]; then
echo >&2 "Usage: mvpre <prefix-to-add> [path...]"
return 1
fi
prefix="$1"
shift
for fn in "$@"; do
mv -n "${fn}" "${prefix}${fn}"
done
}
# Renames to add a suffix to each path
mvsuf() {
if [[ $# -lt 2 ]]; then
echo >&2 "Usage: mvsuf <suffix-to-add> [path...]"
return 1
fi
suffix="$1"
shift
for fn in "$@"; do
mv -n "${fn}" "${fn}${suffix}"
done
}
myip() {
myip4
myip6
}
myip4() {
wget -qO- http://ipecho.net/plain
echo
}
myip6() {
wget -qO- http://ip6echo.net/plain/ \
| sed -Ee 's/<[^>]+>|[[:space:]]+|What'\''s My IPv6 Address\?//g' -e '/^$/d'
echo
}
pacbin() {
pacman -Ql "$1" | /usr/bin/grep --color=never /bin/ | awk '{print $2}'
}
pexec() {
echo "$@"
"$@"
}
pls() {
sudo -- "${SHELL:-zsh}" -c "$(fc -ln -1)"
}
realwhich() {
realpath "$(env which "$1")"
}
# Add all SSH keys to agent
sshadd() {
ssh-add ~/.ssh/ssh-*-{rsa,ed25519}
}
termbin() {
nc termbin.com 9999
}
vimpc() {
env vimpc -h "$(cat ~/.mpd/password)@localhost"
}
# Sets the current directory as the WINEPREFIX
winehere() {
export WINEPREFIX=$PWD WINEHOME=$PWD
}
# Kills all wine processes in the current WINEPREFIX
winekill() {
wineserver -k
}
# Sources xprofile in this shell
xprofile() {
source "$HOME/.xprofile"
}
# Colorize text
red() { printf "\e[31m\e[1m%s\e[0m" "$@"; }
green() { printf "\e[32m\e[1m%s\e[0m" "$@"; }
blue() { printf "\e[34m\e[1m%s\e[0m" "$@"; }
redl() { printf "\e[31m%s\e[0m" "$@"; }
greenl() { printf "\e[32m%s\e[0m" "$@"; }
bluel() { printf "\e[34m%s\e[0m" "$@"; }
# Hash checkers
md5check() { [[ $(md5sum "$2" | cut -d' ' -f1) == "$1" ]] && green '[OK]' || red '[FAIL]'; }
sha1check() { [[ $(sha1sum "$2" | cut -d' ' -f1) == "$1" ]] && green '[OK]' || red '[FAIL]'; }
sha224check() { [[ $(sha224sum "$2" | cut -d' ' -f1) == "$1" ]] && green '[OK]' || red '[FAIL]'; }
sha256check() { [[ $(sha256sum "$2" | cut -d' ' -f1) == "$1" ]] && green '[OK]' || red '[FAIL]'; }
sha384check() { [[ $(sha384sum "$2" | cut -d' ' -f1) == "$1" ]] && green '[OK]' || red '[FAIL]'; }
sha512check() { [[ $(sha512sum "$2" | cut -d' ' -f1) == "$1" ]] && green '[OK]' || red '[FAIL]'; }
# Misc
note() { scr note "$@" 2> /dev/null; }
mpva() { @ mpv --no-video "$@"; }
mpvq() { @ mpv --no-terminal --force-window "$@" & }
ytdlq() { ytdl --no-playlist "$@" >/dev/null 2>&1 & }
#####
# }}}
# Functions {{{
###############
# For screenfetches on anonymous imageboards
anon() {
local name="anonymous"
local host="computer"
PS1=${PS1//\%n/"$name"} PS1=${PS1//\%m/"$host"}
printf '\033c'
}
# Manually download PKGBUILD for building
aur() {
if [[ $# -eq 0 ]]; then
echo >&2 'No packages specified.'
return 1
fi
(
cd "$AURDEST"
for pkg in "$@"; do
git clone "https://aur.archlinux.org/$pkg.git"
done
)
}
# For comparing binary files
bdiff() {
if [[ $# -lt 2 ]]; then
echo >&2 'Not enough arguments.'
echo >&2 'Usage: bdiff first-file second-file'
return 1
fi
cmp -l "$1" "$2" | gawk '{printf "%08X %02X %02X\n", $1-1, strtonum(0$2), strtonum(0$3)}'
}
# Currency conversion. Be sure to use official abbreviations
cconv() {
wget -qO- "http://www.google.com/finance/converter?a=$1&from=$2&to=$3" | sed '/res/!d;s/<[^>]*>//g';
}
# Run a command in a subshell with a different cwd
cddo() {
if [[ $# -lt 2 ]]; then
echo >&2 'Not enough arguments.'
echo >&2 'Usage: cddo directory program [arguments...]'
return 1
fi
(
cd "$1"
shift
"$@"
)
}
# A command replacement that says 'this command is disabled'.
command-disabled() {
echo >&2 'This command has been disabled for safety purposes.'
echo >&2 'If you really want to run it, invoke it directly. (e.g. /bin/false instead of false)'
return 1
}
# A simple confirmation message. Usage: 'confirm && (action)'
confirm() {
printf 'Are you sure? [y/N] '
read -r confirm
case "$confirm" in
yes) return 0 ;;
y) return 0 ;;
Y) return 0 ;;
*) return 1 ;;
esac
}
# Detach from TTY
daemon() {
"$@" \
< /dev/null \
> /dev/null \
2> /dev/null \
&
}
# Simple function to calculate date differences
datediff() {
d1="$(date -d "$1" +%s)"
d2="$(date -d "$2" +%s)"
echo "$(( (d1 - d2) / 86400 ))" days
}
# Easily convert from base64
decode64() {
if [[ $# -eq 0 ]]; then
base64 --decode
else
base64 --decode <<< "$1"
fi
}
# Easily convert to base64
encode64() {
if [[ $# -eq 0 ]]; then
base64
else
base64 <<< "$1"
fi
}
# Disable fancy zsh completion on slow filesystems (e.g. NFS)
ftab() {
zstyle ':completion:*' path-completion false
zstyle ':completion:*' accept-exact-dirs true
echo 'Disabled fancy tab completion'
}
# Batch converts from one format to another
ffbatch() {
if [[ $# -lt 2 ]]; then
echo >&2 'Not enough arguments.'
echo >&2 'Usage: ffbatch mp3 ogg'
fi
find -type f -name "*.$1" -print0 \
| while read -d $'\0' a; do
ffmpeg -i "$a" "${a[@]/%$1/$2}" < /dev/null
done
#rm -ir **/*.$1
}
# Joins L and R tracks into a single file.
ffstereo() {
if [[ $# -lt 3 ]]; then
echo >&2 'Not enough arguments.'
echo >&2 'Usage: ffstereo left-track-file right-track-file output-file'
return 1
fi
ffmpeg -i "$1" -i "$2" -filter_complex "[0:a][1:a]amerge=inputs=2[aout]" -map "[aout]" -c:a flac "$3"
}
# Changes the volume of a video or audio file.
ffvolume() {
if [[ $# -lt 3 ]]; then
echo >&2 'Not enough arguments.'
echo >&2 'Usage: ffvolume input-file output-file volume-modifier'
return 1
fi
ffmpeg -i "$1" -af "volume=$3" "$2"
}
# Look for a certain .desktop file
findapp() {
for arg in "$@"; do
grep -rnw "$arg" "/usr/share/applications/"
grep -rnw "$arg" "$HOME/.local/share/applications/"
done
}
# Finds the volume of an audio file in decibels
getvolume() {
ffmpeg -i "$1" -af 'volumedetect' -f null /dev/null
}
# Run gofmt on *.go files
gofmtall() {
if [[ $# -eq 0 ]]; then
gofmtall *.go
return $?
fi
for fn in "$@"; do
gofmt "$fn" | sponge "$fn"
done
}
# Insult the user if their command fails.
insult() {
# Warning: slow
printf 'root says: '
curl -s randominsults.net | sed -n '/<strong>/{s;^.*<i>\(.*\)</i>.*$;\1;p}'
}
# Make sure the user is kexec'ing the right machine
kexec() {
sleep 0.5
printf "About to reboot (via kexec) \e[1m%s\e[0m. You sure? " "$(cat /etc/hostname)"
read -r response
case "$response" in
y*|Y*) sudo systemctl kexec ;;
*) echo >&2 'Aborting.'
esac
}
# List dbus services
lsdbus() {
if [[ $# -eq 0 ]] || [[ $1 == '--help' ]]; then
echo >&2 'Not enough arguments.'
echo >&2 'Usage: lsdbus (session | system)'
return 1
fi
dbus-send "--$1" \
--dest=org.freedesktop.DBus \
--type=method_call \
--print-reply \
/org/freedesktop \
org.freedesktop.DBus.ListNames
}
# List file descriptors of the given pids or processes
lsfd() {
if [[ $# -eq 0 ]]; then
echo >&2 'Usage: lsfd (pid | process)...'
return 1
fi
for arg in "$@"; do
if [[ $arg -gt 0 ]]; then
ls -l "/proc/$arg/fd"
else
env pgrep "$arg" | \
while read -r pid; do
echo "$pid"
ls -lv "/proc/$pid/fd"
done
fi
done
}
# List osu! songs
lsosu() {
readonly local osu_song_dir='/media/media/games/osu!/Songs'
if [[ $# -eq 0 ]]; then
ls "$osu_song_dir"
else
ls "$osu_song_dir" | grep -i "$1"
fi
}
# Print your most used shell commands
mostused() {
history \
| awk '{ print $4 }' \
| sort \
| uniq -c \
| sort -nr \
| head -n 20
}
# Rename files with the same name to something else.
# Usage: movc original destination [extensions]
movc() {
case "$#" in
2)
readonly local exts='ch'
;;
3)
readonly local exts="$3"
;;
*)
printf >&2 'Usage: movc ORIG DEST [EXTENSIONS]\n'
return 1
;;
esac
sed -e 's/\(.\)/\1\n/g' <<< "$exts" \
| while read -r ext; do
if [[ -z "$ext" ]]; then
break;
fi
mv -v "$1.$ext" "$2.$ext" || true
done
}
# Create a new shell without ssh-agent
nossh() {
unset SSH_AGENT_PID
unset SSH_AUTH_SOCK
}
# Run the given process without ssh-agent
nossh2() {
(
nossh
exec ssh "$@"
)
}
# Opens a caption stored in a directory
opencaption() {
if [[ $# -eq 0 ]]; then
echo >&2 'Usage: opencaption directory'
return 1
fi
images=("$1"/*.{gif,jpg,jpeg,png}(N))
if [[ ${#images} -gt 0 ]]; then
nomacs -p "${images[1]}" >/dev/null 2>&1 &
fi
cat "$1/caption.txt"
}
# Print the 256 terminal colors (if supported)
p256() {
local x="$(tput op)"
local y="$(printf %$((${COLUMNS}-6))s)"
for i in {0..256}; do
local o="00$i"
echo -e "${o:${#o}-3:3} "$(tput setaf $i; tput setab $i)"${y// /=}$x"
done
}
# Reopen a PDF if the viewer closes
pdf-always() {
if [[ $# -eq 0 ]]; then
echo >&2 'Usage: pdf-always filename'
return 1
fi
while true; do
if [[ -f $1 ]]; then
mupdf "$1"
else
sleep 1
fi
done
}
# Print number of processes
procno() {
find /proc -maxdepth 1 -regex '/proc/[0-9]+' -printf . | wc -c
}
# Pretty print a JSON file
pjson() {
python -m json.tool <<< "$@"
}
# Make sure the user is rebooting the right machine
reboot() {
sleep 0.5
printf "About to reboot \e[1m%s\e[0m. You sure? " "$(cat /etc/hostname)"
read -r response
case "$response" in
y*|Y*) sudo reboot ;;
*) echo >&2 'Aborting.'
esac
}
# Run one of my scripts without specifying the path
scr() {
if [[ $# -eq 0 ]]; then
printf >&2 'Usage: scr script-name.\n'
return 1
fi
dofind() {
if [[ $(uname) == Linux ]]; then
find -L '/usr/local/scripts' -iname "*$1*" -executable -print
else
find -L "$HOME/git/scripts" -iname "*$1*" -perm +x -print
fi
}
IFS=$'\n' \
local scripts=($(dofind "$1"))
case "${#scripts[@]}" in
0)
printf >&2 'Cannot find script "%s".\n' "$1"
return 1
;;
1)
local script="${scripts[1]}"
echo "$script"
shift
"$script" "$@"
return 0
;;
*)
printf >&2 'Multiple script candidates:\n'
printf >&2 '%s\n' "${scripts[@]}"
return 1
;;
esac
}
# Default options for encfs mount
encmnt() {
if [[ $# -eq 0 ]]; then
echo >&2 "Usage: encmnt <crypt-dir> <mount-dir> [extra-flags]"
return 1
fi
if [[ $# -ge 2 ]]; then
local crypt_dir="$1"
local mount_dir="$2"
shift 2
else
local crypt_dir="$1.encfs"
local mount_dir="$1"
shift 1
fi
if [[ ! -d $mount_dir ]]; then
mkdir "$mount_dir"
fi
encfs "$(realpath "$crypt_dir")" "$(realpath "$mount_dir")"
}
encumnt() {
if [[ $# -eq 0 ]]; then
echo "Usage: encumnt <mount-dir>"
return 1
fi
~/git/scripts/nomacs-wipe.sh
encfs -u "$(realpath "$1")" && \
rmdir "$1"
}
# Default options for securefs mount
secmount() {
if [[ $# -eq 0 ]]; then
echo >&2 "Usage: secmount <crypt-dir> <mount-dir> [extra-flags]"
return 1
fi
if [[ $# -ge 2 ]]; then
local crypt_dir="$1"
local mount_dir="$2"
shift 2
else
local crypt_dir="$1.secfs"
local mount_dir="$1"
shift 1
fi
if [[ -f "$crypt_dir/.securefs.lock" ]]; then
if pgrep securefs > /dev/null; then
echo >&2 "securefs mount already running. if you're sure this isn't true, then remove $crypt_dir/.securefs.lock"
return 1
else
rm "$crypt_dir/.securefs.lock"
fi
fi
securefs mount -b --log "$crypt_dir/.securefs.log" "$@" -- "$crypt_dir" "$mount_dir"
}
secumount() {
if [[ $# -eq 0 ]]; then
echo >&2 "Usage: secumount <mount-dir>"
return 1
fi
~/git/scripts/nomacs-wipe.sh
fusermount -u "$1" && \
rmdir "$1"
}
# Set the title of the terminal
settitle() {
echo -ne "\e]2;$@\a\e]1;$@\a"
}
# Changes up how the sl train looks
sl() {
local sett=e
readonly local rand="$RANDOM"
[[ $[ $rand & 1 ] -gt 0 ]] && sett+=a
[[ $[ $rand & 2 ] -gt 0 ]] && sett+=l
[[ $[ $rand & 4 ] -gt 0 ]] && sett+=F
[[ $[ $rand & 8 ] -gt 0 ]] && sett+=c
env sl "-$sett"
}
# Make sure the user is shutting down the right machine
shutdown() {
sleep 0.5
printf "About to shutdown \e[1m%s\e[0m. You sure? " "$(cat /etc/hostname)"
read -r response
case "$response" in
y*|Y*) sudo shutdown now ;;
*) echo >&2 'Aborting.'
esac
}
# Make sure the user is rebooting the right machine
reboot() {
sleep 0.5
printf "About to reboot \e[1m%s\e[0m. You sure? " "$(cat /etc/hostname)"
read -r response
case "$response" in
y*|Y*) sudo reboot ;;
*) echo >&2 'Aborting.'
esac
}
# Get lengths of command line arguments
strlen() {