File: clam.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # clam 27 # 28 # Command-Line Augmentation Module (clam): get the best out of your shell. 29 # 30 # 31 # This is a collection of arguably useful shell functions and shortcuts: 32 # some of these extra commands can be real time/effort savers, ideally 33 # letting you concentrate on getting things done. 34 # 35 # Some of these commands depend on my other scripts from the `pac-tools`, 36 # others either rely on widely-preinstalled command-line apps, or ones 37 # which are available on most of the major command-line `package` managers. 38 # 39 # To use this script, you're supposed to `source` it, so its definitions 40 # stay for your whole shell session: for that, you can run `source clam` or 41 # `. clam` (no quotes either way), either directly or at shell startup. 42 # 43 # Almost all commands defined in this script work with `bash`, `zsh`, and 44 # even `dash`, which is debian linux's default non-interactive shell. Some 45 # of its commands even seem to work on busybox's shell. 46 47 48 case "$1" in 49 -h|--h|-help|--help) 50 # show help message, using the info-comment from this very script 51 awk ' 52 /^case / { exit } 53 /^# +clam$/, /^$/ { gsub(/^# ?/, ""); print } 54 ' "$0" 55 exit 0 56 ;; 57 esac 58 59 60 # dash doesn't support regex-matching syntax, forcing to use case statements 61 case "$0" in 62 -bash|-dash|-sh|bash|dash|sh|/bin/sh) 63 # script is being sourced with bash, dash, or ash, which is good 64 : 65 ;; 66 67 *) 68 case "$ZSH_EVAL_CONTEXT" in 69 *:file) 70 # script is being sourced with zsh, which is good 71 : 72 ;; 73 74 *) 75 # script is being run normally, which is a waste of time 76 printf "\e[7mDon't run this script directly: instead source it\e[0m\n" 77 printf "\e[7mby running '. clam' (without the single quotes).\e[0m\n" 78 printf "\n" 79 printf "\e[7mBefore doing that, you may want to see the help,\e[0m\n" 80 printf "\e[7mby running 'clam -h' (without the single quotes).\e[0m\n" 81 # exiting during shell-startup may deny shell access, even if 82 # the script is being run, instead of being sourced directly 83 ;; 84 esac 85 ;; 86 esac 87 88 89 alias 0='sbs' 90 91 alias 1='bsbs 1' 92 alias 2='bsbs 2' 93 alias 3='bsbs 3' 94 alias 4='bsbs 4' 95 alias 5='bsbs 5' 96 alias 6='bsbs 6' 97 alias 7='bsbs 7' 98 alias 8='bsbs 8' 99 alias 9='bsbs 9' 100 101 # Less with Header n runs `less` with line numbers, ANSI styles, without 102 # line-wraps, and using the first n lines as a sticky-header, so they always 103 # show on top 104 alias lh1='less --header=1 -MKNiCRS' 105 alias lh2='less --header=2 -MKNiCRS' 106 alias lh3='less --header=3 -MKNiCRS' 107 alias lh4='less --header=4 -MKNiCRS' 108 alias lh5='less --header=5 -MKNiCRS' 109 alias lh6='less --header=6 -MKNiCRS' 110 alias lh7='less --header=7 -MKNiCRS' 111 alias lh8='less --header=8 -MKNiCRS' 112 alias lh9='less --header=9 -MKNiCRS' 113 114 # View with Header n runs `less` without line numbers, ANSI styles, without 115 # line-wraps, and using the first n lines as a sticky-header, so they always 116 # show on top 117 alias vh1='less --header=1 -MKiCRS' 118 alias vh2='less --header=2 -MKiCRS' 119 alias vh3='less --header=3 -MKiCRS' 120 alias vh4='less --header=4 -MKiCRS' 121 alias vh5='less --header=5 -MKiCRS' 122 alias vh6='less --header=6 -MKiCRS' 123 alias vh7='less --header=7 -MKiCRS' 124 alias vh8='less --header=8 -MKiCRS' 125 alias vh9='less --header=9 -MKiCRS' 126 127 alias c='cat' 128 alias e='echo' 129 alias r='reset' 130 131 # Awk Print 132 alias ap=abp 133 134 # Book-like MANual, lays out `man` docs as pairs of side-by-side pages; uses 135 # my tool `bsbs` 136 alias bman=bookman 137 138 # load/concatenate BYTES from named data sources 139 # alias bytes='cat' 140 141 # load/concatenate BYTES from named data sources; uses my tool `get` 142 alias bytes='get' 143 144 # Compile C Optimized 145 alias cco='cc -Wall -O2 -s -march=native -mtune=native -flto' 146 147 # Color DMESG 148 alias cdmesg='dmesg --color=always' 149 150 # Colored Json Query runs the `jq` app, allowing an optional filepath as the 151 # data source, and even an optional transformation formula 152 alias cjq='jq -C' 153 154 # CLear Screen 155 alias cls='tput -T xterm reset 2> /dev/null || reset' 156 157 # Compile C Plus Plus Optimized 158 alias cppo='c++ -Wall -O2 -s -march=native -mtune=native -flto' 159 160 # CURL Silent spares you the progress bar, but still tells you about errors 161 alias curls='curl --silent --show-error' 162 163 # dictionary-DEFine the word given, using an online service 164 alias def=define 165 166 # turn JSON Lines into a proper json array 167 # alias dejsonl='jq -s -M' 168 169 # turn json lines into a proper json array using the `jq` app 170 alias dejql='jq -s -M' 171 172 # turn UTF-16 data into UTF-8 173 alias deutf16='iconv -f utf16 -t utf8' 174 175 # edit plain-text files 176 # alias edit='micro' 177 178 # ENV with 0/null-terminated lines on stdout 179 alias env0='env -0' 180 181 # ENV Change folder, runs the command given in the folder given (first) 182 alias envc='env -C' 183 184 # Extended Plain Interactive Grep 185 alias epig='ugrep --color=never -Q -E' 186 187 # Editor Read-Only 188 alias ero='micro -readonly true' 189 190 # Expand 4 turns each tab into up to 4 spaces 191 alias expand4='expand -t 4' 192 193 # run the Fuzzy Finder (fzf) in multi-choice mode, with custom keybindings 194 alias ff='fzf -m --bind ctrl-a:select-all,ctrl-space:toggle' 195 196 # get FILE's MIME types 197 alias filemime='file --mime-type' 198 199 # run `gcc` with all optimizations on and with static analysis on 200 alias gccmax='gcc -Wall -O2 -s -march=native -mtune=native -flto -fanalyzer' 201 202 # hold stdout if used at the end of a pipe-chain 203 alias hold='less -MKiCRS' 204 205 # find all hyperlinks inside HREF attributes in the input text 206 alias hrefs=href 207 208 # make JSON Lines out of JSON data 209 alias jl=jsonl 210 211 # shrink/compact JSON using the `jq` app, allowing an optional filepath, and 212 # even an optional transformation formula after that 213 alias jq0='jq -c -M' 214 215 # show JSON data on multiple lines, using 2 spaces for each indentation level, 216 # allowing an optional filepath, and even an optional transformation formula 217 # after that 218 alias jq2='jq --indent 2 -M' 219 220 # find the LAN (local-area network) IP address for this device 221 alias lanip='hostname -I' 222 223 # run `less`, showing line numbers, among other settings 224 alias least='less -MKNiCRS' 225 226 # try to run the command given using line-buffering for its (standard) output 227 alias livelines='stdbuf -oL' 228 229 # LOAD data from the filename or URI given; uses my `get` tool 230 alias load=get 231 232 # LOcal SERver webserves files in a folder as localhost, using the port 233 # number given, or port 8080 by default 234 alias loser=serve 235 236 # Live RipGrep 237 alias lrg='rg --line-buffered' 238 239 # run `ls` showing how many 4k pages each file takes 240 alias lspages='ls -s --block-size=4096' 241 242 # Listen To Youtube 243 alias lty=yap 244 245 # MAKE IN folder 246 alias makein=mif 247 248 # Multi-Core MaKe runs `make` using all cores 249 alias mcmk=mcmake 250 251 # run `less`, showing line numbers, among other settings 252 alias most='less -MKNiCRS' 253 254 # emit nothing to output and/or discard everything from input 255 alias nil=null 256 257 # Nice Json Query colors JSON data using the `jq` app 258 alias njq=cjq 259 260 # Plain Interactive Grep 261 alias pig='ugrep --color=never -Q -E' 262 263 # Quick Compile C Optimized 264 alias qcco='cc -Wall -O2 -s -march=native -mtune=native -flto' 265 266 # Quick Compile C Plus Plus Optimized 267 alias qcppo='c++ -Wall -O2 -s -march=native -mtune=native -flto' 268 269 # Read-Only Editor 270 alias roe='micro -readonly true' 271 272 # Read-Only Micro (text editor) 273 alias rom='micro -readonly true' 274 275 # Read-Only Top 276 alias rot='htop --readonly' 277 278 # RUN IN folder 279 alias runin='env -C' 280 281 # place lines Side-By-Side 282 # alias sbs='column' 283 284 # Silent CURL spares you the progress bar, but still tells you about errors 285 alias scurl='curl --silent --show-error' 286 287 # Stdbuf Output Line-buffered 288 alias sol='stdbuf -oL' 289 290 # TRY running a command, showing its outcome/error-code on failure; uses my 291 # `verdict` tool 292 alias try='verdict' 293 294 # Time Verbosely the command given 295 alias tv='/usr/bin/time -v' 296 297 # VERTical REVert emits lines in reverse order of appearance 298 alias vertrev='tac' 299 300 # emit lines in reverse order of appearance 301 alias upsidedown='tac' 302 303 # run `cppcheck` with even stricter options 304 alias vetc='cppcheck --enable=portability,style --check-level=exhaustive' 305 306 # run `cppcheck` with even stricter options, also checking for c89 compliance 307 alias vetc89='cppcheck --enable=portability,style --check-level=exhaustive --std=c89' 308 309 # run `cppcheck` with even stricter options 310 alias vetcpp='cppcheck --enable=portability,style --check-level=exhaustive' 311 312 # VET SHell scripts 313 alias vetsh=vetshell 314 315 # check shell scripts for common gotchas, avoiding complaints about using 316 # the `local` keyword, which is widely supported in practice 317 alias vetshell='shellcheck -e 3043' 318 319 # run a command using an empty environment 320 alias void='env -i' 321 322 # turn plain-text from latin-1 into UTF-8; the name is from `vulgarization`, 323 # which is the mutation of languages away from latin during the middle ages 324 alias vulgarize='iconv -f latin-1 -t utf-8' 325 326 # recursively find all files with trailing spaces/CRs 327 alias wheretrails=whichtrails 328 329 # run `xargs`, using zero/null bytes as the extra-arguments terminator 330 alias x0='xargs -0' 331 332 # Xargs Lines, runs `xargs` using whole lines as extra arguments 333 alias xl=xargsl 334 335 # Awk Begin Print 336 abp() { 337 local arg 338 for arg in "$@"; do 339 awk "BEGIN { print (${arg}); exit }" 340 done 341 } 342 343 # APT UPdate/grade 344 aptup() { sudo apt update && sudo apt upgrade "$@"; sudo -k; } 345 346 # emit each argument given as its own line of output 347 args() { [ $# -eq 0 ] || printf "%s\n" "$@"; } 348 349 # AWK in BLOCKS/paragraphs-input mode 350 awkblocks() { 351 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 352 stdbuf -oL awk -F='' -v RS='' "$@" 353 else 354 awk -F='' -v RS='' "$@" 355 fi 356 } 357 358 # AWK using TABS as input/output field-separators 359 awktabs() { 360 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 361 stdbuf -oL awk -F "\t" -v OFS="\t" "$@" 362 else 363 awk -F "\t" -v OFS="\t" "$@" 364 fi 365 } 366 367 # Breathe lines 3: separate groups of 3 lines with empty lines 368 b3() { 369 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 370 stdbuf -oL awk 'NR % 3 == 1 && NR != 1 { print "" } 1' "$@" 371 else 372 awk 'NR % 3 == 1 && NR != 1 { print "" } 1' "$@" 373 fi 374 } 375 376 # Breathe lines 5: separate groups of 5 lines with empty lines 377 b5() { 378 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 379 stdbuf -oL awk 'NR % 5 == 1 && NR != 1 { print "" } 1' "$@" 380 else 381 awk 'NR % 5 == 1 && NR != 1 { print "" } 1' "$@" 382 fi 383 } 384 385 # show an ansi-styled BANNER-like line 386 banner() { printf "\e[7m%-$(tput -T xterm cols)s\e[0m\n" "$*"; } 387 388 # emit a colored bar which can help visually separate different outputs 389 bar() { 390 [ "${1:-80}" -gt 0 ] && printf "\e[48;2;218;218;218m%${1:-80}s\e[0m\n" "" 391 } 392 393 # Breathe Header 3: add an empty line after the first one (the header), 394 # then separate groups of 3 lines with empty lines between them 395 bh3() { 396 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 397 stdbuf -oL awk '(NR - 1) % 3 == 1 { print "" } 1' "$@" 398 else 399 awk '(NR - 1) % 3 == 1 { print "" } 1' "$@" 400 fi 401 } 402 403 # Breathe Header 5: add an empty line after the first one (the header), 404 # then separate groups of 5 lines with empty lines between them 405 bh5() { 406 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 407 stdbuf -oL awk '(NR - 1) % 5 == 1 { print "" } 1' "$@" 408 else 409 awk '(NR - 1) % 5 == 1 { print "" } 1' "$@" 410 fi 411 } 412 413 # emit a line with a repeating block-like symbol in it 414 blocks() { [ "${1:-80}" -gt 0 ] && printf "%${1:-80}s\n" "" | sed 's- -█-g'; } 415 416 # BOOK-like MANual, lays out `man` docs as pairs of side-by-side pages; uses 417 # my tool `bsbs` 418 bookman() { 419 local w 420 w="$(tput -T xterm cols)" 421 w="$((w / 2 - 4))" 422 if [ "$w" -lt 65 ]; then 423 w=65 424 fi 425 MANWIDTH="$w" man "$@" | bsbs 2 426 } 427 428 # split lines using the separator given, turning them into single-item lines 429 breakdown() { 430 local sep="${1:- }" 431 [ $# -gt 0 ] && shift 432 local command='awk' 433 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 434 command='stdbuf -oL awk' 435 fi 436 437 ${command} -F "${sep}" '{ for (i = 1; i <= NF; i++) print $i }' "$@" 438 } 439 440 # CAlculator with Nice numbers runs my tool `ca` and colors results with 441 # my tool `nn`, alternating styles to make long numbers easier to read 442 can() { 443 local arg 444 for arg in "$@"; do 445 [ $# -ge 2 ] && printf "\e[7m%s\e[0m\n" "${arg}" > /dev/stderr 446 ca "${arg}" | nn 447 done 448 } 449 450 # uppercase the first letter on each line, and lowercase all later letters 451 capitalize() { sed -E 's-^(.*)-\L\1-; s-^(.)-\u\1-'; } 452 453 # center-align lines of text, using the current screen width 454 center() { 455 awk -v width="$(tput -T xterm cols)" ' 456 { 457 gsub(/\r$/, "") 458 lines[NR] = $0 459 s = $0 460 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", s) # ANSI style-changers 461 l = length(s) 462 if (maxlen < l) maxlen = l 463 } 464 465 END { 466 n = (width - maxlen) / 2 467 if (n % 1) n = n - (n % 1) 468 fmt = sprintf("%%%ds%%s\n", (n > 0) ? n : 0) 469 for (i = 1; i <= NR; i++) printf fmt, "", lines[i] 470 } 471 ' "$@" 472 } 473 474 # Colored Go Test on the folder given; uses my command `gbm` 475 cgt() { go test "${@:-.}" 2>&1 | gbm '^ok' '^[-]* ?FAIL' '^\?'; } 476 477 # Colored RipGrep ensures app `rg` emits colors when piped 478 crg() { 479 if [ -p /dev/stdout ] || [ -t 1 ]; then 480 rg --line-buffered --color=always "${@:-.}" 481 else 482 rg --color=always "${@:-.}" 483 fi 484 } 485 486 # Compile Rust Optimized 487 cro() { 488 rustc -C lto=true -C codegen-units=1 -C debuginfo=0 -C strip=symbols \ 489 -C opt-level=3 "$@" 490 } 491 492 # emit a line with a repeating cross-like symbol in it 493 crosses() { [ "${1:-80}" -gt 0 ] && printf "%${1:-80}s\n" "" | sed 's- -×-g'; } 494 495 # listen to streaming DANCE music 496 dance() { 497 printf "streaming \e[7mDance Wave Retro\e[0m\n" 498 mpv --really-quiet https://retro.dancewave.online/retrodance.mp3 499 } 500 501 # emit a line with a repeating dash-like symbol in it 502 dashes() { [ "${1:-80}" -gt 0 ] && printf "%${1:-80}s\n" "" | sed 's- -—-g'; } 503 504 # remove commas in numbers, as well as leading dollar signs in numbers 505 decomma() { 506 sed -E 's-([0-9]{3}),-\1-g; s-([0-9]{1,2}),-\1-g; s-\$([0-9\.]+)-\1-g' 507 } 508 509 # remove indentations from lines 510 dedent() { 511 awk ' 512 { 513 lines[NR] = $0 514 if (match($0, /^ +/) && (n == 0 || n > RLENGTH)) n = RLENGTH 515 } 516 517 END { 518 if (n == 0) { 519 for (i = 1; i <= NR; i++) print lines[i] 520 } else { 521 for (i = 1; i <= NR; i++) print substr(lines[i], n + 1) 522 } 523 } 524 ' "$@" 525 } 526 527 dehtmlify() { 528 local command='awk' 529 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 530 command='stdbuf -oL awk' 531 fi 532 533 ${command} ' 534 { 535 gsub(/<\/?[^>]+>/, "") 536 gsub(/&/, "&") 537 gsub(/</, "<") 538 gsub(/>/, ">") 539 gsub(/^ +| *\r?$/, "") 540 gsub(/ +/, " ") 541 print 542 } 543 ' "$@" 544 } 545 546 # expand tabs each into up to the number of space given, or 4 by default 547 detab() { 548 local tabstop="${1:-4}" 549 [ $# -gt 0 ] && shift 550 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 551 stdbuf -oL expand -t "${tabstop}" "$@" 552 else 553 expand -t "${tabstop}" "$@" 554 fi 555 } 556 557 # DIVide 2 numbers 3 ways, including the complement 558 div() { 559 awk -v a="${1:-1}" -v b="${2:-1}" ' 560 BEGIN { 561 gsub(/_/, "", a) 562 gsub(/_/, "", b) 563 if (a > b) { c = a; a = b; b = c } 564 c = 1 - a / b 565 if (0 <= c && c <= 1) printf "%f\n%f\n%f\n", a / b, b / a, c 566 else printf "%f\n%f\n", a / b, b / a 567 exit 568 }' 569 } 570 571 # emit a line with a repeating dot-like symbol in it 572 dots() { [ "${1:-80}" -gt 0 ] && printf "%${1:-80}s\n" "" | sed 's- -·-g'; } 573 574 # show the current Date and Time 575 dt() { 576 printf "\e[38;2;78;154;6m%s\e[0m \e[38;2;52;101;164m%s\e[0m\n" \ 577 "$(date +'%a %b %d')" "$(date +%T)" 578 } 579 580 # show the current Date, Time, and a Calendar with the 3 `current` months 581 dtc() { 582 { 583 # show the current date/time center-aligned 584 printf "%20s\e[38;2;78;154;6m%s\e[0m \e[38;2;52;101;164m%s\e[0m\n\n" \ 585 "" "$(date +'%a %b %d')" "$(date +%T)" 586 # debian linux has a different `cal` app which highlights the day 587 if [ -e /usr/bin/ncal ]; then 588 # fix debian/ncal's weird way to highlight the current day 589 ncal -C -3 | sed -E 's/_\x08(.+)_\x08([^ ]+)/\x1b\[7m\1\2\x1b\[0m/' 590 else 591 cal -3 592 fi 593 } | less -MKiCRS 594 } 595 596 # EDit RUN shell commands, using an interactive editor; uses my tool `leak` 597 edrun() { 598 # dash doesn't support the process-sub syntax 599 # . <( micro -readonly true -filetype shell | leak --inv ) 600 micro -readonly true -filetype shell | leak --inv | . /dev/fd/0 601 } 602 603 # convert EURos into CAnadian Dollars, using the latest official exchange 604 # rates from the bank of canada; during weekends, the latest rate may be 605 # from a few days ago; the default amount of euros to convert is 1, when 606 # not given 607 eur2cad() { 608 local url 609 local site='https://www.bankofcanada.ca/valet/observations/group' 610 local csv_rates="${site}/FX_RATES_DAILY/csv" 611 url="${csv_rates}?start_date=$(date -d '3 days ago' +'%Y-%m-%d')" 612 curl -s "${url}" | awk -F, -v amount="$(echo "${1:-1}" | sed 's-_--g')" ' 613 /EUR/ { for (i = 1; i <= NF; i++) if($i ~ /EUR/) j = i } 614 END { gsub(/"/, "", $j); if (j != 0) printf "%.2f\n", amount * $j } 615 ' 616 } 617 618 # Fix Audio Duration on a separate copy of the file given 619 fad() { ffmpeg -i "${1:-input.m4a}" -acodec copy "${2:-output.dat}"; } 620 621 # get the first n lines, or 1 by default 622 first() { head -n "${1:-1}" "${2:--}"; } 623 624 # Field-Names AWK remembers field-positions by name, from the first input line 625 fnawk() { 626 local code="${1:-1}" 627 [ $# -gt 0 ] && shift 628 629 local buffering='' 630 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 631 buffering='stdbuf -oL' 632 fi 633 634 ${buffering} awk -v OFS="\t" ' 635 NR == 1 { 636 FS = /\t/ ? "\t" : " " 637 $0 = $0 638 for (i in names) delete names[i] 639 for (i = 1; i <= NF; i++) names[$i] = i 640 i = "" 641 } 642 { low = lower = tolower($0) } 643 '"${code}"' 644 ' "$@" 645 } 646 647 # start from the line number given, skipping all previous ones 648 fromline() { tail -n +"${1:-1}" "${2:--}"; } 649 650 # convert a mix of FeeT and INches into meters 651 ftin() { 652 local ft="${1:-0}" 653 ft="$(echo "${ft}" | sed 's-_--g')" 654 local in="${2:-0}" 655 in="$(echo "${in}" | sed 's-_--g')" 656 awk "BEGIN { print 0.3048 * ${ft} + 0.0254 * ${in}; exit }" 657 } 658 659 # Gawk Bignum Print 660 gbp() { gawk --bignum "BEGIN { print $1; exit }"; } 661 662 # glue/stick together various lines, only emitting a line-feed at the end; an 663 # optional argument is the output-item-separator, which is empty by default 664 glue() { 665 local sep="${1:-}" 666 [ $# -gt 0 ] && shift 667 awk -v sep="${sep}" ' 668 NR > 1 { printf "%s", sep } 669 { gsub(/\r/, ""); printf "%s", $0 } 670 END { if (NR > 0) print "" } 671 ' "$@" 672 } 673 674 # GO Build Stripped: a common use-case for the go compiler 675 gobs() { go build -ldflags "-s -w" -trimpath "$@"; } 676 677 # GO DEPendencieS: show all dependencies in a go project 678 godeps() { go list -f '{{ join .Deps "\n" }}' "$@"; } 679 680 # GO IMPortS: show all imports in a go project 681 goimps() { go list -f '{{ join .Imports "\n" }}' "$@"; } 682 683 # go to the folder picked using an interactive TUI; uses my tool `bf` 684 goto() { 685 local where 686 where="$(bf "${1:-.}")" 687 if [ $? -ne 0 ]; then 688 return 0 689 fi 690 691 where="$(realpath "${where}")" 692 if [ ! -d "${where}" ]; then 693 where="$(dirname "${where}")" 694 fi 695 cd "${where}" || return 696 } 697 698 # show Help laid out on 2 side-by-side columns; uses my tool `bsbs` 699 h2() { naman "$@" | bsbs 2; } 700 701 # Highlighted-style ECHO 702 hecho() { printf "\e[7m%s\e[0m\n" "$*"; } 703 704 # show each byte as a pair of HEXadecimal (base-16) symbols 705 hexify() { 706 cat "$@" | od -v -x -A n | awk ' 707 { gsub(/ +/, ""); printf "%s", $0 } 708 END { print "" } 709 ' 710 } 711 712 # Help Me Remember my custom shell commands 713 hmr() { 714 local cmd="bat" 715 # debian linux uses a different name for the `bat` app 716 if [ -e /usr/bin/batcat ]; then 717 cmd="batcat" 718 fi 719 720 "$cmd" \ 721 --style=plain,header,numbers --theme='Monokai Extended Light' \ 722 --wrap=never --color=always "$(which clam)" | 723 sed -e 's-\x1b\[38;5;70m-\x1b[38;5;28m-g' \ 724 -e 's-\x1b\[38;5;214m-\x1b[38;5;208m-g' \ 725 -e 's-\x1b\[38;5;243m-\x1b[38;5;103m-g' \ 726 -e 's-\x1b\[38;5;238m-\x1b[38;5;245m-g' \ 727 -e 's-\x1b\[38;5;228m-\x1b[48;5;228m-g' | 728 less -MKiCRS 729 } 730 731 # convert seconds into a colon-separated Hours-Minutes-Seconds triple 732 hms() { 733 echo "${@:-0}" | sed -E 's-_--g; s- +-\n-g' | awk ' 734 /./ { 735 x = $0 736 h = (x - x % 3600) / 3600 737 m = (x % 3600) / 60 738 s = x % 60 739 printf "%02d:%02d:%05.2f\n", h, m, s 740 } 741 ' 742 } 743 744 # find all hyperlinks inside HREF attributes in the input text 745 href() { 746 local arg 747 local awk_cmd='awk' 748 local grep_cmd='grep' 749 if [ -p /dev/stdout ] || [ -t 1 ]; then 750 grep_cmd='grep --line-buffered' 751 if [ -e /usr/bin/stdbuf ]; then 752 awk_cmd='stdbuf -oL awk' 753 fi 754 fi 755 756 for arg in "${@:--}"; do 757 ${grep_cmd} -i -E -o 'href="[^"]+"' "${arg}" 758 done | ${awk_cmd} '{ gsub(/^href="|"\r?$/, ""); print }' 759 } 760 761 # avoid/ignore lines which case-insensitively match any of the regexes given 762 iavoid() { 763 local command='awk' 764 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 765 command='stdbuf -oL awk' 766 fi 767 768 ${command} ' 769 BEGIN { 770 if (IGNORECASE == "") { 771 m = "this variant of AWK lacks case-insensitive regex-matching" 772 print(m) > "/dev/stderr" 773 exit 125 774 } 775 IGNORECASE = 1 776 777 for (i = 1; i < ARGC; i++) { 778 e[i] = ARGV[i] 779 delete ARGV[i] 780 } 781 } 782 783 { 784 for (i = 1; i < ARGC; i++) if ($0 ~ e[i]) next 785 print 786 got++ 787 } 788 789 END { exit(got == 0) } 790 ' "${@:-^\r?$}" 791 } 792 793 # ignore command in a pipe: this allows quick re-editing of pipes, while 794 # still leaving signs of previously-used steps, as a memo 795 idem() { cat; } 796 797 # ignore command in a pipe: this allows quick re-editing of pipes, while 798 # still leaving signs of previously-used steps, as a memo 799 ignore() { cat; } 800 801 # only keep lines which case-insensitively match any of the regexes given 802 imatch() { 803 local command='awk' 804 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 805 command='stdbuf -oL awk' 806 fi 807 808 ${command} ' 809 BEGIN { 810 if (IGNORECASE == "") { 811 m = "this variant of AWK lacks case-insensitive regex-matching" 812 print(m) > "/dev/stderr" 813 exit 125 814 } 815 IGNORECASE = 1 816 817 for (i = 1; i < ARGC; i++) { 818 e[i] = ARGV[i] 819 delete ARGV[i] 820 } 821 } 822 823 { 824 for (i = 1; i < ARGC; i++) { 825 if ($0 ~ e[i]) { 826 print 827 got++ 828 next 829 } 830 } 831 } 832 833 END { exit(got == 0) } 834 ' "${@:-[^\r]}" 835 } 836 837 # start each non-empty line with extra n spaces 838 indent() { 839 local command='awk' 840 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 841 command='stdbuf -oL awk' 842 fi 843 844 ${command} ' 845 BEGIN { 846 n = ARGV[1] + 0 847 delete ARGV[1] 848 fmt = sprintf("%%%ds%%s\n", (n > 0) ? n : 0) 849 } 850 851 /^\r?$/ { print ""; next } 852 { gsub(/\r$/, ""); printf(fmt, "", $0) } 853 ' "$@" 854 } 855 856 # INSTall APT packages 857 instapt() { sudo apt install "$@"; sudo -k; } 858 859 # emit each word-like item from each input line on its own line; when a file 860 # has tabs on its first line, items are split using tabs alone, which allows 861 # items to have spaces in them 862 items() { 863 local command='awk' 864 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 865 command='stdbuf -oL awk' 866 fi 867 868 ${command} ' 869 FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 } 870 { gsub(/\r$/, ""); for (i = 1; i <= NF; i++) print $i } 871 ' "$@" 872 } 873 874 # listen to streaming JAZZ music 875 jazz() { 876 printf "streaming \e[7mSmooth Jazz Instrumental\e[0m\n" 877 mpv --quiet https://stream.zeno.fm/00rt0rdm7k8uv 878 } 879 880 # show a `dad` JOKE from the web, sometimes even a very funny one 881 joke() { 882 curl --silent --show-error https://icanhazdadjoke.com | fold -s | 883 awk '{ gsub(/ *\r?$/, ""); print }' 884 } 885 886 # JSON Query Lines turns JSON top-level arrays into multiple individually-JSON 887 # lines using the `jq` app, keeping all other top-level values as single line 888 # JSON outputs 889 jql() { 890 local code="${1:-.}" 891 [ $# -gt 0 ] && shift 892 jq -c -M "${code} | .[]" "$@" 893 } 894 895 # JSON Query Keys runs `jq` to find all unique key-combos from tabular JSON 896 jqk() { 897 local code="${1:-.}" 898 [ $# -gt 0 ] && shift 899 jq -c -M "${code} | .[] | keys" "$@" | awk '!c[$0]++' 900 } 901 902 # JSON Keys finds all unique key-combos from tabular JSON data; uses my tools 903 # `jsonl` and `tjp` 904 jsonk() { 905 tjp '[e.keys() for e in v] if isinstance(v, (list, tuple)) else v.keys()' \ 906 "${1:--}" | jsonl | awk '!c[$0]++' 907 } 908 909 # JSON Table, turns TSV tables into tabular JSON, where valid-JSON values are 910 # auto-parsed into numbers, booleans, etc...; uses my tools `jsons` and `tjp` 911 jsont() { 912 jsons "$@" | tjp \ 913 '[{k: rescue(lambda: loads(v), v) for k, v in e.items()} for e in v]' 914 } 915 916 # emit the given number of random/junk bytes, or 1024 junk bytes by default 917 junk() { head -c "$(echo "${1:-1024}" | sed 's-_--g')" /dev/urandom; } 918 919 # get the last n lines, or 1 by default 920 last() { tail -n "${1:-1}" "${2:--}"; } 921 922 # convert pounds (LB) into kilograms 923 lb() { 924 echo "${@:-1}" | sed -E 's-_--g; s- +-\n-g' | 925 awk '/./ { printf "%.2f\n", 0.45359237 * $0 }' 926 } 927 928 # convert a mix of pounds (LB) and weight-ounces (OZ) into kilograms 929 lboz() { 930 local lb="${1:-0}" 931 lb="$(echo "${lb}" | sed 's-_--g')" 932 local oz="${2:-0}" 933 oz="$(echo "${oz}" | sed 's-_--g')" 934 awk "BEGIN { print 0.45359237 * ${lb} + 0.028349523 * ${oz}; exit }" 935 } 936 937 # limit stops at the first n bytes, or 1024 bytes by default 938 limit() { head -c "$(echo "${1:-1024}" | sed 's-_--g')" "${2:--}"; } 939 940 # ensure LINES are never accidentally joined across files, by always emitting 941 # a line-feed at the end of each line 942 lines() { 943 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 944 stdbuf -oL awk 1 "$@" 945 else 946 awk 1 "$@" 947 fi 948 } 949 950 # regroup adjacent lines into n-item tab-separated lines 951 lineup() { 952 local command='awk' 953 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 954 command='stdbuf -oL awk' 955 fi 956 957 local n="${1:-0}" 958 [ $# -gt 0 ] && shift 959 960 if [ "$n" -le 0 ]; then 961 ${command} ' 962 NR > 1 { printf "\t" } 963 { printf "%s", $0 } 964 END { if (NR > 0) print "" } 965 ' "$@" 966 return $? 967 fi 968 969 ${command} -v n="$n" ' 970 NR % n != 1 && n > 1 { printf "\t" } 971 { printf "%s", $0 } 972 NR % n == 0 { print "" } 973 END { if (NR % n != 0) print "" } 974 ' "$@" 975 } 976 977 # LiSt files, showing how many 4K-sized storage blocks they use 978 ls4k() { ls -s --block-size=4096 "$@"; } 979 980 # LiSt MAN pages 981 lsman() { man -k "${1:-.}"; } 982 983 # MARK the current tab with the message given, followed by the current folder; 984 # works only on the `bash` shell 985 mark() { 986 if [ $# -eq 0 ]; then 987 PS1="\[\e[0m\e]0;\w\a\$ " 988 else 989 PS1="\[\e[0m\e]0;${*} \w\a\$ " 990 fi 991 } 992 993 marklinks() { 994 local re='https?://[A-Za-z0-9+_.:%-]+(/[A-Za-z0-9+_.%/,#?&=-]*)*' 995 sed -E 's-('"${re}"')-\x1b]8;;\1\x1b\\\1\x1b]8;;\x1b\\-g' "$@" 996 } 997 998 # Multi-Core MAKE runs `make` using all cores 999 mcmake() { make -j "$(nproc)" "$@"; } 1000 1001 # merge stderr into stdout, which is useful for piped commands 1002 merrge() { "${@:-cat /dev/null}" 2>&1; } 1003 1004 metajq() { 1005 # https://github.com/stedolan/jq/issues/243#issuecomment-48470943 1006 jq -r -M ' 1007 [ 1008 path(..) | 1009 map(if type == "number" then "[]" else tostring end) | 1010 join(".") | split(".[]") | join("[]") 1011 ] | unique | map("." + .) | .[] 1012 ' "$@" 1013 } 1014 1015 # Make In Folder, also showing time and max memory used 1016 mif() { 1017 local f='real %e user %U sys %S mem %M exit %x' 1018 local folder 1019 folder="${1:-.}" 1020 [ $# -gt 0 ] && shift 1021 env -C "${folder}" /usr/bin/time -f "$f" make "$@" 1022 } 1023 1024 # MINimize DECimalS ignores all trailing decimal zeros in numbers, and even 1025 # the decimal dots themselves, when decimals in a number are all zeros 1026 # mindecs() { 1027 # local cmd='sed -E' 1028 # if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 1029 # cmd='sed -E -u' 1030 # fi 1031 # ${cmd} 's-([0-9]+)\.0+\W-\1-g; s-([0-9]+\.[0-9]*[1-9])0+\W-\1-g' "$@" 1032 # } 1033 1034 # Number all lines counting from 0, using a tab right after each line number 1035 n0() { 1036 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 1037 stdbuf -oL nl -b a -w 1 -v 0 "$@" 1038 else 1039 nl -b a -w 1 -v 0 "$@" 1040 fi 1041 } 1042 1043 # Number all lines counting from 1, using a tab right after each line number 1044 n1() { 1045 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 1046 stdbuf -oL nl -b a -w 1 -v 1 "$@" 1047 else 1048 nl -b a -w 1 -v 1 "$@" 1049 fi 1050 } 1051 1052 # NArrow MANual, keeps `man` narrow, even if the window/tab is wide when run 1053 naman() { 1054 local w 1055 w="$(tput -T xterm cols)" 1056 w="$((w / 2 - 4))" 1057 if [ "$w" -lt 80 ]; then 1058 w=80 1059 fi 1060 MANWIDTH="$w" man "$@" 1061 } 1062 1063 # Not AND sorts its 2 inputs, then finds lines not in common 1064 nand() { 1065 # comm -3 <(sort "$1") <(sort "$2") 1066 # dash doesn't support the process-sub syntax 1067 (sort "$1" | (sort "$2" | (comm -3 /dev/fd/3 /dev/fd/4) 4<&0) 3<&0) 1068 } 1069 1070 # Nice DEFine dictionary-defines the words given, using an online service 1071 ndef() { 1072 local arg 1073 local gap=0 1074 local options='-MKiCRS' 1075 1076 if [ $# -eq 0 ]; then 1077 printf "\e[38;2;204;0;0mndef: no words given\e[0m\n" >&2 1078 return 1 1079 fi 1080 1081 if [ $# -eq 1 ]; then 1082 options='--header=1 -MKiCRS' 1083 fi 1084 1085 for arg in "$@"; do 1086 [ "${gap}" -gt 0 ] && printf "\n" 1087 gap=1 1088 printf "\e[7m%-80s\e[0m\n" "${arg}" 1089 curl --silent "dict://dict.org/d:${arg}" | awk ' 1090 { gsub(/\r$/, "") } 1091 /^151 / { 1092 printf "\x1b[38;2;52;101;164m%s\x1b[0m\n", $0 1093 next 1094 } 1095 /^[1-9][0-9]{2} / { 1096 printf "\x1b[38;2;128;128;128m%s\x1b[0m\n", $0 1097 next 1098 } 1099 1 1100 ' 1101 done | less ${options} 1102 } 1103 1104 # listen to streaming NEW WAVE music 1105 newwave() { 1106 printf "streaming \e[7mNew Wave radio\e[0m\n" 1107 mpv --quiet https://puma.streemlion.com:2910/stream 1108 } 1109 1110 # Nice Json Query Lines colors JSONL data using the `jq` app 1111 njql() { 1112 local code="${1:-.}" 1113 [ $# -gt 0 ] && shift 1114 jq -c -C "${code} | .[]" "$@" 1115 } 1116 1117 # empty the clipboard 1118 noclip() { wl-copy --clear; } 1119 1120 # show the current date and time 1121 now() { date +'%Y-%m-%d %H:%M:%S'; } 1122 1123 # Nice Print Awk result; uses my tool `nn` 1124 npa() { 1125 local arg 1126 for arg in "$@"; do 1127 awk "BEGIN { print(${arg}); exit }" 1128 done | nn 1129 } 1130 1131 # Nice Print Python result; uses my tool `nn` 1132 npp() { 1133 local arg 1134 for arg in "$@"; do 1135 python -c "print(${arg})" 1136 done | nn 1137 } 1138 1139 # Nice Size, using my tool `nn` 1140 ns() { wc -c "$@" | nn; } 1141 1142 # emit nothing to output and/or discard everything from input 1143 null() { [ $# -gt 0 ] && "$@" > /dev/null; } 1144 1145 # Print Python result 1146 pp() { 1147 local arg 1148 for arg in "$@"; do 1149 python -c "print(${arg})" 1150 done 1151 } 1152 1153 # PRecede (input) ECHO, prepends a first line to stdin lines 1154 precho() { echo "$@" && cat /dev/stdin; } 1155 1156 # LABEL/precede data with an ANSI-styled line 1157 prelabel() { 1158 printf "\e[7m%-*s\e[0m\n" "$(($(tput -T xterm cols) - 2))" "$*" 1159 cat - 1160 } 1161 1162 # PREcede (input) MEMO, prepends a first highlighted line to stdin lines 1163 prememo() { printf "\e[7m%s\e[0m\n" "$*"; cat -; } 1164 1165 # start by joining all arguments given as a tab-separated-items line of output, 1166 # followed by all lines from stdin verbatim 1167 pretsv() { 1168 awk ' 1169 BEGIN { 1170 for (i = 1; i < ARGC; i++) { 1171 if (i > 1) printf "\t" 1172 printf "%s", ARGV[i] 1173 } 1174 if (ARGC > 1) print "" 1175 exit 1176 } 1177 ' "$@" 1178 cat - 1179 } 1180 1181 # Plain RipGrep 1182 prg() { 1183 if [ -p /dev/stdout ] || [ -t 1 ]; then 1184 rg --line-buffered --color=never "${@:-.}" 1185 else 1186 rg --color=never "${@:-.}" 1187 fi 1188 } 1189 1190 # Quiet MPV 1191 # qmpv() { mpv --quiet "${@:--}"; } 1192 1193 # Quiet MPV 1194 qmpv() { mpv --really-quiet "${@:--}"; } 1195 1196 # ignore stderr, without any ugly keyboard-dancing 1197 quiet() { "$@" 2> /dev/null; } 1198 1199 # keep only lines between the 2 line numbers given, inclusively 1200 rangelines() { 1201 { [ $# -eq 2 ] || [ $# -eq 3 ]; } && [ "${1}" -le "${2}" ] && { 1202 tail -n +"${1}" "${3:--}" | head -n $(("${2}" - "${1}" + 1)) 1203 } 1204 } 1205 1206 # RANdom MANual page 1207 ranman() { 1208 find "/usr/share/man/man${1:-1}" -type f | shuf -n 1 | xargs basename | 1209 sed 's-\.gz$--' | xargs man 1210 } 1211 1212 # REPeat STRing emits a line with a repeating string in it, given both a 1213 # string and a number in either order 1214 repstr() { 1215 awk ' 1216 BEGIN { 1217 if (ARGV[2] ~ /^[+-]?[0-9]+$/) { 1218 symbol = ARGV[1] 1219 times = ARGV[2] + 0 1220 } else { 1221 symbol = ARGV[2] 1222 times = ARGV[1] + 0 1223 } 1224 1225 if (times < 0) exit 1226 if (symbol == "") symbol = "-" 1227 s = sprintf("%*s", times, "") 1228 gsub(/ /, symbol, s) 1229 print s 1230 exit 1231 } 1232 ' "$@" 1233 } 1234 1235 # show a RULER-like width-measuring line 1236 ruler() { 1237 [ "${1:-80}" -gt 0 ] && printf "%${1:-80}s\n" "" | sed -E \ 1238 's- {10}-····╵····│-g; s- -·-g; s-·····-····╵-' 1239 } 1240 1241 # SystemCTL; `sysctl` is already taken for a separate/unrelated app 1242 sctl() { systemctl "$@" 2>&1 | less -MKiCRS; } 1243 1244 # show a unique-looking SEParator line; useful to run between commands 1245 # which output walls of text 1246 sep() { 1247 [ "${1:-80}" -gt 0 ] && 1248 printf "\e[48;2;218;218;218m%${1:-80}s\e[0m\n" "" | sed 's- -·-g' 1249 } 1250 1251 # webSERVE files in a folder as localhost, using the port number given, or 1252 # port 8080 by default 1253 serve() { 1254 if [ -d "$1" ]; then 1255 printf "\e[7mserving files in %s\e[0m\n" "$1" >&2 1256 python3 -m http.server -d "$1" "${2:-8080}" 1257 else 1258 printf "\e[7mserving files in %s\e[0m\n" "${2:-$(pwd)}" >&2 1259 python3 -m http.server -d "${2:-$(pwd)}" "${1:-8080}" 1260 fi 1261 } 1262 1263 # SET DIFFerence sorts its 2 inputs, then finds lines not in the 2nd input 1264 setdiff() { 1265 # comm -23 <(sort "$1") <(sort "$2") 1266 # dash doesn't support the process-sub syntax 1267 (sort "$1" | (sort "$2" | (comm -23 /dev/fd/3 /dev/fd/4) 4<&0) 3<&0) 1268 } 1269 1270 # SET INtersection, sorts its 2 inputs, then finds common lines 1271 setin() { 1272 # comm -12 <(sort "$1") <(sort "$2") 1273 # dash doesn't support the process-sub syntax 1274 (sort "$1" | (sort "$2" | (comm -12 /dev/fd/3 /dev/fd/4) 4<&0) 3<&0) 1275 } 1276 1277 # SET SUBtraction sorts its 2 inputs, then finds lines not in the 2nd input 1278 setsub() { 1279 # comm -23 <(sort "$1") <(sort "$2") 1280 # dash doesn't support the process-sub syntax 1281 (sort "$1" | (sort "$2" | (comm -23 /dev/fd/3 /dev/fd/4) 4<&0) 3<&0) 1282 } 1283 1284 # Show Files (and folders), coloring folders and links 1285 sf() { 1286 local arg 1287 local gap=0 1288 local options='-MKiCRS' 1289 1290 if [ $# -le 1 ]; then 1291 options='--header=1 -MKiCRS' 1292 fi 1293 1294 for arg in "${@:-.}"; do 1295 [ "${gap}" -gt 0 ] && printf "\n" 1296 printf "\e[7m%s\e[0m\n\n" "$(realpath "${arg}")" 1297 gap=1 1298 1299 ls -al --file-type --color=never --time-style iso "${arg}" | awk ' 1300 BEGIN { 1301 drep = "\x1b[38;2;0;135;255m\x1b[48;2;228;228;228m&\x1b[0m" 1302 lrep = "\x1b[38;2;0;135;95m\x1b[48;2;228;228;228m&\x1b[0m" 1303 } 1304 1305 NR < 4 { next } 1306 (NR - 3) % 5 == 1 && (NR - 3) > 1 { print "" } 1307 1308 { 1309 gsub(/^(d[rwx-]+)/, drep) 1310 gsub(/^(l[rwx-]+)/, lrep) 1311 printf "%6d %s\n", NR - 3, $0 1312 } 1313 ' 1314 done | less ${options} 1315 } 1316 1317 # run apps in color-mode, using the popular option `--color=always` 1318 shine() { 1319 local cmd="$1" 1320 [ $# -gt 0 ] && shift 1321 "${cmd}" --color=always "$@" 1322 } 1323 1324 # skip the first n lines, or the 1st line by default 1325 skip() { tail -n +$(("${1:-1}" + 1)) "${2:--}"; } 1326 1327 # skip the last n lines, or the last line by default 1328 skiplast() { head -n -"${1:-1}" "${2:--}"; } 1329 1330 # SLOW/delay lines from the standard-input, waiting the number of seconds 1331 # given for each line, or waiting 1 second by default 1332 slow() { 1333 local seconds="${1:-1}" 1334 [ $# -gt 0 ] && shift 1335 ( 1336 IFS="$(printf "\n")" 1337 awk 1 "$@" | while read -r line; do 1338 sleep "${seconds}" 1339 printf "%s\n" "${line}" 1340 done 1341 ) 1342 } 1343 1344 # Show Latest Podcasts, using my tools `podfeed` and `si` 1345 slp() { 1346 local title 1347 title="Latest Podcast Episodes as of $(date +'%F %T')" 1348 podfeed -title "${title}" "$@" | si 1349 } 1350 1351 # emit the first line as is, sorting all lines after that, using the 1352 # `sort` command, passing all/any arguments/options to it 1353 sortrest() { 1354 awk -v sort="sort $*" ' 1355 FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } 1356 { gsub(/\r$/, "") } 1357 NR == 1 { print; fflush() } 1358 NR >= 2 { print | sort } 1359 ' 1360 } 1361 1362 # SORt Tab-Separated Values: emit the first line as is, sorting all lines after 1363 # that, using the `sort` command in TSV (tab-separated values) mode, passing 1364 # all/any arguments/options to it 1365 sortsv() { 1366 awk -v sort="sort -t \"$(printf '\t')\" $*" ' 1367 FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } 1368 { gsub(/\r$/, "") } 1369 NR == 1 { print; fflush() } 1370 NR >= 2 { print | sort } 1371 ' 1372 } 1373 1374 # emit a line with the number of spaces given in it 1375 spaces() { [ "${1:-80}" -gt 0 ] && printf "%${1:-80}s\n" ""; } 1376 1377 # SQUeeze horizontal spaces and STOMP vertical gaps 1378 squomp() { 1379 local command='awk' 1380 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 1381 command='stdbuf -oL awk' 1382 fi 1383 1384 ${command} ' 1385 FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } 1386 /^\r?$/ { empty = 1; next } 1387 empty { if (n > 0) print ""; empty = 0 } 1388 1389 { 1390 gsub(/^ +| *\r?$/, "") 1391 gsub(/ *\t */, "\t") 1392 gsub(/ +/, " ") 1393 print; n++ 1394 } 1395 ' "$@" 1396 } 1397 1398 # TAC Lines outputs input-lines in reverse order, last one first, and so on... 1399 tacl() { 1400 awk ' 1401 { gsub(/\r$/, ""); lines[NR] = $0 } 1402 END { for (i = NR; i >= 1; i--) print lines[i] } 1403 ' "$@" 1404 } 1405 1406 # TINY GO Build Optimized: a common use-case for the tinygo compiler 1407 tinygobo() { tinygo build -no-debug -opt=2 "$@"; } 1408 1409 # Timed Make, also showing max memory used 1410 tm() { 1411 local f='real %e user %U sys %S mem %M exit %x' 1412 /usr/bin/time -f "$f" make "$@" 1413 } 1414 1415 # show current date in a specifc format 1416 today() { date +'%Y-%m-%d %a %b %d'; } 1417 1418 # get the first n lines, or 1 by default 1419 toline() { head -n "${1:-1}" "${2:--}"; } 1420 1421 # get the processes currently using the most cpu 1422 topcpu() { 1423 local n="${1:-10}" 1424 [ "$n" -gt 0 ] && ps aux | awk ' 1425 NR == 1 { print; fflush() } 1426 NR > 1 { print | "sort -rnk3,3" } 1427 ' | head -n "$(("$n" + 1))" 1428 } 1429 1430 # get the processes currently using the most memory 1431 topmemory() { 1432 local n="${1:-10}" 1433 [ "$n" -gt 0 ] && ps aux | awk ' 1434 NR == 1 { print; fflush() } 1435 NR > 1 { print | "sort -rnk6,6" } 1436 ' | head -n "$(("$n" + 1))" 1437 } 1438 1439 # only keep UNIQUE lines, keeping them in their original order 1440 unique() { 1441 local command='awk' 1442 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 1443 command='stdbuf -oL awk' 1444 fi 1445 1446 ${command} ' 1447 BEGIN { for (i = 1; i < ARGC; i++) if (f[ARGV[i]]++) delete ARGV[i] } 1448 !c[$0]++ 1449 ' "$@" 1450 } 1451 1452 # fix lines, ignoring leading UTF-8_BOMs (byte-order-marks) on each input's 1453 # first line, turning all end-of-line CRLF byte-pairs into single line-feeds, 1454 # and ensuring each input's last line ends with a line-feed; trailing spaces 1455 # are also ignored 1456 unixify() { 1457 local command='awk' 1458 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 1459 command='stdbuf -oL awk' 1460 fi 1461 1462 ${command} ' 1463 FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } 1464 { gsub(/ *\r?$/, ""); print } 1465 ' "$@" 1466 } 1467 1468 # skip the first/leading n bytes 1469 unleaded() { tail -c +$(("$1" + 1)) "${2:--}"; } 1470 1471 # go UP n folders, or go up 1 folder by default 1472 up() { 1473 if [ "${1:-1}" -le 0 ]; then 1474 cd . 1475 else 1476 cd "$(printf "%${1:-1}s" "" | sed 's- -../-g')" || return $? 1477 fi 1478 } 1479 1480 # convert United States Dollars into CAnadian Dollars, using the latest 1481 # official exchange rates from the bank of canada; during weekends, the 1482 # latest rate may be from a few days ago; the default amount of usd to 1483 # convert is 1, when not given 1484 usd2cad() { 1485 local url 1486 local site='https://www.bankofcanada.ca/valet/observations/group' 1487 local csv_rates="${site}/FX_RATES_DAILY/csv" 1488 url="${csv_rates}?start_date=$(date -d '3 days ago' +'%Y-%m-%d')" 1489 curl -s "${url}" | awk -F, -v amount="$(echo "${1:-1}" | sed 's-_--g')" ' 1490 /USD/ { for (i = 1; i <= NF; i++) if($i ~ /USD/) j = i } 1491 END { gsub(/"/, "", $j); if (j != 0) printf "%.2f\n", amount * $j } 1492 ' 1493 } 1494 1495 # What Are These (?) shows what the names given to it are/do 1496 wat() { 1497 local arg 1498 local gap=0 1499 local less_options='-MKiCRS' 1500 1501 if [ $# -eq 0 ]; then 1502 echo "$0" 1503 return 0 1504 fi 1505 1506 if [ $# -lt 2 ]; then 1507 less_options='-MKiCRS --header=1' 1508 fi 1509 1510 for arg in "$@"; do 1511 [ "${gap}" -gt 0 ] && printf "\n" 1512 gap=1 1513 printf "\e[7m%-80s\e[0m\n" "${arg}" 1514 1515 while alias "${arg}" > /dev/null 2> /dev/null; do 1516 arg="$(alias "${arg}" | sed -E "s-^[^=]+=['\"](.+)['\"]\$-\\1-")" 1517 done 1518 1519 if echo "${arg}" | grep -q ' '; then 1520 printf "%s\n" "${arg}" 1521 continue 1522 fi 1523 1524 if declare -f "${arg}"; then 1525 continue 1526 fi 1527 1528 if which "${arg}" > /dev/null 2> /dev/null; then 1529 which "${arg}" 1530 continue 1531 fi 1532 1533 printf "\e[38;2;204;0;0m%s not found\e[0m\n" "${arg}" 1534 done | { less -MKiCRS ${less_options} 2> /dev/null || cat; } 1535 } 1536 1537 # find all WEB/hyperLINKS (https:// and http://) in the input text 1538 weblinks() { 1539 local arg 1540 local re='https?://[A-Za-z0-9+_.:%-]+(/[A-Za-z0-9+_.%/,#?&=-]*)*' 1541 local grep_cmd='grep' 1542 if [ -p /dev/stdout ] || [ -t 1 ]; then 1543 grep_cmd='grep --line-buffered' 1544 fi 1545 1546 for arg in "${@:--}"; do 1547 ${grep_cmd} -i -E -o "${re}" "${arg}" 1548 done 1549 } 1550 1551 # recursively find all files with trailing spaces/CRs 1552 whichtrails() { 1553 if [ -p /dev/stdout ] || [ -t 1 ]; then 1554 rg --line-buffered -c '[ \r]+$' "${@:-.}" 1555 else 1556 rg -c '[ \r]+$' "${@:-.}" 1557 fi 1558 } 1559 1560 # turn all wsl/unix-style full-paths into WINdows-style full-PATHS 1561 winpaths() { sed -E 's-/mnt/(.)/-\u\1:/-' "$@"; } 1562 1563 # XARGS Lines, runs `xargs` using whole lines as extra arguments 1564 xargsl() { 1565 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 1566 stdbuf -oL awk -v ORS='\000' ' 1567 FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } 1568 { gsub(/\r$/, ""); print } 1569 ' | stdbuf -oL xargs -0 "$@" 1570 else 1571 awk -v ORS='\000' ' 1572 FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } 1573 { gsub(/\r$/, ""); print } 1574 ' | xargs -0 "$@" 1575 fi 1576 } 1577 1578 # Youtube Audio Player 1579 yap() { 1580 local url 1581 # some youtube URIs end with extra playlist/tracker parameters 1582 url="$(echo "$1" | sed 's-&.*--')" 1583 mpv "$(yt-dlp -x --audio-format best --get-url "${url}" 2> /dev/null)" 1584 } 1585 1586 # show a calendar for the current YEAR, or for the year given 1587 year() { 1588 { 1589 # show the current date/time center-aligned 1590 printf \ 1591 "%21s\e[38;2;78;154;6m%s\e[0m \e[38;2;52;101;164m%s\e[0m\n\n" \ 1592 "" "$(date +'%a %b %d %Y')" "$(date +'%H:%M')" 1593 # debian linux has a different `cal` app which highlights the day 1594 if [ -e /usr/bin/ncal ]; then 1595 # fix debian/ncal's weird way to highlight the current day 1596 ncal -C -y "$@" | sed -E \ 1597 's/_\x08(.+)_\x08([^ ]+)/\x1b\[7m\1\2\x1b\[0m/' 1598 else 1599 cal -y "$@" 1600 fi 1601 } | { less -MKiCRS 2> /dev/null || cat; } 1602 } 1603 1604 # show the current date in the YYYY-MM-DD format 1605 ymd() { date +'%Y-%m-%d'; }