My dotfiles. Period.

main.bashrc 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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}
  62. while test "$n" -gt 0;
  63. do
  64. "$2" &
  65. ((n--))
  66. done
  67. }
  68. twice() {
  69. #
  70. # Run $@ two times
  71. #
  72. "$@"
  73. "$@"
  74. }
  75. git() {
  76. #
  77. # /usr/bin/git wrapper just to disable dangerous commands
  78. #
  79. # In particular, `git-am` is a a close typo of my favorite
  80. # aliases `ap` (`git-add --patch`) and `cm` (`git-commit`)
  81. # but it's dangerous to call by accident as it destroys
  82. # workdir changes.
  83. #
  84. if grep -Fwqse "$1" "$GIT_DISABLED_COMMANDS"; then
  85. echo "You don't want this." >&2
  86. return 1
  87. else
  88. command git "$@"
  89. fi
  90. }
  91. grepcl() {
  92. #
  93. # Grep in common chat log files
  94. #
  95. # The order is from older apps to newer as I was moving towards
  96. # weechat.
  97. #
  98. grep "$@" -R \
  99. "$HOME/.config/xchat2/xchatlogs" \
  100. "$HOME/.config/xchat2/scrollback" \
  101. "$HOME/.config/hexchat/logs" \
  102. "$HOME/.config/hexchat/scrollback" \
  103. "$HOME/.local/share/weechat/logs.old" \
  104. "$HOME/.local/share/weechat/logs"
  105. }
  106. grepfx() {
  107. #
  108. # Grep recursively for technical debt
  109. #
  110. # '[F]' hack to avoid matching self
  111. #
  112. grep --color -n \
  113. --exclude-dir 'utils' \
  114. --exclude-dir '.git' \
  115. -o '#[F]IXME:.*' -R "$@"
  116. }
  117. grepr() {
  118. #
  119. # Grep recursively, keeping numbers and ignoring common dirs
  120. #
  121. # Number one tool for refactoring!
  122. #
  123. local p=$1; shift
  124. grep --color -n --exclude-dir=".git" -e "$p" -R "$@"
  125. }
  126. grepr1() {
  127. #
  128. # Like grepr() but only list matching items in this directory
  129. #
  130. # Ie. list only files in current directory or names of subdirectories
  131. # where matches were found "somewhere".
  132. #
  133. grepr "$@" -l | cut -d/ -f1 | uniq
  134. }
  135. greph() {
  136. #
  137. # Grep through bash history
  138. #
  139. history | sed 's/^ *//; s/ / /' | cut -d' ' -f2- | grep "$@"
  140. }
  141. gitcd() {
  142. #
  143. # Deep in git repo, cd to the git root
  144. #
  145. cd "$(git rev-parse --show-toplevel)" || return 1
  146. }
  147. clsz() {
  148. #
  149. # Clear screen and move the prompt to bottom
  150. #
  151. tput clear; tput cup "$(tput lines)" 0
  152. }
  153. bcdiff() {
  154. #
  155. # Call bcompare only if objects actually differ
  156. #
  157. test $# -eq 2 && diff "$@" >/dev/null && return
  158. bcompare "$@" &
  159. }
  160. strip_colors() {
  161. #
  162. # Strip color codes from stdin
  163. #
  164. # Stolen from http://unix.stackexchange.com/a/4533/9365
  165. #
  166. sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g'
  167. }
  168. vims() {
  169. #
  170. # List Vim .swp files in a nice table
  171. #
  172. # **Note:** For this ro work, you need to set up Vim to
  173. # use same swap directory as assigned below. this can
  174. # be achieved by addoing section such as this to your
  175. # .vimrc:
  176. #
  177. # if isdirectory($HOME . '/.local/share/vim') == 0
  178. # :silent !mkdir -p ~/.vim/swap >/dev/null 2>&1
  179. # endif
  180. # set directory=./.vim-swap//
  181. # set directory+=~/.local/share/vim/swap//
  182. # set directory+=~/tmp//
  183. # set directory+=.
  184. #
  185. # Call this after crash or poweroff to remind yourself what
  186. # files you were editing before fact. Example:
  187. #
  188. # 2015-07-10 06:10:48.603953758 +0200 ~/TODO.todo
  189. #
  190. # This should make it easier to recover after a little
  191. # disaster.
  192. #
  193. local swap="$HOME/.local/share/vim/swap"
  194. #shellcheck disable=SC1117
  195. find "$swap" -type f -print0 \
  196. | xargs -0 -r stat -c "%y %n" \
  197. | sed "
  198. s| $swap/| |
  199. s|%|/|g
  200. s|.swp$||
  201. s| $HOME| ~|
  202. s| \(..:..\):[^ ]* +.... | \\1 |
  203. " \
  204. | sort \
  205. | tac
  206. }
  207. fixnl() {
  208. #
  209. # Fix newlines in stdin and pass to stdout
  210. #
  211. # Note: We need to read whole stdin; this cannot work
  212. # for infinite stream.
  213. #
  214. # Fix annoying cases:
  215. #
  216. # * If multi-line text is missing newline at the
  217. # end---add the missing newline.
  218. #
  219. # * If -c is passed, and single-line text does have
  220. # a newline at the end, remove it.
  221. #
  222. # This will not touch properly terminated multi-line texts,
  223. # or zero-line texts (ie. oly chars with no newline at all).
  224. #
  225. local arg=$1 # argument passed
  226. local cache # cache to keep stream in
  227. local nlcount # count of newlines in the stream
  228. local lastchr # hex dump (2-digit, lowercase) of last char
  229. local single=keep # single-line streams:
  230. # keep: do nothing
  231. # chop: drop last char if it's a LF
  232. case $arg in
  233. -c|--chop-single) single=chop ;;
  234. esac
  235. cache="$(mktemp -t fixnl.XXXXXXXX)"
  236. cat >"$cache"
  237. nlcount=$(<"$cache" wc -l)
  238. lastchr=$(<"$cache" tail -c1 | hexdump -e '"%02x"')
  239. case $nlcount:$lastchr:$single in
  240. 0:??:*) cat "$cache" ;; # 'abc' => keep as is
  241. 1:0a:chop) head -c -1 "$cache" ;; # 'abc\n' => -c passed: chop!
  242. 1:0a:keep) cat "$cache" ;; # 'abc\n' => keep as is
  243. *:0a:*) cat "$cache" ;; # 'a\nb\nc\n' => keep as is
  244. *:??:*) cat "$cache"; echo ;; # 'a\nb\nc' => add the missing NL
  245. esac
  246. rm "$cache"
  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 mkittool='mkittool -v'
  431. alias grep='grep --color'
  432. alias sc='se --direction=encz.cz'
  433. alias taskm='task modify'
  434. alias tasks='task rc.context:none'
  435. alias ts='ts "%F %T"'
  436. alias lsblk='lsblk -o +UUID,LABEL'
  437. alias pstree='pstree -h'
  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. export MDVIMB__CONVERTER=commonmark
  467. export MDVIMB__STYLE=darkzen # pergamen
  468. #shellcheck disable=SC2034
  469. {
  470. GIT_PS1_SHOWDIRTYSTATE=true
  471. GIT_PS1_SHOWUNTRACKEDFILES=true
  472. __SATURNIN_COMPLETE_DEBUG=true
  473. GIT_DISABLED_COMMANDS="$HOME/.gittum/disabled-commands"
  474. }
  475. export GIT_PAGER='less -S'
  476. # disable mounting things like SFTP to ~/.gvfs when accessed
  477. # via GIO (used by nautilus etc.)
  478. export GVFS_DISABLE_FUSE=1
  479. # disable the terrible beep sound (only for X; for tty?, blacklist pcspkr)
  480. xhost >& /dev/null && xset b off
  481. # get rid of those .pyc files once and for all
  482. export PYTHONDONTWRITEBYTECODE=1
  483. ssh-add -l >& /dev/null || ssh-add