My dotfiles. Period.

main.bashrc 16KB

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