My dotfiles. Period.

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