-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.rc
4875 lines (4219 loc) · 144 KB
/
.rc
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
#
# ~/.rc
# shell functions
# 2024 by mountaineerbr
# https://github.com/mountaineerbr/dotfiles
# __ _ ___
# __ _ ___ __ _____ / /____ _(_)__ ___ ___ ____/ _ )____
# / ' \/ _ \/ // / _ \/ __/ _ `/ / _ \/ -_) -_) __/ _ / __/
#/_/_/_/\___/\_,_/_//_/\__/\_,_/_/_//_/\__/\__/_/ /____/_/
# This file is sourced by ~/.bashrc, ~/.kshrc, and ~/.zshrc
#tmux
#file to hold Tmux signal
_TMUXSIG="${TMPD:-/tmp}/tmux.exit.$EUID"
#exit tmux, and terminal emulator
qq() { [[ -z $TMUX ]] || echo exit >"$_TMUXSIG"; exit ;}
#start tmux
if [[ -n $ZSH_VERSION && -z $TMUX$VIFMSET && $EUID -gt 0 ]]
then
# Check if the line contains "attached"
unset REPLY session;
while IFS= read -r
do case "$REPLY" in *[Aa]ttached*) continue;;
[0-9]:*|[0-9][0-9]:*) session=${REPLY%%:*}; break;;
esac;
done < <(tmux ls)
# If attached session is found, attach to it
if [[ -n $session ]]
then
command tmux attach #attach-session -t $session
# If no detached session found, create a new session
else
command tmux
fi &&
#is exit signal file present?
if [[ -e $_TMUXSIG ]]
then
#execute a simple command
set -- "$(<"$_TMUXSIG")"
command rm "$_TMUXSIG"
eval "$*"
set --
fi
unset REPLY session;
#new-session -n $HOST
#tmux attach \; new-window
#Tmux DBUS (and other envars) on my Arch Linux i7
elif [[ -n $TMUX && -z $DBUS_SESSION_BUS_ADDRESS ]] && pidof -q xfce4-session
then
while read envar
do export "$envar"
done < <(xargs --null --max-args=1 < /proc/$(pidof xfce4-session)/environ \
| grep -e DBUS_SESSION_BUS_ADDRESS -e CDM_SPAWN -e CREDENTIALS_DIRECTORY \
-e DEBUGINFOD_URLS -e GTK3_MODULES -e GTK_MODULES -e '^HG' -e LC_ADDRESS \
-e LC_MEASUREMENT -e LC_MONETARY -e LC_TIME -e LESSOPEN -e MAIL \
-e MOTD_SHOWN -e NVCC_PREPEND_FLAGS -e XDG_RUNTIME_DIR -e XDG_SEAT \
-e XDG_SESSION_CLASS -e XDG_SESSION_ID -e XDG_SESSION_TYPE -e XDG_VTNR)
unset envar
#https://askubuntu.com/questions/772631/how-to-connect-screen-tmux-byobu-to-dbus
#dont use: export $(dbus-launch)
#https://bbs.archlinux.org/viewtopic.php?id=289375
fi
#set xterm font
#{ echo -e "\e]50;-xos4-terminus-medium-r-*--18-*-*-*-*-*-*-*\a" ;}
#sources
[[ -e ~/.apikeys ]] && . ~/.apikeys
[[ -e ~/.rc_xtra ]] && . ~/.rc_xtra
# Set colorful PS1 only on colorful terminals.
# dircolors --print-database uses its own built-in database
# instead of using /etc/DIR_COLORS. Try to use the external file
# first to take advantage of user additions. We run dircolors directly
# due to its changes in file syntax and terminal name patching.
use_color=false
if { [[ -n $BASH_VERSION ]] && type -P dircolors >/dev/null ;} ||
whence -p dircolors >/dev/null
then
# Enable colors for ls, etc. Prefer ~/.dir_colors #64489
LS_COLORS=
if [[ -f ~/.dir_colors ]] ; then
eval "$(dircolors -b ~/.dir_colors)"
elif [[ -f /etc/DIR_COLORS ]] ; then
eval "$(dircolors -b /etc/DIR_COLORS)"
else
eval "$(dircolors -b)"
fi
# Note: We always evaluate the LS_COLORS setting even when it's the
# default. If it isn't set, then `ls` will only colorize by default
# based on file attributes and ignore extensions (even the compiled
# in defaults of dircolors). #583814
if [[ -n ${LS_COLORS:+set} ]] ; then
use_color=true
else
# Delete it if it's empty as it's useless in that case.
unset LS_COLORS
fi
else
# Some systems (e.g. BSD & embedded) don't typically come with
# dircolors so we need to hardcode some terminals in here.
case ${TERM} in
[aEkx]term*|rxvt*|gnome*|konsole*|screen|tmux|cons25|*color) use_color=true;;
esac
fi
if ${use_color} ; then
#if [[ ${EUID} == 0 ]] ; then
# PS1+='\[\033[01;31m\]\h\[\033[01;34m\] \w \$\[\033[00m\] '
#else
# PS1+='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
#fi
##BSD#@export CLICOLOR=1
##GNU#@alias ls='ls --color=auto'
#alias grep='grep --colour=auto'
#alias egrep='grep -E --colour=auto'
#alias fgrep='grep -F --colour=auto'
# Color definitions (taken from Color Bash Prompt HowTo).
# Some colors might look different of some terminals.
# For example, I see 'Bold Red' as 'orange' on my screen,
# hence the 'Green' 'BRed' 'Red' sequence I often use in my prompt.
# Normal Colors # Bold # Background
Black='\e[0;30m' BBlack='\e[1;30m' On_Black='\e[40m' \
Red='\e[0;31m' BRed='\e[1;31m' On_Red='\e[41m' \
Green='\e[0;32m' BGreen='\e[1;32m' On_Green='\e[42m' \
Yellow='\e[0;33m' BYellow='\e[1;33m' On_Yellow='\e[43m' \
Blue='\e[0;34m' BBlue='\e[1;34m' On_Blue='\e[44m' \
Purple='\e[0;35m' BPurple='\e[1;35m' On_Purple='\e[45m' \
Cyan='\e[0;36m' BCyan='\e[1;36m' On_Cyan='\e[46m' \
White='\e[0;37m' BWhite='\e[1;37m' On_White='\e[47m' \
Bold='\u001b[0;1m' Alert=$BWhite$On_Red NC='\e[m' # Color Reset
#Alert: Bold White on red background
#\\e[0;0;0m \\e[m' #\e \E \033 \u001b
#http://tldp.org/LDP/abs/html/sample-bashrc.html
#coloured man pages
man()
{
if [[ "$TERM" = linux ]]
then env \
LESS_TERMCAP_mb=$(printf "\e[34m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[44;93m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[32m") \
/usr/bin/man "$@"
else env \
LESS_TERMCAP_mb=$(printf "\e[1;34m") \
LESS_TERMCAP_md=$(printf "\e[38;5;9m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[44;93m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[38;5;10m") \
/usr/bin/man "$@"
fi
}
#https://boredzo.org/blog/archives/2016-08-15/colorized-man-pages-understood-and-customized
#https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors2
#https://bbs.archlinux.org/viewtopic.php?id=181414
# GRML-zshrc (alternative)
#export LESS_TERMCAP_mb=$'\E[01;31m'
#export LESS_TERMCAP_md=$'\E[01;31m'
#export LESS_TERMCAP_me=$'\E[0m'
#export LESS_TERMCAP_se=$'\E[0m'
#export LESS_TERMCAP_so=$'\E[01;44;33m'
#export LESS_TERMCAP_ue=$'\E[0m'
#export LESS_TERMCAP_us=$'\E[01;32m'
export JQ_COLORS='1;31:0;35:1;35:0;37:0;33:1;39:1;32'
#https://github.com/statico/dotfiles/blob/main/.zshrc
#export JQ_COLORS="48;5;237:48;5;52:48;5;22:38;5;80:38;5;214:1;39:1;39"
else
# show root@ when we don't have colors
PS1='\u@\h \w \$ '
fi
# Try to keep environment pollution down, EPA loves us.
unset use_color
#https://gitweb.gentoo.org/repo/gentoo.git/tree/app-shells/bash/files/bashrc
#https://github.com/magnuskiro/configs/blob/ddb877dbd344fd9000bdcd47fc4b3d44e188b8ba/color.bashrc
#colour packages
#colorize-git, ccat, rainbow, lolcat, ls++
#pipe.sh, cmatrix
#<https://misc.flogisoft.com/bash/tip_colors_and_formatting>
#<https://www.reddit.com/r/linux/comments/94nh4w/what_is_your_ps1/>
#the xhost program is used to add and delete host names or user names to
#the list allowed to make connections to the X server. In the case of hosts,
#this provides a rudimentary form of privacy control and security.
xhost +local:root >/dev/null 2>&1
#also check ~/.Xauthority
#ex - archive extractor
#usage: ex <file>
extract()
{
if [[ -f "$1" ]]
then case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.tbz2) tar xvjf "$1" ;;
*.tgz) tar xvzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*.deb) ar x "$1" ;;
*.zstd) zstd -d "$1" ;;
*.xz) unxz "$1" ;;
*.exe) cabextract "$1" ;;
*.tar.*) tar xvf "$1" ;;
*) echo "'$1' cannot be extracted via ex()" >&2;;
esac
else echo "'$1' is not a valid file" >&2
fi
}
#also check 'unp' script
#bigger font for terminal
alias bigger='echo "setfont iso02-12x22" ;setfont iso02-12x2'
#font list
alias fontlist='fc-list | col | sort | less'
#https://wiki.netbsd.org/tutorials/how_to_use_ttf_fonts_in_xterm/
#check which fonts have got some (unicode) characters:
#{ fc-list :charset=A4B0,A4B1 }
#alias cp="cp -i" # confirm before overwriting something
alias df='df -h' # human-readable sizes
#alias free='free -m' # show sizes in MB
#alias np='nano -w PKGBUILD'
#safeguards for common operations
#removing files: set -I to interactively ask once
alias rm='rm --interactive=always'
#copying: '-i' interactively & recursively on
alias cp='cp --interactive'
#moving
alias mv='mv --interactive'
#grep cmds
#colorise grep (useful for retrieving logs)
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias cgrep='grep --color=always'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias igrep='grep -i'
#'agrep' for approximate search from the 'tre' pkg
#ls, du, df output with commas:
#export BLOCK_SIZE="'1"
#l() { ls -hF --group-directories-first "$@" | cut -c1-22 | column -c 80 ;}
#la() { ls -ahF --group-directories-first "$@" | cut -c1-22 | column -c 80 ;}
alias ll='ls++ --potsf'
#https://unix.stackexchange.com/questions/112335/can-i-truncate-long-file-names-in-ls-listing/112341#112341
#alias l='ls -l'
#alias ll='ls -l'
#alias ls='ls -F'
alias lsd='command ls -lsd'
pl() { printf '%s\n' "$@" ;}
p() { printf '%s\n' "$*" ;}
#alias j='jobs -l'
#alias pu=pushd
#alias po=popd
#print all /bin files
lsb()
{
local dir ar
ar=(${path[@]:-${PATH//:/ }})
for dir in ${ar[@]}
do if (($#))
then ls $dir | grep "$@"
else ls $dir
fi
done
}
#grep file name
lgrep()
{
[[ -n $ZSH_VERSION ]] || shopt -s globstar
printf '%s\n' **/* | grep -i "${@:?name}"
}
#grep file name of all files
llgrep()
(
[[ -n $ZSH_VERSION ]] && setopt dotglob || shopt -s dotglob
lgrep "$@"
)
#parent pid of given process
ppid()
{
local id=${1:-$$}
ps -p $id -o ppid=
#ps j $id
#pstree -sg $id
#grep '^PPid:' /proc/$id/status
}
#https://superuser.com/questions/150117/how-to-get-parent-pid-of-a-given-process-in-gnu-linux-from-command-line
#scripts: parent pid is in shell variable $PPID
#grep id and process
grepid()
{
ps -eo ppid,pid,cmd | awk '{p[$1]=p[$1]","$3}END{ for(i in p) print i, p[i]}'
}
#use pstree -s to get a tree of the process
#https://stackoverflow.com/questions/7138783/how-to-display-the-current-process-tree-of-a-bash-session
#For the full command, not just the name of the program:
pscom()
{
local pid=$1
ps -p $pid -o command
#ps -p $pid -o comm=
}
#https://superuser.com/questions/632979/if-i-know-the-pid-number-of-a-process-how-can-i-get-its-name
# Make your directories and files access rights sane.
sanitize()
{
chmod -R u=rwX,g=rX,o= "$@"
}
#(un)archive and gpg file/dir
gar()
{
#decrypt (sigle file)
if [[ "$1" = *.tar.gpg* ]]
then gpg -d -- "$1" | tar xk
#encrypt (multiple files)
else tar c --format pax -- "$@" | gpg -ci -o "${1%/}".tar.gpg
fi
}
#cli password dialog: gpg --pinentry-mode loopback
#preferably, use gnu/gnutar or pax/posix formats
#(pax format causes extended headers to be extracted as files by a few implementations)
#https://www.baeldung.com/linux/encrypting-decrypting-directory
#https://www.gnu.org/software/tar/manual/html_node/Standard.html
#https://www.gnu.org/software/tar/manual/html_node/gnu.html#SEC145
#https://mgorny.pl/articles/portability-of-tar-features.html
#https://serverfault.com/questions/250511/which-tar-file-format-should-i-use
#also see: `gpgtar' (mainly written due the problems of porting a shell
#script making use of tar (gpg-zip) to Windows; may be slower than
#standard tar on Linux platforms)
#https://lists.gnupg.org/pipermail/gnupg-users/
#forget gpg session passwords
#{ gpg-connect-agent reloadagent /bye ;}
#https://askubuntu.com/questions/349238/how-can-i-clear-my-cached-gpg-password
#silver searcher
alias ag='/usr/bin/ag --nofollow --hidden --smart-case'
#alias grep=ag
#also check vimgrep, ripgrep, agrep
#user agent
#usage: curl/wget --header "$UAG"
#chrome on windows 10
UAG='user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
#simple
UAG2='user-agent: Mozilla/5.0 Gecko'
#ask for compressed data
alias curl='curl -f --compressed'
alias curlu='curl -f -L --compressed --insecure --cookie non-existing --header "$UAG"'
#html filter
htmlfilter()
{
case "$1" in
w3m) w3m -dump -T text/html ${@:2} ;;
elinks) elinks -force-html -dump -no-references ${@:2} ;;
links) links -force-html -dump ${@:2} ;;
lynx) lynx -force_html -dump -nolist $([[ -t 0 ]] || echo -stdin) ${@:2} ;;
sed) sed '/</{ :loop ;s/<[^<]*>//g ;/</{ N ;b loop } }' ${@:2} ;;
*) htmlfilter w3m "$@" ;;
esac
}
alias htmldump=htmlfilter
alias hf=htmlfilter
alias hf2="sed 's/<[^>]*>//g'"
alias hf3="sed -Ee 's/<\/\?(script|style)[^>]*>/\n&\n/g' | sed -Ee '/^<(script|style)[^>]*>/,/^<\/(script|style)>/ d' -e '/</{ :loop ;s/<[^<]*>//g ;/</{ N ;b loop } }'"
#https://www.thegeekstuff.com/2009/12/unix-sed-tutorial-6-examples-for-sed-branching-operation/
#filter html from clipboard
chf()
{
xclip -o -sel clip | htmlfilter
}
#Remove the data between pattern ” ” in a whole file
#{ sed ':loop ;$!{ N ;/\n$/!b loop } ;s/\"[^\"]*\"//g' ;}
#If a line ends with a backslash append the next line to it.
#alias unbackslash="sed ':loop ;/\\$/N ;s/\\\n */ / ;t loop'"
#Commify a numeric strings
alias commify="sed ':loop ;s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/; t loop'"
#Replace every leading space of a line with ‘+’
#{ sed 's/^ */&\n/ ;:loop ;s/^\n//;s/ \n/\n+/ ;t loop' ;}
#curly brackets filter
#{ sed ':again;N;b again; s/{[^}]*}//g' ;}
#https://unix.stackexchange.com/questions/166873/how-can-i-delete-all-text-between-curly-brackets-in-a-multiline-text-file
#css filter
#{ sed "s@/\*.*\*/@@g" | sed '/\/\*/,/\*\//d' ;}
#https://www.linuxquestions.org/questions/programming-9/remove-css-comments-with-sed-776853/
#website links
sedlinks()
{
sed "s|href=[\"']|\n&|g" | sed -nE "s|.*href=['\"]([^'\"]+)['\"].*|\1| p"
}
sedlinks2()
{
perl -ne "while ( m/href=['\"]([^'\"]*)['\"]/gc ) { print \$1, \"\n\"; }"
}
#https://stackoverflow.com/questions/1881237/easiest-way-to-extract-the-urls-from-an-html-page-using-sed-or-awk-only
sedtags()
{
sed -E -e 's/<(p|h1|h2)[ >]/\n&/g' -e 's/<\/(p|h1|h2)>/\n&/g' | sed -E -n -e '/<(p|h1|h2)[ >]/,/<\/(p|h1|h2)>/p'
}
#jq filters
#get all paths of a json
jqpath()
{
jq -r 'def path2text($value):
def tos: if type == "number" then . else tojson end;
reduce .[] as $segment (""; .
+ ($segment
| if type == "string" then "." + . else "[\(.)]" end))
+ " = \($value | tos)";
paths(scalars) as $p
| getpath($p) as $v
| $p | path2text($v)' "$@"
}
jqpath2()
{
jq -rc 'path(..)|[.[]|tostring]|join("/")' "$@"
#jq -r '[path(..)|map(if type=="number" then "[]" else tostring end)|join(".")|split(".[]")|join("[]")]|unique|map("."+.)|.[]' "$@"
}
#https://github.com/stedolan/jq/issues/243
#also see `gron' package to flatten json
#https://www.datafix.com.au/BASHing/2022-03-23.html
#browse usenet
#from w3m man page
w3mu()
{
w3m -m "nntp://news.aioe.org/${1:-comp}"
}
#w3m -m nntp://news.aioe.org/comp.os.linux.networking
#start virtualbox from command line
bsd13()
{
VBoxManage startvm "bsd13" --type headless
}
bsd13halt()
{
ssh -t jack@bsd 'doas shutdown -h now' || return
echo "VBox: \`resume' or \`pause' or \`*poweroff'" >&2
sleep 30
VBoxManage controlvm "bsd13" ${1:-poweroff} --type headless
#resume or pause or poweroff
}
#https://www.techrepublic.com/article/how-to-run-virtualbox-virtual-machines-from-the-command-line/
#check runnign VMs: { VBoxManage list runningvms ;}
#parse sitemap.xml
#{ sed 's|<loc>\(.*\)<\/loc>$|\1|g' ;}
#sitemap() { sed -n 's|.*<loc>\(.*\)<\/loc>.*|\1|g p' ;}
#beautify css
#{ [[ -t 0 ]] && opt=-f || opt=-s
# "$HOME/node_modules/.bin/cssbeautify-cli" "$opt" "$1" ;}
#https://prefetch.net/blog/2017/12/02/formatting-css-from-the-linux-command-line/
#dump page with chromium/chrome
cdump()
{
google-chrome-stable --disable-gpu --headless --dump-dom --user-agent="$UAG" "${@:?address required}"
}
cdump2()
{
chromium --disable-gpu --headless --dump-dom --user-agent="$UAG" "${@:?address required}"
}
#https://developers.google.com/web/updates/2017/04/headless-chrome
#https://intoli.com/blog/making-chrome-headless-undetectable/
#headless chrome and chromium flags
#list of chromium command line switches:
#https://peter.sh/experiments/chromium-command-line-switches/
#http://www.chromium.org/developers/how-tos/run-chromium-with-flags
#headless firefox flags
#https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options
#also see: wkhtmltopdf
#feh automatically scale image to fit window
alias feh='feh --edit --scale-down --auto-zoom -g 640x480 --insecure' #--borderless --draw-filename --draw-tinted
#newsboat
alias nb=newsboat
alias nbrc='vim ~/.newsboat/urls'
#shell history
h()
{
[[ -n "$ZSH_VERSION" ]] && set -- "${1:-0}"
history "$@"
}
#edit grub config
gconf()
{
sudo vim /etc/default/grub
echo "Don't forget to update GRUB!" >&2
}
#grub update hook
alias upgrub='sudo grub-mkconfig -o /boot/grub/grub.cfg'
#visudo
alias visudo='sudo /usr/bin/visudo'
#shutdown
sdh()
{
echo '[halt]' >&2
sudo systemctl poweroff
#sudo poweroff
#sudo halt -p
#sudo shutdown -h now
#xfce4-session-logout --halt
}
#reboot
sdr()
{
echo '[reboot]' >&2
sudo systemctl reboot
#sudo reboot
#sudo halt --reboot
#sudo shutdown -r now
#xfce4-session-logout --reboot
}
#xfce4 logout
out()
{
if ps aux | grep \[x\]fce4-session | grep -q "^$(whoami)"
then if [[ -n "$TMUX" ]]
then echo 'xfce4-session-logout --logout --fast' >"$_TMUXSIG"
exit
else xfce4-session-logout --logout --fast
fi
fi
echo '[nothing to do]' >&2
return 1
}
#OBS: ``$_TMUXSIG'' is defined at ~/.rc file!
#exit tmux and rerun last command
#{
# if [[ -n $TMUX ]]
# then fc -ln -- -1 >"$_TMUXSIG"
# exit
# else fc -s
# fi ;}
#startx
#alias x=startx
##kill detached screen sessions
#{ screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill ;}
##list screen processes
#{ screen -list | grep -Eo "[0-9]{4,}" | xargs -n 1 pstree -p ;}
#https://askubuntu.com/questions/302662/reattaching-to-an-existing-screen-session
#tmux
alias tls='tmux ls'
alias ta='tmux attach-session -t'
#kill dettached tmux sessions
tdk()
{
local line
tmux list-sessions | grep -E -v '\(attached\)$' |
while read line
do tmux kill-session -t "${line%%:*}"
done
}
#https://superuser.com/questions/635904/how-to-delete-a-detached-tmux-session
#kill process
alias ka='killall'
#kill user
alias ku='sudo pkill -15 -u'
#kill sub-processes
#{ kill -- -$$ ;} #kill a process group
#{ kill 0 ;}
#{ pkill -P $$ ;}
#SIGINT = 1 , SIGKILL = 9 , SIGTERM = 15
#list of exit signals
#install core/man-pages -> man 7 signal
#check battery level on arch linux
batt()
{
X=$( < /sys/class/power_supply/BAT0/charge_now )
Y=$( < /sys/class/power_supply/BAT0/charge_full )
python2 -c "z=$X/$Y.; print('%.2f' % z)"
acpi -bi
}
#from the real plato's sober october arch tips
#https://therealplato.com/posts/soberoctober07/
#disable bluetooth
#rfkill block bluetooth
#enable bluetooth
#rfkill unblock bluetooth
#calendar
alias cal='cal -s'
#vifm
#set shell working directory after leaving Vifm
#type :cq[uit] to cancel directory picking
#record working directory leaving Vifm
vf()
{
local recfile="$HOME/.config/vifm/vifmcd.txt"
VIFMSET=1 command vifm --choose-dir "$recfile" "$@"
echo -e "\aPWD: $PWD"
}
((VIFMSET)) && echo -e \\aYou are within VIFM! >&2
#cd to last visited dir in Vifm
vfcd()
{
local recfile="$HOME/.config/vifm/vifmcd.txt"
cd "$(<"$recfile")" || return
echo -e "\aPWD: $PWD"
}
#see another idea from:
#https://wiki.vifm.info/index.php?title=How_to_set_shell_working_directory_after_leaving_Vifm
#vifm command history
vifmh()
{
local vifminfo="$HOME/.config/vifm/vifminfo.json"
jq -r '.["cmd-hist"][] | "\(.ts|strflocaltime("%Y-%m-%dT%H:%M:%S%Z")) text: \(.text)"' "$vifminfo"
#also: prompt-hist
}
#alias vimdiff='vimdiff -o "+colorscheme default" "+syntax off" "+set t_Co=16"'
# Ref vimdiff colurs: https://stackoverflow.com/questions/2019281/load-different-colorscheme-when-using-vimdiff
alias diff='/usr/bin/diff -W$(tput cols) -y --suppress-common-lines'
#https://www.reddit.com/r/git/comments/6mnnag/view_modified_files_in_the_last_n_commits_in_git/
#vim
alias v=vim
alias e=vim
#vim -u NONE <-- starts Vim without any vimrc
#vim -u NORC <-- starts Vim without any vimrc, but with plugins
#open last vim file
#register nought "'0" jumps back to where you exited last
alias vimlast='vim -c "normal '"'"'0"'
alias vlast=vimlast
#cd to vim backup
alias vbak='cd ~/.vim/bak'
#cd to vim swap
vswap()
{
vim -r
cd ~/.vim/swap || return
echo "$PWD"
}
#pager
#alias more=less
#tip: use less -R to respect ANSI color escapes
alias lss=less
#vim pager
les()
{
vim -c 'runtime! macros/less.vim' "$@"
}
alias vless=les
#https://github.com/isaacs/.vim/blob/master/macros/less.sh
vman()
{
COLUMNS=$((COLUMNS-5)) /usr/bin/man ${@-man} | col -bpx | iconv -c | vim -c 'set ft=man nomod' -c 'set noeb vb t_vb=' -c 'map q <Esc>:q!<cr>' -c 'map i <nop>' -
}
#copy less buffer
#in less --help:
#|Xcommand Pipe file between current pos & mark X to shell command.
#a mark is any upper-case or lower-case letter. Certain marks are predefined:
# ^ means beginning of the file
# $ means end of the file
#so if you go to the top of the buffer (<) and then:
#|$cat > /tmp/foo.txt
#the contents of the buffer will be written out to /tmp/foo.txt.
#set bat pager offset
#alias bat='bat --terminal-width -2'
alias bat='bat --style=changes,snip,header'
#move to trash
trash()
{
local d f g trash
trash=$HOME/.local/share/Trash/files
d=$(date +'%Y-%m-%dT%H-%M-%S')
for f
do f=${f%%/} g=${f##*/}
[[ $g == .* ]] || [[ ${g%.*} == ${g} ]] || g=${g%.*}.${g##*.}
/usr/bin/mv -v -- "$f" "${trash}/${g}"
printf '%s\t%s\t%s\n' "$d" "$PWD" "$f" | tee -a "${trash}/.${g}" >/dev/null
done
}
#reload ~/.Xresources
rr()
{
xrdb -remove
sleep 2
xrdb -load ~/.Xresources
sleep 2
xrdb -query
echo '[.Xresources reloaded]' >&2
}
#xbindkeys rebind keys
kk()
{
#OBS: avoid starting xbindkeys in TMUX
killall -15 xbindkeys
sleep 1
xbindkeys #-f ~/.xbindkeysrc
sleep 1
xbindkeys --show
}
#edit c files
alias vrc='vim ~/.vimrc'
alias vfrc='vim ~/.config/vifm/vifmrc'
alias pkglist='vim ~/arq/docs/archInstallPkgSuggestion.txt'
#edit and source
sbrc()
{
[[ -n "$BASH_VERSION" ]] && exec bash
}
szrc()
{
[[ -n "$ZSH_VERSION" ]] && exec zsh
}
#~/.bashrc
brc()
{
vim ~/.bashrc
sbrc
}
#~/.zshrc
zrc()
{ vim ~/.zshrc
szrc
}
#~/.rc
rc()
{
vim ~/.rc
szrc || sbrc
}
#git aliases
#use gitk --all for a gui interface
#install git and tk
#gnu git viewer: gitg
#use git log or gitk to get hashes
#git diff HEAD versionHash [file]
#https://stackoverflow.com/questions/3338126/how-do-i-diff-the-same-file-between-two-different-commits-on-the-same-branch
#git commit
gcom()
{
git commit -m "${*:-sync..}"
}
#git push
gpush()
{
git push origin "${1:-master}"
}
#git diff
gdiff()
{
git diff HEAD~${1:-1} HEAD -- $2
#filename as $2 is optional
}
#{ git diff --name-only HEAD~${1:-1} HEAD ;}
#alias gdca='git diff --cached' # diff between your staged file and the last commit
#alias gdcc='git diff HEAD{,^}' # diff between your latest two commits
#https://stackoverflow.com/questions/1191282/how-to-see-the-changes-between-two-commits-without-commits-in-between
#compare files
#{ git diff --no-index dir1/ dir2/ ;}
#{ diff -r dir1/ dir2/ ;}
#{ comm -23 <(ls dir1 |sort) <(ls dir2|sort) ;} #files in dir1 and not in dir2
#https://stackoverflow.com/questions/16787916/find-the-files-existing-in-one-directory-but-not-in-the-other
#find + md5sum + diff at: https://askubuntu.com/questions/421712/comparing-the-contents-of-two-directories
#simple git sync
gitup()
{
git add -A && git commit -m "${*:-sync}" && git push
#git add -A && git commit -m "${*:-sync}"; git push;
}
#retrieve the remote git address of a repo?
gaddr()
{
git remote -v
}
#https://stackoverflow.com/questions/8816107/how-can-i-retrieve-the-remote-git-address-of-a-repo
#remove a release tag
#{ git push --delete origin tagname ;}
#Resize the image if it is larger than the specified dimensions.
#This will automatically preserve the aspect ratio of the image too.
thumblr()
{
local f size
[[ ${size:=$1} = [0-9Xx]*[!a-wyzA-WYZ] ]] && shift || size=200x150
for file in "$@"
do convert "$file" -resize $size\> "${file%.*}_thumb.${file##*.}"
done
}
#OBS: prefer to use programme `thumb' from imagemagickA6
# Using FFMPEG to Extract a Thumbnail from a Video
#{ ffmpeg -i InputFile.FLV -vframes 1 -an -s 400x222 -ss 30 OutputFile.jpg ;}
#https://networking.ringofsaturn.com/Unix/extractthumbnail.php
# Meaningful thumbnails for a Video using FFmpeg
#{ ffmpeg -ss 3 -i input.mp4 -vf "select=gt(scene\,0.4)" -frames:v 5 -vsync vfr -vf fps=fps=1/600 out%02d.jpg ;}
#https://superuser.com/questions/538112/meaningful-thumbnails-for-a-video-using-ffmpeg
#Github credentials for repository
#SSH login:
#{ git remote set-url origin [email protected]:mountaineerbr/REPONAME.git ;}
#older (deprecated) login methods:
#{ git config --local user.name [username] ;}
#{ git config --local user.email [email] ;}
#{ git push ;} #will ask to update password
#{ git config --local credential.helper ;}
#{ git config --local --unset user.password ;}
#{ git config --local credential.helper "" ;} ;{ git push origin master ;}
#init new git repo (must have created it on GitHub website)
#gnew()
#{
# local usr repo
# usr=bionota repo="${1:?repo name required}"
# #create a new repository on the command line
# echo "# $repo" >> README.md
# git init &&
# git add README.md &&
# git commit -m "init" &&
# git branch -M main && #used to be `master`
# #or only push an existing repository from the command line
# git remote add origin https://github.com/$usr/$repo.git &&
# git remote set-url origin [email protected]:$usr/$repo.git &&
# git push -u origin main
#}
#reset/refresh cloned repo
gres()
{
git fetch --all && {
git reset --hard "${1:-origin/main}" || {
[[ -z $1 ]] && git reset --hard origin/master ;}
}
#git clean -f -d -x
}
#use a commit hash or 'HEAD^' to revert to last commit
#https://stackoverflow.com/questions/4327708/git-reset-hard-head-leaves-untracked-files-behind/4327720
#remove last n commit
#use: grmlast 2 # removes last 2 commits, otherwise removes last commit
grmlast()
{
[[ -z "$1" ]] && set -- 'HEAD^'
[[ -n "$1" ]] && [[ "${#1}" -le 2 ]] && set -- "HEAD~$1"
git reset --hard "$1" && git push origin -f
}
#https://gist.github.com/CrookedNumber/8964442
#switching between branches in a repository you already have
#overwrite a file with a version of that file from another revision
#checkout to a particular status of a particular file
gout()
{
git checkout HEAD -- "$1"
}
#remove large .git/objects
#this will not keep your old commit history around
grmob()
{
#checkout
git checkout --orphan latest_branch
#add all the files
git add -A
#commit the changes
git commit -am sync
#delete the branch
git branch -D main
#rename the current branch to main
git branch -m main
#finally, force update your repository
git push -f origin main
#push the current branch and set the remote as upstream
git push --set-upstream origin main
}
#https://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-github
#also check 'bgf repo-cleaner' https://rtyley.github.io/bfg-repo-cleaner/
#git grep
ggrep()
{
git rev-list --all | xargs git grep "$@"
}
#https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history
gdate()
{
git log -1 --format="%ad" "${@:?Commit hashes required}"
}
#test if inside a git dir
#{ git rev-parse --is-inside-work-tree ;}
#{ git -C DIR rev-parse --is-inside-work-tree ;}
#download a single file from github repos
#usage: gd [LAB|HUB] [USER] [REPO[/BRANCH]] [SUBREPO/PATH/]FILE
#usage: gd grml grml-etc-core etc/zsh/zshrc
#one positional parameter is read as FILE or a subrepo path to a FILE;
#two args are read as REPO and FILE;
#three arguments give the full address to a git USER, REPO and FILE location.
#must set def git user and repo
gd()
{
local defuser defrepo hub branch url