My dotfiles. Period.

main.bashrc 16KB

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