-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.zsh
More file actions
513 lines (453 loc) · 12.5 KB
/
func.zsh
File metadata and controls
513 lines (453 loc) · 12.5 KB
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
#function yta() {
#mpv --ytdl-format=bestaudio ytdl://ytsearch:"$*"
#}
# Music functions
function yta() {
#PLAYLIST=~/.config/mpd/playlists/yt-playlist.m3u
PLAYLIST=~/Music/Playlists/yt-playlist.m3u
printf "#EXTM3U\n#EXTINF:" > $PLAYLIST
youtube-dl -f bestaudio -j ytsearch:"$*" | jq -cMr .duration | tr '\n' ',' >> $PLAYLIST
youtube-dl -f bestaudio --get-title ytsearch:"$*" >> $PLAYLIST
youtube-dl -f bestaudio -g ytsearch:"$*" >> $PLAYLIST
mpc load $(basename -s .m3u $PLAYLIST)
}
function ytao() {
mpv --ytdl-format=bestaudio ytdl://ytsearch:"$*"
}
function ytaa() {
PLAYLIST=~/Music/Playlists/yt-playlist.m3u
#printf "#EXTM3U\n#EXTINF:" > $PLAYLIST
printf "" > $PLAYLIST
youtube-dl -f bestaudio -g ytsearch:"$*" >> $PLAYLIST
mpc load $(basename -s .m3u $PLAYLIST)
}
function yt() {
mpv ytdl://ytsearch:"$*"
}
function m() {
if [ $# -eq 0 ]
then
TRACK="$(mpc listall -f "%file%\t%title%\t%artist%\t%album%" | fzf | head -n 1 | sed "s/\t.*//")"
else
TRACK="$(mpc listall -f "%file%\t%title%\t%artist%\t%album%" | fzf -f "$*" | head -n 1 | sed "s/\t.*//")"
fi
# exit if no track selected
if [[ $TRACK == "" ]]
then
return
fi
mpc add "$TRACK"
}
function mp() {
if [ $# -eq 0 ]
then
TRACK="$(mpc listall -f "%file%\t%title%\t%artist%\t%album%" | fzf | head -n 1 | sed "s/\t.*//")"
else
TRACK="$(mpc listall -f "%file%\t%title%\t%artist%\t%album%" | fzf -f "$*" | head -n 1 | sed "s/\t.*//")"
fi
# exit if no track selected
if [[ $TRACK == "" ]]
then
return
fi
if $(mpc playlist -f "%file%" | grep -Fxq "$TRACK")
then
mpc play $(mpc playlist -f "%file%" | grep -nFx "$TRACK" | sed "s/:.*//" | head -n 1)
else
mpc add "$TRACK"
mpc play $(mpc playlist | wc -l)
fi
}
# Convenience functions
function search() {
echo $PATH | tr ':' '\n' | xargs -I{} find '{}' -iname $1
}
function cheat() {
curl cheat.sh/$1
}
# automatically run 'ls' after every directory change
# automatically run 'git status' if in a git repository
function chpwd() {
emulate -L zsh
if $(git rev-parse --is-inside-work-tree 2>/dev/null)
then
git status
fi
ls
}
function pdf-merge() {
env gs -sDEVICE=pdfwrite -sOutputFile=$1 -dNOPAUSE -dBATCH -q ${@:2} -c "[ /Title () /DOCINFO pdfmark" -f
}
# print ^C
TRAPINT() {
print -n "^C"
return $(( 128 + $1 ))
}
function n() {
ncmpcpp
}
function ncs() {
ncmpcpp --screen search_engine
}
function t() {
zsh
}
function q() {
if [ $# -gt 0 ]
then
qalc -t "$*"
else
qalc
fi
}
#latex popup
#credit to annoyatron255 for this function
function tx() {
LATEX_DIR=/tmp/latex_temp
mkdir -p $LATEX_DIR
if [[ "$*" != *"edit"* ]]
then
echo -e "\\\\begin{align*}\n\t\n\\\\end{align*}" > $LATEX_DIR/latex_input.tex
fi
nvim +2 $LATEX_DIR/latex_input.tex
echo -E "${$(<$HOME/.vim/templates/shortdoc.tex)//CONTENTS/$(<$LATEX_DIR/latex_input.tex)}" > $LATEX_DIR/latex.tex
( cd $LATEX_DIR ; pdflatex $LATEX_DIR/latex.tex )
pdfcrop --margins 12 $LATEX_DIR/latex.pdf $LATEX_DIR/latex.pdf
pdf2svg $LATEX_DIR/latex.pdf $LATEX_DIR/latex.svg
pdftoppm $LATEX_DIR/latex.pdf $LATEX_DIR/latex -png -f 1 -singlefile -rx 600 -ry 600
if [[ "$*" == *"svg"* ]]
then
nohup xclip -selection clipboard -target image/x-inkscape-svg -i $LATEX_DIR/latex.svg 1>&- 2>&- 0<&-
else
nohup xclip -selection clipboard -target image/png -i $LATEX_DIR/latex.png 1>&- 2>&- 0<&-
fi
}
function ved() {
TMPFILE=$(mktemp)
if [[ $# -eq 0 ]]; then
xclip -out -selection clipboard > $TMPFILE
fi
nvim $TMPFILE
nohup xclip -in -selection clipboard -rmlastnl 1>&- 2>&- 0<&- < $TMPFILE
rm $TMPFILE
}
#ipython sympy environment
function sym() {
ipython ~/.ipython_sympy.py -i --no-banner --no-confirm-exit
}
#set up notes environment
function notes() {
if [ $# -eq 0 ]
then
DATE="$(date "+%Y-%m-%d")"
FOLDER="$DATE"
FILENAME="notes.tex"
else
FOLDER="$*"
FILENAME="$*.tex"
fi
index=0
while [ -d "$FOLDER" ]; do
printf -v FOLDER -- '%s_%01d' "$FOLDER" "$((++index))"
done
mkdir $FOLDER
cd $FOLDER
cp ~/.vim/templates/notes.tex "./$FILENAME"
cp ~/.vim/templates/latexmkrc .
sed -i "s/DATE/$(date "+%B %-d, %Y")/g" "./$FILENAME"
SUBJECT=$(basename "$(dirname "$(dirname "$(pwd)")")" | sed -e "s/\([a-zA-Z]\)\([0-9]\)/\1 \2/g")
SUBJECT=${SUBJECT:u}
TYPE=$(basename "$(dirname "$(pwd)")")
TYPE=${TYPE:u}
case $TYPE in
HOMEWORK)
TYPE="Homework $(basename "$(pwd)" | sed "s/[^0-9]//g")"
;;
NOTE*)
TYPE="Notes"
;;
LAB*)
TYPE="Lab $(basename "$(pwd)" | sed "s/[^0-9]//g")"
;;
QUIZ*)
TYPE="Quiz $(basename "$(pwd)" | sed "s/[^0-9]//g")"
;;
EXAM*)
TYPE="Exam $(basename "$(pwd)" | sed "s/[^0-9]//g")"
;;
*)
TYPE=${(C)TYPE}
;;
esac
sed -i "s/TITLE/$SUBJECT $TYPE/g" "./$FILENAME"
nvim +'$-2' +VimtexCompile "./$FILENAME"
}
#search pdf notes
function notesearch() {
rga --sort path --type pdf "$*"
}
#open todo
function td() {
nvim ~/.vimwiki/wiki/TODO.wiki +0
}
#open the current terminal buffer in vim
function vimbuffer() {
# Paste scrollback to print_file. Terminal specific.
xdotool key --window $WINDOWID ctrl+Print
local print_file="/tmp/urxvt_screen"
local written_file="/tmp/urxvt_buffer.sh"
local prompt_string="$(print -P "$PS1" | sed 's/\x1b\[[0-9;]*m//g')"
# Remove trailing newlines
printf '%s\n' "$(cat "$print_file")" > "$written_file"
# Remove last lines of buffer
tail -n $(tac "$written_file" | grep -nm1 "$prompt_string" | cut -d : -f 1) \
"$written_file" | wc -c | xargs -I {} truncate "$written_file" -s -{}
local scrollback_line_length=$(( $(wc -l < "$written_file") + 1 ))
echo "$prompt_string$PREBUFFER$BUFFER" >> "$written_file"
local byte_offset=$(( ${#PREBUFFER//$'\n'/} + ${#LBUFFER//$'\n'/} + \
$(printf "%s" "$prompt_string" | wc -m) ))
vim "+${scrollback_line_length}" "+normal ${byte_offset} " -- \
"$written_file" </dev/tty
print -Rz - "$(tail -n $(tac "$written_file" | grep -nm1 "$prompt_string" \
| cut -d : -f 1) "$written_file" | tail -c +$(( $(printf "%s" \
"$prompt_string" | wc -c) + 1 )))"
rm "$written_file"
zle send-break
}
rga-fzf() {
RG_PREFIX="rga --files-with-matches"
local file
file="$(
FZF_DEFAULT_COMMAND="$RG_PREFIX '$1'" \
fzf --sort --preview="[[ ! -z {} ]] && rga --pretty --context 5 {q} {}" \
--phony -q "$1" \
--bind "change:reload:$RG_PREFIX {q}" \
--preview-window="70%:wrap"
)" &&
echo "opening $file" &&
xdg-open "$file"
}
rgai-fzf() {
RG_PREFIX="rga --files-with-matches --rga-adapters=+tesseract"
local file
file="$(
FZF_DEFAULT_COMMAND="$RG_PREFIX '$1'" \
fzf --sort --preview="[[ ! -z {} ]] && rga --rga-adapters=+tesseract --pretty --context 5 {q} {}" \
--phony -q "$1" \
--bind "change:reload:$RG_PREFIX {q}" \
--preview-window="70%:wrap"
)" &&
echo "opening $file" &&
xdg-open "$file"
}
function manpdf() {
zathura <(man -Tpdf $*) & disown
}
function vim_session_color() {
if [[ $VIMRUNTIME != "" ]]; then
print -P "%F{51}"
else
printf "\$fg[red]"
fi
}
# credit to annoyatron255 for this function
function shaders() {
SHADER_PATH="$HOME/git/compton-shaders/"
if [ $# -eq 0 ]
then
SHADER="$(find $SHADER_PATH -type f -iname "*.glsl" | fzf --delimiter / --with-nth -1 | head -n 1)"
else
SHADER="$(find $SHADER_PATH -type f -iname "*.glsl" | fzf -f "$*" | head -n 1)"
fi
if [[ -n "$SHADER" || $! -eq "0" ]]
then
killall picom
while killall -0 picom
do
sleep 1
done
picom -b --backend glx --force-win-blend --use-damage --glx-fshader-win "$(cat "$SHADER")"
fi
}
# optical character recognition function
function ocr() {
tesseract =(import png:-) - 2> /dev/null | xclip -selection clipboard 1>&- 2>&-
}
function vsync() {
xrandr --output DisplayPort-0 --set TearFree $1
xrandr --output DisplayPort-1 --set TearFree $2
}
function gamer() {
if [ $# -eq 0 ]
then
vsync on off
pkill picom
pgrep mygestures && pkill mygestures
else
vsync on on
picom &disown
pgrep mygestures
if [ $? -ne 0 ]
then
mygestures --device 'Virtual core pointer' --without-brush
fi
fi
}
function ff() {
firefox $*
}
function clip() {
xclip -selection clipboard -rmlastnl
}
function jpc() {
jupyter console --existing --ZMQTerminalInteractiveShell.banner= --ZMQTerminalInteractiveShell.image_handler=None --no-confirm-exit
}
# Twitch function
# relies on TWITCH_CHANNELS, a string of space-separated channel names, to be sourced from .extra1.zsh
function tw() {
local QUALITY="best"
while getopts ":q:" o; do
case "${o}" in
q)
QUALITY=$OPTARG
shift 2
;;
:)
echo "Error: -${OPTARG} requires an argument"
return
;;
esac
done
if [ $# -gt 0 ]
then
case $1 in
http*)
local CHANNEL=$(echo $1 | grep -oP "\w+$")
local TITLE
if [ $# -gt 1 ]; then
TITLE="$2"
else
TITLE=$(curl $1 | grep -oP '<[^<>]+property="og:description"[^<>]+>' | grep -oP 'content="\K([^"]+)')
fi
echo "Quality: $QUALITY"
streamlink --player mpv --title "$TITLE" --twitch-disable-ads --twitch-low-latency $1 $QUALITY &
chromium --new-window "https://www.twitch.tv/popout/$CHANNEL/chat?popout="
;;
*)
if [ $# -gt 1 ]; then
tw -q $QUALITY "https://www.twitch.tv/$1" $2
else
tw -q $QUALITY "https://www.twitch.tv/$1"
fi
;;
esac
else
local OUTPUT=$(echo $TWITCH_CHANNELS | tr ' ' '\n' | \
parallel 'OUT=$(curl https://www.twitch.tv/{} 2>/dev/null) \
&& echo $OUT | grep isLiveBroadcast > /dev/null \
&& TITLE=$(echo $OUT | grep -oP '"'"'<[^<>]+property="og:description"[^<>]+>'"'"' | \
grep -oP '"'"'content="\K([^"]+)'"'"') \
&& printf "%-17s %s\n" {} $TITLE' | fzf)
local CHANNEL=$(echo $OUTPUT | grep -oP "^([\w]+)")
local TITLE=$(echo $OUTPUT | grep -oP "^[\w]+\s+\K(.*$)")
if [ "$CHANNEL" = "" ]; then
echo "No channel selected"
else
tw -q "$QUALITY" "$CHANNEL" "$TITLE"
fi
fi
}
function dsc() {
if [ $# -gt 0 ]
then
local URL=$(echo $1 | sed -e "s/^https/discord/g")
discord --url -- $URL
else
discord
fi
}
function tdir() {
DSTR=""
if [ $# -gt 0 ]
then
DSTR=$(date "+%Y-%m-%d_$1")
else
DSTR=$(date "+%Y-%m-%d")
fi
mkdir $DSTR && cd $DSTR && pwd
}
function carbonyl() {
podman run -ti --rm fathyb/carbonyl $*
}
function b() {
if [[ $# -eq 0 ]]; then
ddcutil getvcp 10 -d 1
ddcutil getvcp 10 -d 2
elif [[ $# -eq 1 ]]; then
ddcutil setvcp 10 $1 -d 1
ddcutil setvcp 10 $1 -d 2
else
ddcutil setvcp 10 $1 -d 1
ddcutil setvcp 10 $2 -d 2
fi
}
function freq() {
if [[ $# -gt 0 && $1 -eq "off" ]]; then
sudo cpupower frequency-set -g performance
else
sudo cpupower frequency-set -g schedutil
fi
}
function ext() {
find . -type f | grep -oP '\K\.[^\.]+$' | sort | uniq -c | sort -n
}
# Aliases
alias l="ls -lAh"
alias la="ls -lah"
alias lt="ls -lAthr"
#Prevents accidental running of ghostscript command
alias gs="git status"
alias glr="git pull --rebase"
alias gadp="git add -p"
alias gsm="git submodule"
alias gsmu="git submodule update --init --recursive"
alias gdc="git --no-pager diff --compact-summary"
alias gdcs="git --no-pager diff --compact-summary --staged"
alias se="sudo -e"
alias igrep="grep -i"
alias isv='if [[ $VIMRUNTIME != "" ]]; then; echo "Vim session found"; else; echo "No Vim session found"; fi;'
alias bgd="bg && disown"
alias ds="find . -maxdepth 1 -exec du -sh '{}' \; | sort -h | grep -vP '^[^\s]+\s\.$'"
alias j="jump"
alias za="zathura"
alias v="nvim"
alias vim="nvim"
alias de="disown && exit"
alias truestudio="/opt/truestudio/ide/TrueSTUDIO"
alias R="R --quiet"
alias bench='time zsh -i -c "exit"'
alias feh="feh --scale-down --auto-zoom --auto-rotate --auto-reload"
alias ffprobe='ffprobe -hide_banner'
alias ffmpeg='ffmpeg -hide_banner'
alias ffmpeg-vaapi='ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi'
alias lsofstat='lsof | tail -n "+2" | grep -oP "^[^\s]+" | sort | uniq -c | sort -n'
alias bat='bat -p'
alias histo='sort | uniq -c | sort -n'
alias makevars='make -pn | grep -A1 "^# makefile"| grep -v "^#\|^--" | sort | uniq'
alias ns='notesearch'
alias venv='python3 -m venv'
alias rstudio='rstudio-bin'
alias ipython='ipython --no-confirm-exit'
alias op='xdg-open'
alias rstudio='rstudio-bin --no-sandbox'
alias x='exit'
alias rgai='rga --rga-adapters=+tesseract -j$(($(nproc) / 2))'
alias .='cd .'
alias bt='bsdtar'
alias nc='ncat'
alias noa='setarch x86_64 -R'
alias beep='mpv --really-quiet --keep-open=no ~/sync/beep.wav'
alias watch='watch -n1'
alias xxd='xxd -R never'
alias ncdu='ncdu --color dark'
alias cpi='xclip -selection clipboard -target image/png -i'