My dotfiles. Period.

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