My dotfiles. Period.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. #!/bin/bash
  2. ## hack to workaround Fedora/Red Hat bug 878428
  3. #shellcheck disable=SC1091
  4. test -f /usr/share/git-core/contrib/completion/git-prompt.sh \
  5. && . /usr/share/git-core/contrib/completion/git-prompt.sh
  6. #######################################################
  7. ### things to do BEFORE host/user-specific settings ###
  8. #######################################################
  9. ### .... ###
  10. ### SUBZ ###
  11. ### '''' ###
  12. prepend() {
  13. #
  14. # pre-pend $2 to filename $1
  15. #
  16. local path=$1
  17. local prefix=$2
  18. local dirpath
  19. local filename
  20. local abspath
  21. abspath=$(readlink -f "$path")
  22. dirpath=$(dirname "$abspath")
  23. filename=$(basename "$abspath")
  24. echo mv "$path" "$dirpath/$prefix-$filename"
  25. }
  26. smv() {
  27. #
  28. # scp but rm the source
  29. #
  30. local src=$1
  31. local dst=$2
  32. test $# -eq 2 || {
  33. echo "i can only 2 argz!" >&2
  34. return 2
  35. }
  36. scp "$src" "$dst" && rm "$src"
  37. }
  38. smvr() {
  39. #
  40. # smv but recursive
  41. #
  42. local src=$1
  43. local dst=$2
  44. test $# -eq 2 || {
  45. echo "i can only 2 argz!" >&2
  46. return 2
  47. }
  48. scp -r "$src" "$dst" && rm -r "$src"
  49. }
  50. snore() {
  51. #
  52. # Sleep loudly-ish for $1 time
  53. #
  54. local stime=${1:-0}
  55. test "$stime" == 0 || echo -e "sleeping for $BASHUM_COLOR_LGREEN$stime$BASHUM_COLOR_NORMAL"
  56. sleep "$stime"
  57. }
  58. dotsleep() {
  59. #
  60. # Sleep with dots for $1 seconds
  61. #
  62. local secs=$1
  63. echo -e "sleeping for $BASHUM_COLOR_LGREEN$secs$BASHUM_COLOR_NORMAL seconds"
  64. local secs_togo=$secs
  65. while test "$secs_togo" -gt 0; do
  66. sleep 1
  67. echo -n .
  68. ((secs_togo--))
  69. done
  70. echo
  71. }
  72. bb() {
  73. #
  74. # Ring the bell after $1 time of sleep
  75. #
  76. local stime=${1:-0}
  77. snore "$stime"
  78. printf '\a'
  79. }
  80. nn() {
  81. #
  82. # Ring the bell and send notification $1 after $2 time
  83. #
  84. local msg=${1:-this is done}
  85. local stime=$2
  86. bb "$stime"
  87. echo -e "hey, $BASHUM_COLOR_LRED$msg$BASHUM_COLOR_NORMAL"
  88. notify-send -a 'nn()' "hey, $msg"
  89. }
  90. pp() {
  91. #
  92. # Ring the bell and send notification $1 and pause
  93. #
  94. local msg=${1:-this is done}
  95. bb
  96. echo -e "hey, \033[1;31m$msg\033[1;0m!"
  97. notify-send -a 'pp()' "hey, $msg"
  98. read -r junk -p "Press Enter to continue"
  99. }
  100. lrep() {
  101. #
  102. # Print each line on STDIN $1 times
  103. #
  104. local rep=${1:-1}; shift
  105. local line
  106. local n
  107. while read -r line; do
  108. n=$rep
  109. while test "$n" -gt 0;
  110. do
  111. echo "$line"
  112. ((n--))
  113. done
  114. done
  115. }
  116. prep() {
  117. #
  118. # Print STDIN $1 times
  119. #
  120. local rep=${1:-1}; shift
  121. local data
  122. data=$(cat)
  123. n=$rep
  124. while test "$n" -gt 0;
  125. do
  126. echo "$data"
  127. echo
  128. ((n--))
  129. done
  130. }
  131. dt() {
  132. #
  133. # Open $1 new terminals
  134. #
  135. __bashum_run_n "${1:-1}" urxvt
  136. }
  137. dT() {
  138. #
  139. # Open $1 new alterminals
  140. #
  141. __bashum_run_n "${1:-1}" xfce4-terminal
  142. }
  143. hr() {
  144. #
  145. # Make horizontal ruler
  146. #
  147. local i
  148. local line
  149. for i in $(seq 1 "$COLUMNS"); do
  150. case $((i%10)) in
  151. 0) line+='#' ;;
  152. *) line+='-' ;;
  153. esac
  154. done
  155. echo "$line"
  156. echo "$line"
  157. echo "$line"
  158. }
  159. __bashum_run_n() {
  160. #
  161. # Run $2.. in background $1 times
  162. #
  163. local n=${1:-1}; shift
  164. while test "$n" -gt 0;
  165. do
  166. "$@" &
  167. ((n--))
  168. done
  169. }
  170. twice() {
  171. #
  172. # Run $@ two times
  173. #
  174. "$@"
  175. "$@"
  176. }
  177. ckyaml() {
  178. #
  179. # Check if YAML file $1 is loadable
  180. #
  181. local yfile=$1
  182. test -n "$yfile" || { echo "usage: ckyaml FILE.yaml"; return 2; }
  183. python -c 'import yaml; import sys; print(yaml.load(sys.stdin)["this"])' <"$yfile"
  184. }
  185. git() {
  186. #
  187. # /usr/bin/git wrapper just to disable dangerous commands
  188. #
  189. # In particular, `git-am` is a a close typo of my favorite
  190. # aliases `ap` (`git-add --patch`) and `cm` (`git-commit`)
  191. # but it's dangerous to call by accident as it destroys
  192. # workdir changes.
  193. #
  194. if grep -Fwqse "$1" "$GIT_DISABLED_COMMANDS"; then
  195. echo "You don't want this." >&2
  196. return 1
  197. else
  198. command git "$@"
  199. fi
  200. }
  201. grepcl() {
  202. #
  203. # Grep in common chat log files
  204. #
  205. # The order is from older apps to newer as I was moving towards
  206. # weechat.
  207. #
  208. grep "$@" -R \
  209. "$HOME/.config/xchat2/xchatlogs" \
  210. "$HOME/.config/xchat2/scrollback" \
  211. "$HOME/.config/hexchat/logs" \
  212. "$HOME/.config/hexchat/scrollback" \
  213. "$HOME/.local/share/weechat/logs.old" \
  214. "$HOME/.local/share/weechat/logs"
  215. }
  216. grepfx() {
  217. #
  218. # Grep recursively for technical debt
  219. #
  220. # '[F]' hack to avoid matching self
  221. #
  222. grep --color -n \
  223. --exclude-dir 'utils' \
  224. --exclude-dir '.git' \
  225. -o '#[F]IXME:.*' -R "$@"
  226. }
  227. grepr() {
  228. #
  229. # Grep recursively, keeping numbers and ignoring common dirs
  230. #
  231. # Number one tool for refactoring!
  232. #
  233. local p=$1; shift
  234. grep --color -n --exclude-dir=".git" -e "$p" -R "$@"
  235. }
  236. grepr1() {
  237. #
  238. # Like grepr() but only list matching items in this directory
  239. #
  240. # Ie. list only files in current directory or names of subdirectories
  241. # where matches were found "somewhere".
  242. #
  243. grepr "$@" -l | cut -d/ -f1 | uniq
  244. }
  245. greph() {
  246. #
  247. # Grep through bash history
  248. #
  249. history | sed 's/^ *//; s/ / /' | cut -d' ' -f2- | grep "$@"
  250. }
  251. gitcd() {
  252. #
  253. # Deep in git repo, cd to the git root
  254. #
  255. cd "$(git rev-parse --show-toplevel)" || return 1
  256. }
  257. clsz() {
  258. #
  259. # Clear screen and move the prompt to bottom
  260. #
  261. tput clear; tput cup "$(tput lines)" 0
  262. }
  263. bcdiff() {
  264. #
  265. # Call bcompare only if objects actually differ
  266. #
  267. test $# -eq 2 && diff "$@" >/dev/null && return
  268. bcompare "$@" &
  269. }
  270. strip_colors() {
  271. #
  272. # Strip color codes from stdin
  273. #
  274. # Stolen from http://unix.stackexchange.com/a/4533/9365
  275. #
  276. sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g'
  277. }
  278. vims() {
  279. #
  280. # List Vim .swp files in a nice table
  281. #
  282. # **Note:** For this ro work, you need to set up Vim to
  283. # use same swap directory as assigned below. this can
  284. # be achieved by addoing section such as this to your
  285. # .vimrc:
  286. #
  287. # if isdirectory($HOME . '/.local/share/vim') == 0
  288. # :silent !mkdir -p ~/.vim/swap >/dev/null 2>&1
  289. # endif
  290. # set directory=./.vim-swap//
  291. # set directory+=~/.local/share/vim/swap//
  292. # set directory+=~/tmp//
  293. # set directory+=.
  294. #
  295. # Call this after crash or poweroff to remind yourself what
  296. # files you were editing before fact. Example:
  297. #
  298. # 2015-07-10 06:10:48.603953758 +0200 ~/TODO.todo
  299. #
  300. # This should make it easier to recover after a little
  301. # disaster.
  302. #
  303. local swap="$HOME/.local/share/vim/swap"
  304. #shellcheck disable=SC1117
  305. find "$swap" -type f -print0 \
  306. | xargs -0 -r stat -c "%y %n" \
  307. | sed "
  308. s| $swap/| |
  309. s|%|/|g
  310. s|.swp$||
  311. s| $HOME| ~|
  312. s| \(..:..\):[^ ]* +.... | \\1 |
  313. " \
  314. | sort \
  315. | tac
  316. }
  317. fixnl() {
  318. #
  319. # Fix newlines in stdin and pass to stdout
  320. #
  321. # Note: We need to read whole stdin; this cannot work
  322. # for infinite stream.
  323. #
  324. # Fix annoying cases:
  325. #
  326. # * If multi-line text is missing newline at the
  327. # end---add the missing newline.
  328. #
  329. # * If -c is passed, and single-line text does have
  330. # a newline at the end, remove it.
  331. #
  332. # This will not touch properly terminated multi-line texts,
  333. # or zero-line texts (ie. oly chars with no newline at all).
  334. #
  335. local arg=$1 # argument passed
  336. local cache # cache to keep stream in
  337. local nlcount # count of newlines in the stream
  338. local lastchr # hex dump (2-digit, lowercase) of last char
  339. local single=keep # single-line streams:
  340. # keep: do nothing
  341. # chop: drop last char if it's a LF
  342. case $arg in
  343. -c|--chop-single) single=chop ;;
  344. esac
  345. cache="$(mktemp -t fixnl.XXXXXXXX)"
  346. cat >"$cache"
  347. nlcount=$(<"$cache" wc -l)
  348. lastchr=$(<"$cache" tail -c1 | hexdump -e '"%02x"')
  349. case $nlcount:$lastchr:$single in
  350. 0:??:*) cat "$cache" ;; # 'abc' => keep as is
  351. 1:0a:chop) head -c -1 "$cache" ;; # 'abc\n' => -c passed: chop!
  352. 1:0a:keep) cat "$cache" ;; # 'abc\n' => keep as is
  353. *:0a:*) cat "$cache" ;; # 'a\nb\nc\n' => keep as is
  354. *:??:*) cat "$cache"; echo ;; # 'a\nb\nc' => add the missing NL
  355. esac
  356. rm "$cache"
  357. }
  358. nlfor() {
  359. #
  360. # Replace $1 (or blank space) with newlines and fix final newline
  361. #
  362. # Shorthand for commonly used tr syntax. Example:
  363. #
  364. # $ echo "$PATH" | nlat :
  365. # /foo
  366. # /bar
  367. # $
  368. #
  369. # is almost like `tr : '\n'` except that it is a bit easier
  370. # to type and fixes the annoying problem with missing
  371. # newline at the end of the replaced string.
  372. #
  373. local char=$1
  374. test -n "$char" || char='[:blank:]'
  375. tr "$char" '\n' \
  376. | fixnl
  377. }
  378. nljoin() {
  379. #
  380. # Join lines with $1 (or space)
  381. #
  382. # Shorthand for commonly used tr syntax. Example:
  383. #
  384. # $ { echo foo; echo bar; } | nljoin :
  385. #
  386. # is almost like `tr '\n' :` except that it is a bit easier
  387. # to type and fixes the annoying problem with extraneous
  388. # newline/colon at the end of final string.
  389. #
  390. local char=$1
  391. test -n "$char" || char=' '
  392. tr '\n' "$char" \
  393. | sed "s/$char*$//" \
  394. | fixnl -c
  395. }
  396. urle() {
  397. #
  398. # Encode or decode (if $1 is '-d') message on stdin as URL encode
  399. #
  400. local fn=quote
  401. case $1 in
  402. -d) fn=unquote ;;
  403. -*) echo 'usage: urle [-d] <text' >&2; return 2 ;;
  404. esac
  405. python3 -c "import sys; from urllib.parse import $fn; print($fn(sys.stdin.read()));"
  406. }
  407. qpr() {
  408. #
  409. # Encode or decode (if $1 is '-d') message on stdin as Quoted Printable
  410. #
  411. local fn=encode
  412. case $1 in
  413. -d) fn=decode ;;
  414. -*) echo 'usage: qpr [-d] <text' >&2; return 2 ;;
  415. esac
  416. perl -MMIME::QuotedPrint -pe "\$_=MIME::QuotedPrint::$fn(\$_);"
  417. }
  418. xop() {
  419. #
  420. # Common clipboard operations with fixnl() on top
  421. #
  422. local op=$1
  423. local file=${2:-/dev/stdin}
  424. case $op in
  425. oo) xclip -selection clipboard -o | fixnl ;;
  426. o) xclip -o | fixnl ;;
  427. ii) fixnl -c | xclip -selection clipboard -i ;;
  428. i) fixnl -c | xclip -i ;;
  429. esac <"$file"
  430. }
  431. xod() {
  432. #
  433. # Like mktemp but get the content from clipboard
  434. #
  435. # Dump xo in a file named $1 somewhere in /tmp; echo the path
  436. # usage: scp $(xod useful_name.txt) eg:some/shared/place
  437. # instead of:
  438. #
  439. # xo > useful_name.txt
  440. # scp useful_name.txt eg:some/shared/place
  441. # rm useful_name.txt
  442. #
  443. local name="${1:-clipboard_dump.txt}"
  444. local tmp
  445. tmp=$(mktemp -d -t "xod.XXXXXXXX")
  446. xclip -o > "$tmp/$name"
  447. echo "$tmp/$name"
  448. }
  449. xood() {
  450. #
  451. # Just like xod() but using xoo (i.e. 'clipboard' clipboard)
  452. #
  453. local name="${1:-clipboard_dump.txt}"
  454. local tmp
  455. tmp=$(mktemp -d -t "xood.XXXXXXXX")
  456. xclip -selection clipboard -o > "$tmp/$name"
  457. echo "$tmp/$name"
  458. }
  459. xl() {
  460. #
  461. # Show primary clipboard in less(1)
  462. #
  463. xop o | less -S
  464. }
  465. xxl() {
  466. #
  467. # Show C-C clipboard in less(1)
  468. #
  469. xop oo | less -S
  470. }
  471. xvxx() {
  472. #
  473. # Edit text in primary clipboard and save it to C-C clipboard
  474. #
  475. # Ideal for editing snippets in browser-based editors: during editing,
  476. # select the text, open terminal, call `xvxx`, edit the text, move
  477. # back to the browser and paste it over the old (still selected) text.
  478. # That's it, no need to make up and save temporary files.
  479. #
  480. # If you quickly realize you need to re-edit the text, use xvvx().
  481. #
  482. xop o | vipe | xop ii
  483. }
  484. xcp() {
  485. #
  486. # Copy paths of $@ to clipboard
  487. #
  488. local path
  489. find "$@" -maxdepth 0 -mindepth 0 \
  490. | while read -r path; do
  491. readlink -e "$path"
  492. done \
  493. | xop i
  494. }
  495. xpst() {
  496. #
  497. # Paste paths from clipboard
  498. #
  499. { xop o; echo; } \
  500. | while read -r path; do
  501. echo "path='$path'" >&2
  502. test -e "$path" || {
  503. echo "does not exist: $path"
  504. continue
  505. }
  506. cp -r "$path" .
  507. done
  508. }
  509. xvb() {
  510. #
  511. # Edit text in primary clipboard and run it using bash
  512. #
  513. xop o | vipe | bash
  514. }
  515. xxvb() {
  516. #
  517. # Edit text in C-C clipboard and run it using bash
  518. #
  519. xop oo | vipe | bash
  520. }
  521. vxx() {
  522. #
  523. # Edit text on stdin and save it to C-C clipboard
  524. #
  525. # Ideal for editing outut of command before pasting ot to browser
  526. # or another GUI application. Run the command with `vxx`, edit
  527. # the text, move back to the application and paste it.
  528. #
  529. # If you quickly realize you need to re-edit the text, use xvvx().
  530. #
  531. vipe | xop ii
  532. }
  533. xcxx() {
  534. #
  535. # Filter text in primary clipboard using command $@ and save it to C-C clipboard
  536. #
  537. # Similar to xvvx() but instead of interactive editing, provided filter
  538. # command is run.
  539. #
  540. local cmd=("$@")
  541. xop o | "${cmd[@]}" | xop ii
  542. return "${PIPESTATUS[1]}"
  543. }
  544. vx() {
  545. #
  546. # Edit stdin and save it to primary clipboard
  547. #
  548. # Ideal for selecting snippets of output of arbitrary commands.
  549. # select the text, open terminal, call `xvx`, edit the text, move
  550. # back to the browser and paste it over the old (still selected) text.
  551. # That's it, no need to make up and save temporary files.
  552. #
  553. vipe | xop i
  554. }
  555. xxvxx() {
  556. #
  557. # Edit text in C-C clipboard and save it back
  558. #
  559. xop oo | vipe | xop ii
  560. }
  561. yt2ogg() {
  562. #
  563. # Download YouTube video as vorbis audio
  564. #
  565. local url=$1
  566. youtube-dl -x --audio-format vorbis "$url"
  567. }
  568. yum_hasbin() {
  569. #
  570. # Ask yum who has bin $1 (and make the output actually readable)
  571. #
  572. local bname
  573. for bname in "$@";
  574. do
  575. case $bname in
  576. */*) dnf provides "$bname" ;;
  577. *) dnf provides "*/bin/$bname" ;;
  578. esac \
  579. | grep '^.' \
  580. | grep -E '^[[:alnum:]_:.-]+ :'
  581. done
  582. }
  583. cal() {
  584. #
  585. # cal with the proper version pick
  586. #
  587. local cal_v
  588. local cal_es
  589. cal_v=$(command cal -V 2>/dev/null); cal_es=$?
  590. case $cal_es:$cal_v in
  591. 0:cal\ from\ util-linux\ 2.*)
  592. command cal --color=always --monday --week "$@"
  593. ;;
  594. 64:*)
  595. command cal -M "$@"
  596. ;;
  597. esac
  598. }
  599. ### .... ###
  600. ### BASH ###
  601. ### '''' ###
  602. HISTCONTROL=ignoredups:erasedups
  603. # shopt -s histappend
  604. # PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
  605. HISTIGNORE="$HISTIGNORE:ls:ll:la:cd"
  606. HISTIGNORE="$HISTIGNORE:git st"
  607. HISTIGNORE="$HISTIGNORE:se *:sc *"
  608. HISTSIZE=33333
  609. HISTFILESIZE=33333
  610. HISTTIMEFORMAT='%F %T '
  611. GLOBIGNORE=.:..
  612. # shopt -s histverify
  613. # some more aliases
  614. alias cls='clear'
  615. alias czkrates='czkrates -v'
  616. alias ll='ls -lh'
  617. alias lla='ls -lha'
  618. alias lr='ll -lhrt'
  619. alias open='xdg-open'
  620. alias diff='colordiff -u'
  621. alias dmesg='dmesg --time-format iso'
  622. alias pad4='sed -e "/./s/^/ /"'
  623. alias mkittool='mkittool -v'
  624. alias grep='grep --color'
  625. alias sc='se --direction encz.cz'
  626. alias taskm='task modify'
  627. alias tasks='task rc.context:none'
  628. alias ts='ts "%F %T"'
  629. alias lsblk='lsblk -o +UUID,LABEL'
  630. alias pstree='pstree -h'
  631. alias virsh='virsh --connect qemu:///system'
  632. alias wttr='curl -s "wttr.in/?1&n&q"'
  633. alias xaa='xclip -o | audit2allow'
  634. alias xi='xop i'
  635. alias xii='xop ii'
  636. alias xo='xop o'
  637. alias xoo='xop oo'
  638. alias reboot="echo -n . ; sync ; echo -n . ; sync ; echo -n . ; systemctl reboot"
  639. alias poweroff="echo -n . ; sync ; echo -n . ; sync ; echo -n . ; systemctl poweroff"
  640. x4x() {
  641. #
  642. # Pad text from primary clipboard with 4 spaces; save to C-C clipboard
  643. #
  644. # ...that is, for inclusion as Markdown verbatim text.
  645. #
  646. xo | pad4 | xii
  647. }
  648. BASHUM__RV_TMP="/tmp/bashum-rv"
  649. mkdir -p "$BASHUM__RV_TMP"
  650. ### ...... ###
  651. ### OTHERS ###
  652. ### '''''' ###
  653. export LC_COLLATE=C # make sort upper first
  654. export EDITOR=vim
  655. export LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s"
  656. export LESS=' -R '
  657. export NEATY=color
  658. export PRETTY=color
  659. export PRETTY_DEBUG_EXCLUDE=inigrep
  660. export MDVIMB__CONVERTER=cmark
  661. export MDVIMB__STYLE=darkzen # pergamen
  662. #shellcheck disable=SC2034
  663. {
  664. GIT_PS1_SHOWDIRTYSTATE=true
  665. GIT_PS1_SHOWUNTRACKEDFILES=true
  666. __SATURNIN_COMPLETE_DEBUG=true
  667. GIT_DISABLED_COMMANDS="$HOME/.gittum/disabled-commands"
  668. }
  669. export GIT_PAGER='less -S'
  670. # disable mounting things like SFTP to ~/.gvfs when accessed
  671. # via GIO (used by nautilus etc.)
  672. export GVFS_DISABLE_FUSE=1
  673. # disable the terrible beep sound (only for X; for tty?, blacklist pcspkr)
  674. xhost >& /dev/null && xset b off
  675. # get rid of those .pyc files once and for all
  676. export PYTHONDONTWRITEBYTECODE=1
  677. ssh-add -l >& /dev/null || ssh-add