My dotfiles. Period.

main.bashrc 16KB

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