My dotfiles. Period.

main.bashrc 13KB

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