My dotfiles. Period.

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