-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashrc
1097 lines (970 loc) · 32.5 KB
/
bashrc
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
export DOTFILES=~/.config/dotfiles
# Replace with a tmux session if it is an interactive session and tmux is installed and is not already running
if [[ $UID -ne 0 ]] && [[ $- = *i* ]] && which tmux > /dev/null 2>&1 && [[ -z "$TMUX" ]] && [[ -z "$NOTMUX" ]] && [[ ! $TTY = *tty* ]] ;then
ID="`tmux ls | grep -vm1 attached | cut -d: -f1`" # get the id of a deattached session
if [[ -z "$ID" ]] ;then # if not available create a new one
exec tmux new-session 2>/dev/null
else
exec tmux attach-session -t "$ID" 2>/dev/null # if available attach to it
fi
fi
export EDITOR='vim'
export VISUAL=$EDITOR
export HISTFILESIZE=100000
export HISTSIZE=${HISTFILESIZE}
export HISTFILE=${DOTFILES}.safe/bash_history
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
MYSHELL=$(ps -p $$ -ocomm= 2>/dev/null)
#[[ $- = *i* ]] && stty -ixon
if [[ $- = *i* ]] && [[ "$MYSHELL" = 'bash' ]];then
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
shopt -s nocaseglob 2>/dev/null # Case-insensitive globbing (used in pathname expansion)
shopt -s histappend 2>/dev/null # Append to the Bash history file, rather than overwriting it
shopt -s cdspell 2>/dev/null # Autocorrect typos in path names when using `cd`
shopt -s autocd 2>/dev/null
shopt -s checkwinsize 2>/dev/null
shopt -s cmdhist 2>/dev/null # force multi-line commands to be stored in the history as a single line
shopt -s no_empty_cmd_completion 2>/dev/null
# Reset
Color_Off="\[\033[0m\]" # Text Reset
# Regular Colors
Black="\[\033[0;30m\]" # Black
Red="\[\033[0;31m\]" # Red
Green="\[\033[0;32m\]" # Green
Yellow="\[\033[0;33m\]" # Yellow
Blue="\[\033[0;34m\]" # Blue
Purple="\[\033[0;35m\]" # Purple
Cyan="\[\033[0;36m\]" # Cyan
White="\[\033[0;37m\]" # White
# Bold
BBlack="\[\033[1;30m\]" # Black
BRed="\[\033[1;31m\]" # Red
BGreen="\[\033[1;32m\]" # Green
BYellow="\[\033[1;33m\]" # Yellow
BBlue="\[\033[1;34m\]" # Blue
BPurple="\[\033[1;35m\]" # Purple
BCyan="\[\033[1;36m\]" # Cyan
BWhite="\[\033[1;37m\]" # White
[[ $UID -eq 0 ]] && prompt='#' || prompt='$'
export PS1="\n[${BBlue}${MYSHELL}${Color_Off}][\t] ${BYellow}\w\n\$([[ \$? == 0 ]] && echo \"${BGreen}\" || echo \"${BRed}\")${prompt}${Color_Off} "
fi
_checkargs() {
[[ $# -lt 2 ]] && echo "Incorrect check usage" && return 2
if [[ $1 -lt $2 ]]; then
echo "Atleast $2 argument(s) expected!"
return 1
fi
return 0
}
# Enable `sudo alias`
alias sudo='sudo '
# Enable `sudo bash_function`
sudof() {
if [[ $# -lt 1 || -z "$1" ]]; then
echo "Error: Missing input arguments!"
return 1
fi
fn_name=$1
shift
FUNC=$(declare -f $fn_name)
[[ -z "$FUNC" ]] && { echo "Error: Function '$fn_name' not found"; return; }
sudo bash -c "$FUNC; $fn_name $@"
}
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias h=htop
alias b='cd -'
alias p="pnpm"
alias zs="zpool status -v"
alias zi="zpool iostat -v"
alias zl="zpool list -v"
alias zsp="zfs list -t snapshot"
alias za="zs;echo;zi;echo;zl;zsp;df -Th"
alias zss="zfs list -t snapshot"
alias zssc="sudo zfs snapshot"
alias zssd="sudo zfs destroy"
alias bydl="yt-dlp -o '%(title)s [%(upload_date>%Y.%m.%d)s][%(channel)s][%(id)s].%(ext)s' --no-mtime --embed-metadata --no-embed-info-json --extractor-args 'youtube:lang=en'"
ydl() {
_checkargs $# 1 || return 1
opts=()
res="480"
if [[ "$1" == "ad" ]];then
opts+=(-f 'bestaudio' -x --audio-format opus --audio-quality 0 --embed-metadata --no-embed-info-json --embed-thumbnail)
shift
else
[[ "$1" == "r" ]] && { res="$2"; shift; shift; }
opts+=(-f "bestvideo[height<=$res]+bestaudio/best[height<=$res]")
fi
[[ "$1" == "ws" ]] && { opts+=(--write-subs --write-auto-subs -o 'subtitle:ysubs/%(title)s [%(upload_date>%Y)s][%(channel)s][%(id)s].%(ext)s' --convert-subs 'srt' --compat-options 'no-live-chat'); shift; }
[[ "$1" == "es" ]] && { opts+=(--embed-subs); shift; }
bydl "${opts[@]}" "$@"
}
alias ydla='ydl ad'
alias df='df -Th'
alias lsg='ls -CFalh --color=auto | grep --color=auto -i'
alias psg='ps aux | grep -v grep | grep -i -e "^USER " -e '
alias psgc='ps aux | grep -v grep | grep -i -e '
alias pkl='kill -9'
alias spkl='sudo kill -9'
ports() {
[[ "$1" == "" ]] && { 1="-k6,6n"; 2="-k3,3n"; 3="-k4,4"; 4="-k1,1"; }
sep=";"
(
echo -e "Proto${sep}Address${sep}Port${sep}v${sep}UID${sep}PID${sep}Process${sep}Other"
(
sudo ss --no-header -l4tunpe | sed -r "s/^/v4 /"
sudo ss --no-header -l6tunpe | sed -r "s/^/v6 /"
) | awk '{$6=gensub(/(.+):(.+)/,"\\1'${sep}'\\2",1,$6); rest=""; for(i=8;i<=NF;i++) { rest=rest " " $i; }; if(rest ~ /uid:/) { $8=gensub(/.*uid:([0-9]+).*/,"\\1",1,rest); } else { $8="0"; }; if(rest ~ /^ users/) { $8=$8 gensub(/^ users:\(\("([^"]+)",pid=([0-9]+),(.*)\)\).*/,"'${sep}'\\2'${sep}'\\1'${sep}'\\3",1,rest); $8=gensub(/\),\(/,",","g",$8); } else { $8=$8 "'${sep}'-'${sep}'-'${sep}'-"; }; if(rest ~ /.service/) { $8=$8 "," gensub(".*/([^\\.]+\\.service).*","\\1",1,rest); }; if(rest ~ /docker.*scope/) { $8=$8 ",docker.scope"; }; OFS="'${sep}'"}{print $2,$6,$1,$8}' | sort --field-separator "${sep}" "$@"
) | column -t -s"${sep}"
}
alias mv='mv -v'
alias rm='rm -v'
alias rmdir='rmdir -v'
alias rmd='rmdir -v'
alias rmr='rm -vrf'
alias srmr='sudo rm -vrf'
alias mount='mount -v'
alias umount='umount -v'
alias dmesg='dmesg --human -T'
alias grep='grep -i --color=auto'
alias ev="env | sort"
alias tn='tail -n'
alias hn='head -n'
alias tf='tail -F'
alias df='df -h'
alias du='du -sh'
alias lsblk='lsblk -o NAME,FSTYPE,SIZE,RO,MOUNTPOINT,LABEL,UUID'
rdl() {
dir="${1:-.}"
readlink -f $dir
}
alias vi='vim'
alias se='sudoedit'
alias sv='sudo vim -u ~/.vimrc'
alias e='vim'
alias ee='emacsclient -c'
alias dateh='date --help|sed -n "/^ *%%/,/^ *%Z/p"|while read l;do F=${l/% */}; date +%$F:"|'"'"'${F//%n/ }'"'"'|${l#* }";done|sed "s/\ *|\ */|/g" |column -s "|" -t'
alias xcp='xclip -selection clipboard'
alias httpserver="python3 -m http.server"
alias sx="startx"
alias myips='ip -o -f inet addr | grep -v "127.0.0.1" | cut -d'/' -f1 | sed -r "s/[ \t]+/ /g" | cut -d" " -f2-4 | awk "{print \$1\": \"\$3}" | sort | uniq'
mypublicip() {
printf "curl -s ident.me\ncurl -s icanhazip.com\ncurl -s4 ifconfig.co\ncurl -s6 ifconfig.co" | xargs -I{} sh -c 'x=$({} | tr -d "\"";echo " | {}");echo $x'
}
alias please='sudo $(fc -ln -1)'
alias prettyplease='sudo $(history | tail -1 | awk "{\$1=\"\";print}" | xargs)'
alias rzsh='. ~/.zshrc'
alias rbash='. ~/.bashrc'
alias ga='git add'
alias gc='git commit -m'
alias gca='git commit -am'
alias gcm='git commit --amend'
alias gcma='git commit --amend -a'
alias gs="git ls -n5;echo;git status"
alias gsa="git lsa -n5;echo;git status"
alias gt='git stash'
alias gtp='git stash pop'
alias gcl='git clone'
alias gb='git branch -a'
alias gpu='git push'
alias gp='git pull'
alias gpp='git pull --rebase && git push'
alias gcf='git config --list'
alias grh='git reset HEAD'
alias grc='git rebase --continue'
alias gra='git rebase --abort'
alias gd="git difftool"
alias gdo="git diff"
alias gk="gitk --all"
alias gg="git gui"
alias dk='sudo docker'
alias dkr='sudo docker run'
alias dki='sudo docker images'
alias dkia='sudo docker images -a'
alias dkc='sudo docker ps'
alias dkca='sudo docker ps -a'
dkrc() { sudo docker start $1 && sudo docker attach $1;}
dkrm() { sudo docker kill $@; sudo docker rm $@; }
alias isolate="sudo docker network disconnect bridge"
alias join="sudo docker network connect bridge"
alias pm='podman'
alias pmr='podman run'
alias pmi='podman images'
alias pmia='podman images -a'
alias pmc='podman ps'
alias pmca='podman ps -a'
# Nixos
nxos() {
fl="/tmp/dry-build.txt"
[[ "${1:-}" == "upd" ]] && { nix flake update --flake /etc/nixos; shift; }
nvd diff "$(nix path-info --derivation "/run/current-system")" "$(nix path-info --derivation "/etc/nixos#nixosConfigurations.$(hostname).config.system.build.toplevel")" || return 1
if [[ "${1:-}" == "dry" ]];then
# nixos-rebuild --flake /etc/nixos dry-build &> $fl || return
#nix store diff-closures "$(nix path-info --derivation "/run/current-system")" "$(cat "$fl" | grep nixos-system | tr -d ' ')"
#echo
# echo "Download:"
# cat "$fl" | awk 'p;/will be fetched/{p=1}' | tr -d ' '
#
# echo
# echo "Local:"
# cat "$fl" | awk '/fetched/{p=0}p;/will be built/{p=1}' | while read d ;do grep -qi "preferLocalBuild" "$d" && echo "$d" ;done
#
# echo
# echo "Build:"
# cat "$fl" | awk '/fetched/{p=0}p;/will be built/{p=1}' | while read d ;do grep -qi "preferLocalBuild" "$d" || echo "$d" ;done
#
# echo
:
else
echo
sudo true || return 2;
sudo nice -10 nixos-rebuild --keep-going --flake /etc/nixos "${1:-boot}" |& nom
fi
}
nxd() {
_checkargs $# 1 || return 1
qt="$1"
case "$qt" in
"rf") q="--references" ;;
"rfc") q="--requisites" ;;
"rr") q="--referrers" ;;
"rrc") q="--referrers-closure" ;;
*) q="--references" ;;
esac
p="$2"
echo "$p" | grep -q ".drv$" || p="$(nix-store --query --deriver "$p")"
nix-store --query $q "$p"
}
nxp() {
_checkargs $# 1 || return 1
qt="$1"
case "$qt" in
"rf") q="--references"; shift ;;
"rfc") q="--requisites"; shift ;;
"rr") q="--referrers"; shift ;;
"rrc") q="--referrers-closure"; shift ;;
"dr") q="--deriver"; shift ;;
*) q="--references" ;;
esac
p="$1"
echo "$p" | grep -q ".drv$" && { echo "Error: $p is not a path"; return 1; }
nix-store --query $q "$p"
}
nxds() {
_checkargs $# 1 || return 1
p="$1"
echo "$p" | grep -q ".drv$" || p="$(nix-store --query --deriver "$p")"
nix derivation show "$p^*"
}
nxwl() {
exa -l "$(which "$1")"
}
nxr() {
_checkargs $# 1 || return 1
p=$1
shift
nix run "nixpkgs#$p" "$@"
}
nxwd() {
_checkargs $# 2 || return 1
p1="$1"
echo "$p1" | grep -q ".drv$" || p1="$(nix-store --query --deriver "$p1")"
p2="$2"
echo "$p2" | grep -q ".drv$" || p2="$(nix-store --query --deriver "$p2")"
nix why-depends "$p1" "$p2"
}
nxdiff() {
_checkargs $# 2 || return 1
difft <(nxds $1) <(nxds $2)
}
nxsdiff() {
_checkargs $# 2 || return 1
difft --override='*:json' --skip-unchanged --ignore-comments --context 0 <(nxds $1 | sed -r 's|/nix/store/[^-]+-||g' | jq --sort-keys) <(nxds $2 | sed -r 's|/nix/store/[^-]+-||g' | jq --sort-keys)
}
# Pacman package management
alias pcm='pacman'
alias pcmu='sudo pacman -Syu --needed'
alias pcmi='sudo pacman -S --needed'
alias pcms='pacman -Ss'
alias pcmsl='pacman -Qs'
alias pcmr='sudo pacman -Rcs'
alias pcmri='sudo pacman -Rc'
alias pcmc='sudo pacman -Sc --noconfirm'
alias pcmm='pacman -Qm; echo; pacman -Qdt'
alias pcml='pacman -Fl'
alias pcmll='pacman -Ql'
alias pcmii='pacman -Si'
alias pcmiil='pacman -Qii'
alias pcmo='pacman -Qo'
alias pcmor='pacman -F'
pcmwo() {
wch="$(which $1 2>/dev/null)"
echo $wch
[[ "$wch" =~ ^/.* ]] && pcmo "$wch"
}
alias pru='pikaur -Syu --needed --mflags=--skippgpcheck'
alias prua='pru --devel'
alias pri='pikaur -S --needed'
alias prs='pikaur -Ss'
add_to_repo() {
_checkargs $# 2 || return 1
[[ ! -r PKGBUILD ]] && echo "Must be run in a PKGBUILD dir" && return 1
root=$1
repo_db=$2
source PKGBUILD
pkgver="$pkgver-$pkgrel"
[[ "$MYSHELL" = 'bash' ]] && shopt -s failglob
for i in *$pkgver*tar*;do
x=$(echo $i | sed -r "s/(.*)-$pkgver.*/\1/")
sudo rm -v $root/$x* 2> /dev/null
sudo cp -v $i $root/$i
sudo repo-add $root/$repo_db $i
done
}
# Apt-get package management
alias agu='sudo apt-get update && sudo apt-get upgrade'
alias agi='sudo apt-get install'
alias ags='sudo apt-cache search'
alias agr='sudo apt-get remove'
# Yum package management
alias yumu='sudo yum update'
alias yumi='sudo yum install'
alias yums='sudo yum search'
alias yumr='sudo yum remove'
alias yuml='sudo yum --showduplicates list'
alias yumf='sudo yum --showduplicates info'
osv() {
for i in /etc/*-release /etc/debian_version ;do
[[ -f "$i" ]] && echo "> $i" && cat "$i"
done
}
alias osva='cat /etc/*-release /etc/debian_version 2>/dev/null | sort | uniq | xargs -L1'
tfm() {
n=${1:-30}
sudo journalctl -n${n} -f
}
alias magic2='cd;$DOTFILES/scripts/startOpenVPN.sh ~/directi/client.ovpn `~/sshhhh mnetu | base64 --decode` `~/sshhhh mnetp | base64 --decode` `~/sshhhh mnetc | base64 --decode | python2 $DOTFILES/scripts/gauthenticator.py`'
alias magic='cd;$DOTFILES/scripts/startOpenVPN.sh ~/directi/mnet-client.ovpn `~/sshhhh mnetu | base64 --decode` `~/sshhhh mnetp | base64 --decode` `~/sshhhh mnetc2 | base64 --decode | python2 $DOTFILES/scripts/gauthenticator.py`'
xsshlistener() {
_checkargs $# 2 || return 1
host=$1
shift
str=""
for i in "$@";do
l="$(echo $i | cut -d: -f1)"
r="$(echo $i | cut -d: -f2)" # Equal to $l if no colon
str="$str -L ${l}:localhost:${r}";
done
cmd="ssh -o ServerAliveInterval=60 -fN $str $host"
bash -c "$cmd"
}
xgen2() {
_checkargs $# 1 || return 1
x="$(printf "%d\n" \'${1: -1})"
y=${#1}
echo "$1$((x+5))$((y+5))"
}
xgen() {
_checkargs $# 1 || return 1
f="$(printf "%d\n" \'${1:0:1})"
l="$(printf "%d\n" \'${1: -1})"
y=${#1}
t=$((f+l-96*2))
echo "$1$((t+t%y))"
}
printColors() {
echo $(for code in {0..255};do print -P -- "%F{$code}$code %f" ;done)
for C in {0..255}; do tput setab $C; echo -n "$C "; done; tput sgr0; echo
}
# Taken from http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
MARKPATH=${DOTFILES}.safe/marks
mkdir "$MARKPATH" &>/dev/null
function j {
cd -P "$MARKPATH/${1:-d}" 2>/dev/null || echo "No such mark: $1"
}
function m {
_checkargs $# 1 || return 1
mkdir -p "$MARKPATH"; ln -vs "$(pwd)" "$MARKPATH/$1"
}
function mf {
_checkargs $# 1 || return 1
mkdir -p "$MARKPATH"; ln -vsnf "$(pwd)" "$MARKPATH/$1"
}
function um {
_checkargs $# 1 || return 1
(cd $MARKPATH && rm -v $1)
}
function mks {
ls -n "$MARKPATH" | grep -v total | tr -s ' ' | cut -d ' ' -f 9- | sed 's/->/:/' | column -t -s ':'
}
if [[ $- = *i* ]] && [[ "$MYSHELL" = 'zsh' ]];then
function _completemarkszsh {
reply=($(ls $MARKPATH))
}
compctl -K _completemarkszsh j
compctl -K _completemarkszsh um
fi
if [[ $- = *i* ]] && [[ "$MYSHELL" = 'bash' ]];then
_completemarksbash() {
local curw=${COMP_WORDS[COMP_CWORD]}
local wordlist=$(find $MARKPATH -type l -printf "%f\n")
COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
return 0
}
complete -F _completemarksbash j um
fi
xsendkeys() {
_checkargs $# 1 || return 1
tmux list-windows | grep -v active | cut -d: -f1 | sort -n -r | xargs -I{} bash -c "echo {} && tmux send-keys -t :{} $(echo $1 | sed 's/ / SPACE /g')"
}
xmultispawn() {
option=$1; shift
max="$1"; shift
[[ $max -le 0 ]] && echo "max should be >= 1" && return
name="$1"; shift
{ [[ -z "$name" ]] || tmux list-windows | awk '{print $2}' | tr 'A-Z' 'a-z' | tr -dc 'a-z\n' | grep "^$name$" >/dev/null 2>&1; } && name=$(< /dev/urandom tr -dc "a-z" | head -c3) || true
case "$option" in
v) layout=even-vertical
;;
h) layout=even-horizontal
;;
hv) layout=tiled
;;
*) echo 'Usage: xmultispawn h/v/hv max_pane_per_window windowname "ssh c8-logging-"{1..3}' && return;;
esac
total=$#
echo "Total = $total"
[[ $total -eq 0 ]] && return
totalwindows=$(echo "($total+$max-1)/$max" | bc)
echo "Total windows = $totalwindows"
adjust=$((total%totalwindows))
for i in $(eval echo "{1..$totalwindows}");do
paneperwindow=$((total/totalwindows)); [[ $adjust -gt 0 ]] && paneperwindow=$((paneperwindow+1)) && adjust=$((adjust-1))
echo "Spawning window $i"
tmux neww -dn ${name}-${i} "$1"; shift
sleep 0.3
paneperwindow=$((paneperwindow-1))
[[ $paneperwindow -lt 1 ]] && continue
c=h
for j in $(eval echo "{1..$paneperwindow}");do
tmux splitw -$c -t ${name}-${i}.$j -d "$1"; shift
[[ "$c" = "h" ]] && c=v || c=h
done
tmux select-layout -t ${name}-${i} $layout
tmux set-window-option -t ${name}-${i} synchronize-panes on
done
}
alias ccm='sudo ccm64'
alias xchromestart="chromium --proxy-server='socks://127.0.0.1:9999' --incognito"
# Systemd service management
sds() { sudo systemctl status -l --no-pager -n10 $1; }
sdsf() { sudo systemctl status -l --no-pager -n0 $1; echo; sudo journalctl -f -u $1 -S "$2"; }
sdst() { dt=$(date +'%Y-%m-%d %T'); sudo systemctl start $1; sdsf $1 "$dt"; }
sdsp() { dt=$(date +'%Y-%m-%d %T'); sudo systemctl stop $1; sdsf $1 "$dt"; }
sdr() { dt=$(date +'%Y-%m-%d %T'); sudo systemctl restart $1; sdsf $1 "$dt"; }
sde() { sudo systemctl enable $1; ls -l /etc/systemd/system/multi-user.target.wants; }
sdd() { sudo systemctl disable $1; ls -l /etc/systemd/system/multi-user.target.wants; }
# User service management
sus() { systemctl --user status -l --no-pager -n10 $1; }
susf() { systemctl --user status -l --no-pager -n0 $1; echo; journalctl --user -f -u $1 -S "$2"; }
sust() { dt=$(date +'%Y-%m-%d %T'); systemctl --user start $1; susf $1 "$dt"; }
susp() { dt=$(date +'%Y-%m-%d %T'); systemctl --user stop $1; susf $1 "$dt"; }
sur() { dt=$(date +'%Y-%m-%d %T'); systemctl --user restart $1; susf $1 "$dt"; }
sue() { systemctl --user enable $1; ls -l /home/$USER/.config/systemd/user/*; }
sud() { systemctl --user disable $1; ls -l /home/$USER/.config/systemd/user/*; }
# Init scripts service management
ups() { sudo service $1 status ; }
upst() { sudo service $1 start ; }
upsp() { sudo service $1 stop ; }
upr() { sudo service $1 restart ; }
uprl() { sudo service $1 reload ; }
upe() { sudo chkconfig --add $1 && sudo chkconfig $1 on && sudo chkconfig --list $1 ; }
upd() { sudo chkconfig $1 off && sudo chkconfig --list $1 ; }
hold_fort() {
while true;do
date
xdotool getmouselocation --shell
echo
xdotool mousemove_relative -- $(( $RANDOM % 30 - 15)) $(( $RANDOM % 30 - 15));xdotool getmouselocation --shell
echo
sleep 55
done
}
mkcd() {
dir="$*"
mkdir -vp "$dir" && cd "$dir"
}
ctg() {
cat "$1" | grep "$2"
}
l() {
unset long all dir
[[ "$1" == "_long" ]] && local long="true" && shift
[[ "$1" == "_all" ]] && local all="true" && shift
[[ "$1" == "_dir" ]] && local dir="true" && shift
[[ "$1" == "_sudo" ]] && local pre="sudo " && shift
args=()
if command -v eza &> /dev/null;then
[[ "${long:-}" == "true" ]] && args+=(-lg --icons)
[[ "${all:-}" == "true" ]] && args+=("-aa")
[[ "${dir:-}" == "true" ]] && args+=("--only-dirs")
eval ${pre:-} eza --group-directories-first --color=auto --sort=extension "${args[@]}" "$@"
else
[[ "${long:-}" == "true" ]] && args+=("-l")
[[ "${all:-}" == "true" ]] && args+=("--all")
eval ${pre:-} ls -XF --color=auto --group-directories-first "${args[@]}" "$@"
fi
}
alias la='l _all'
alias ll='l _long _all'
alias sll='l _long _all _sudo'
alias ld='ll _dir'
xs() {
_checkargs $# 1 || return 1
[[ "$1" == "___strict___" ]] && strict="true" && shift
if command -v rg &> /dev/null;then
[[ "${strict:-}" == "true" ]] && args="-s" || args="-i"
rg ${args:-} "$@"
else
[[ "${strict:-}" != "true" ]] && args="-i"
grep -ER ${args:-} "$@" .
fi
}
xss() {
_checkargs $# 1 || return 1
xs "___strict___" "$@"
}
xf() {
_checkargs $# 1 || return 1
[[ "$1" == "___strict___" ]] && strict="true" && shift
if command -v fd &> /dev/null;then
[[ "${strict:-}" == "true" ]] && args="-s" || args="-i"
fd -j1 ${args:-} "$@"
else
[[ "${strict:-}" != "true" ]] && args="-iname"
find ${args:-} "*$**"
fi
}
xfs() {
_checkargs $# 1 || return 1
xf "___strict___" "$@"
}
hh() { if [ -z "$*" ]; then history 1; else history 1 | grep -E "$@"; fi; }
alias jp="japanesec"
japanese() {
_checkargs $# 1 || return 1
python $DOTFILES/scripts/japanese-get-kana.py "$@"
}
japanesec() {
_checkargs $# 1 || return 1
p=$(japanese "$@")
echo $p
echo -n $p | xcp
echo "Copied to clipboard"
}
rand() {
python $DOTFILES/scripts/password.py "$@"
}
randc() {
rand "$@" | tr -d '\n' | xcp
echo "Copied to clipboard"
}
randp() {
password=$(rand "$@" | tr -d '\n')
echo "${#password} $password"
echo -n "$password" | xcp
echo "Copied to clipboard"
}
up() {
if [ -z "$*" ]; then 1='1';fi
pd=`pwd`
cd $(eval printf '../'%.0s {1..$1}) && echo "${pd} -> $(pwd)";
}
fawk() {
_checkargs $# 1 || return 1
[[ -n "$2" ]] && local sep="-F$2"
local first="awk $sep '{print "
local last="}'"
local cmd="${first}\$${1}${last}"
eval $cmd
}
pkla() {
_checkargs $# 1 || return 1
ps aux | grep -v grep | grep -i -e $1 | awk '{print $2}' | xargs kill -9
}
gitkf() {
gitk_follow () {
while (( "$#" )); do
git log -p --oneline --name-status --follow $1;
shift;
done | perl -ne 'if( s{^(?:[ACDMRTUXB]|R\d+)\s+}{} ) { s{\s+}{\n}g; print; }' | sort -u
}
gitk $(gitk_follow $*)
}
man() {
env LESS_TERMCAP_mb=$'\E[01;31m' \
LESS_TERMCAP_md=$'\E[01;38;5;74m' \
LESS_TERMCAP_me=$'\E[0m' \
LESS_TERMCAP_se=$'\E[0m' \
LESS_TERMCAP_so=$'\E[38;5;246m' \
LESS_TERMCAP_ue=$'\E[0m' \
LESS_TERMCAP_us=$'\E[04;38;5;146m' \
man "$@"
}
changehostname() {
if [ $# -lt 2 ]; then
echo "Usage: changehostname centos|ubuntu hostname"
return 1
fi
os="$1"
nhostname="$2"
case "$os" in
centos)
[[ -r '/etc/sysconfig/network' ]] && sudo sed -i "/HOSTNAME/ s/^.*$/HOSTNAME=${nhostname}/" /etc/sysconfig/network || { echo "File not found"; return -1; }
;;
ubuntu)
[[ -r '/etc/hostname' ]] && echo $nhostname | sudo tee /etc/hostname > /dev/null
;;
*) echo "No match!"
;;
esac
sudo hostname "$nhostname"
[[ -r '/etc/hosts' ]] && grep -v "$nhostname" /etc/hosts > /dev/null || sudo sed -i "/127.0.0.1/ s/$/ $nhostname/" /etc/hosts
}
xdeletefromgit() {
_checkargs $# 1 || return 1
# make sure we're at the root of git repo
if [ ! -d .git ]; then
echo "Error: must run this script from the root of a git repository"
exit 1
fi
# remove all paths passed as arguments from the history of the repo
files=$@
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch $files" HEAD
# remove the temporary history git-filter-branch otherwise leaves behind for a long time
rm -rf .git/refs/original/ && git reflog expire --all && git gc --aggressive --prune
}
xreauthorgit() {
_checkargs $# 3 || return 1
git filter-branch --env-filter '
OLD_EMAIL="'"$1"'"
CORRECT_NAME="'"$2"'"
CORRECT_EMAIL="'"$3"'"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
}
xextract() {
if [[ -f "$1" ]]; then
case "$1" in
*.lrz) lrztar -d "$1" ;;
*.tar.bz2) tar xjf "$1";;
*.tar.gz) tar xzf "$1" ;;
*.tar.xz) tar Jxf "$1" ;;
*.bz2) bunzip2 "$1";;
*.rar) rar x "$1";;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
xgitc() {
if [ "$1" = "" ]; then
mstatus="updates: $(date +'%Y-%m-%d %H:%M:%S')"
printf "You've not set any message: '$mstatus' is being used [Y|n]: "
read inp
if [ "$inp" = "n" ]; then
return 1
fi
else
mstatus="$1"
fi
git add -A .
git commit -a -m "$mstatus"
if [ "$2" != "" ]; then
git push -u origin master
fi
}
xgiaction() {
if [ $# -lt 1 ];then
echo "Usage: xgiaction action [arguments]"
return
fi
walkfolderandexecute() {
pushd "$1" > /dev/null || return -1
shift
if [ -d ".git" ];then
printf "\n********git $1 in $(pwd)********\n"
git $*
elif [ -d ".bzr" ];then
if [ "$1" = "pull" ];then
printf "\n********Updating bzr repo $(pwd)********\n"
bzr update
fi
elif [ -d ".svn" ];then
if [ "$1" = "pull" ];then
printf "\n********Updating svn repo $(pwd)********\n"
svn fetch
fi
else
printf "\n********Entering $(pwd)********\n"
for i in *;do
if [ -d "$i" ];then
walkfolderandexecute "$i" $*
fi
done
fi
popd > /dev/null || return -1
}
walkfolderandexecute "." $*
}
xuploadpicasa() {
if [ $# -lt 1 ];then
echo "Usage: xuploadpicasa albumname [nocreate]"
return 1
fi
f="resized"
n=$1
mkdir -p "$f"
for i in *;do
echo $i
[[ -f "$i" ]] && convert "$i" -resize "2048x2048>" "$f/$i"
done
cd "$f" && \
[[ "$2" != "nocreate" ]] && google -v --access=private picasa create "$n" || echo "Reusing $1" && \
google -v picasa post "$n" * && \
cd ..
}
xmakecustomarchiso() {
_checkargs $# 1 || return 1
isoname=sdh-$(basename "$1")
isolabel=$(isoinfo -d -i "$1" | grep "Volume id" | sed "s/Volume id: //")
outputdir=$(pwd)
sudo umount /tmp/sdh-customiso/mnt > /dev/null 2>&1
rm -rf /tmp/sdh-customiso
echo "Creating dirs..." && \
mkdir -p /tmp/sdh-customiso/{,mnt,contents} && \
cd /tmp/sdh-customiso && \
echo "Mounting iso..." && \
sudo mount -o loop "$1" /tmp/sdh-customiso/mnt/ && \
echo "Extracting contents..." && \
cp -a mnt/* contents && \
cd contents && \
echo "Copying scripts..." && \
rsync --copy-links -vr /home/lfiles/dev/archlinux-install-scripts . && \
chmod +x archlinux-install-scripts/* && \
echo "Creating new iso '$isoname' to $outputdir..." && \
genisoimage -r -V "$isolabel" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o /tmp/sdh-customiso/"$isoname" . && \
mv /tmp/sdh-customiso/"$isoname" $outputdir
echo "Cleaning up..."
sudo umount /tmp/sdh-customiso/mnt
#rm -rf /tmp/sdh-customiso
cd $outputdir
echo "Done."
}
xget() {
if [ $# -lt 1 ]; then
for i in $(cat ~/sshhhh | grep ')' | grep -v '*' | grep -v 'mnet' | cut -f1 -d')' | xargs);do
printf "$i "
code=$(~/sshhhh "$i" | base64 --decode | python2 $DOTFILES/scripts/gauthenticator.py)
printf $code
echo
done
else
code=$(~/sshhhh "$1" | base64 --decode | python2 $DOTFILES/scripts/gauthenticator.py)
printf $code | xclip -selection clipboard
echo "Copied $code to clipboard"
fi
}
xlistfiles() {
case "$1" in
hdd1) 1='/run/media/sdh/sdh-hdd1'
flname=files-hdd1
;;
hdd2) 1='/run/media/sdh/sdh-hdd2'
flname=files-hdd2
;;
hdd3) 1='/run/media/sdh/sdh-hdd3'
flname=files-hdd2
;;
hdd4) 1='/run/media/sdh/sdh-hdd4'
flname=files-hdd2
;;
*) echo "Wrong input"
return -1
;;
esac
find "$1" -type d \( -name ".git" -o -name ".hg" -o -name "Dev" -o -name "\$RECYCLE.BIN" -o -name "System Volume Information" -o -name "version-controlled-soft" -o -name "manuals" -o -name "eclipse" \) -prune -o -print | sort > ~/Downloads/${flname}.txt
}
xintegration() {
git stash && \
git checkout master && \
git pull && \
for i in $(git branch | grep sdh);do
git checkout "$i";
git rebase master;
done && \
git checkout master && \
git branch -D integration || true && \
git checkout -b integration && \
for i in $(git branch | grep sdh);do
git merge "$i";
done
}
xplaylist() {
_checkargs $# 1 || return 1
cd $1
if [ "$2" == "" ];then
ext="mp4"
else
ext=$2
fi
for i in *; do
if [ -d "$i" ];then
echo $i
printf "#EXTM3U\n" > $i.m3u
for j in $(for k in $(ls $i/*\.$ext); do echo $k;done | sort -V); do
printf "#EXTINF:-1,$j\n$j\n" >> $i.m3u
done
fi
done
}
xkdechanges() {
dayz=${1:-7}
for i in *;do
[[ -d "$i/.git" ]] && { [[ -n "$(git --git-dir="$i/.git" --work-tree="$i" log --since="$dayz day ago" --pretty=oneline | grep -v SVN_SILENT)" ]] && cd $i && echo $i && { gitk --since="$dayz day ago" || true; } && cd .. ; }
done
}
xpatternrename() {
if [ $# -lt 2 ]; then
echo "Please give two patterns!"
return 1
fi
for i in *;do x=$(echo $i | sed "s|$1|$2|"); if [ ! -r "$x" ];then echo "$i->$x"; mv "$i" "$x";fi;done
}
xgpp() {
_checkargs $# 1 || return 1
if [ $# -eq 2 ]; then
g++ $1 && ./a.out < $2
else
g++ $1 && ./a.out
fi
rm -f a.out
}
xgcc() {
_checkargs $# 1 || return 1
if [ $# -eq 2 ]; then
gcc $1 && ./a.out < $2 && rm a.out
else
gcc $1 && ./a.out && rm a.out
fi
}
xccreatefolder() {
_checkargs $# 1 || return 1