My dotfiles. Period.

main.bashrc 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. nlfor() {
  240. #
  241. # Replace $1 (or blank space) with newlines and fix final newline
  242. #
  243. # Shorthand for commonly used tr syntax. Example:
  244. #
  245. # $ echo "$PATH" | nlat :
  246. # /foo
  247. # /bar
  248. # $
  249. #
  250. # is almost like `tr : '\n'` except that it is a bit easier
  251. # to type and fixes the annoying problem with missing
  252. # newline at the end of the replaced string.
  253. #
  254. local char=$1
  255. test -n "$char" || char='[:blank:]'
  256. tr "$char" '\n' \
  257. | fixnl
  258. }
  259. nljoin() {
  260. #
  261. # Join lines with $1 (or space)
  262. #
  263. # Shorthand for commonly used tr syntax. Example:
  264. #
  265. # $ { echo foo; echo bar; } | nljoin :
  266. #
  267. # is almost like `tr '\n' :` except that it is a bit easier
  268. # to type and fixes the annoying problem with extraneous
  269. # newline/colon at the end of final string.
  270. #
  271. local char=$1
  272. test -n "$char" || char=' '
  273. tr '\n' "$char" \
  274. | sed "s/$char*$//" \
  275. | fixnl -c
  276. }
  277. qpr() {
  278. #
  279. # Encode or decode (if $1 is '-d') message on stdin as Quoted Printable
  280. #
  281. local fn=encode
  282. case $1 in
  283. -d) fn=decode ;;
  284. -*) echo 'usage: qpr [-d] <text' >&2; return 2 ;;
  285. esac
  286. perl -MMIME::QuotedPrint -pe "\$_=MIME::QuotedPrint::$fn(\$_);"
  287. }
  288. xop() {
  289. #
  290. # Common clipboard operations with fixnl() on top
  291. #
  292. local op=$1
  293. local file=${2:-/dev/stdin}
  294. case $op in
  295. oo) xclip -selection clipboard -o | fixnl ;;
  296. o) xclip -o | fixnl ;;
  297. ii) fixnl -c | xclip -selection clipboard -i ;;
  298. i) fixnl -c | xclip -i ;;
  299. esac <"$file"
  300. }
  301. xod() {
  302. #
  303. # Like mktemp but get the content from clipboard
  304. #
  305. # Dump xo in a file named $1 somewhere in /tmp; echo the path
  306. # usage: scp $(xod useful_name.txt) eg:some/shared/place
  307. # instead of:
  308. #
  309. # xo > useful_name.txt
  310. # scp useful_name.txt eg:some/shared/place
  311. # rm useful_name.txt
  312. #
  313. local name="${1:-clipboard_dump.txt}"
  314. local tmp
  315. tmp=$(mktemp -d -t "xod.XXXXXXXX")
  316. xclip -o > "$tmp/$name"
  317. echo "$tmp/$name"
  318. }
  319. xood() {
  320. #
  321. # Just like xod() but using xoo (i.e. 'clipboard' clipboard)
  322. #
  323. local name="${1:-clipboard_dump.txt}"
  324. local tmp
  325. tmp=$(mktemp -d -t "xood.XXXXXXXX")
  326. xclip -selection clipboard -o > "$tmp/$name"
  327. echo "$tmp/$name"
  328. }
  329. xvxx() {
  330. #
  331. # Edit text in primary clipboard and save it to C-C clipboard
  332. #
  333. # Ideal for editing snippets in browser-based editors: during editing,
  334. # select the text, open terminal, call `xvx`, edit the text, move
  335. # back to the browser and paste it over the old (still selected) text.
  336. # That's it, no need to make up and save temporary files.
  337. #
  338. # If you quickly realize you need to re-edit the text, use xvvx().
  339. #
  340. xop o | vipe | xop ii
  341. }
  342. vx() {
  343. #
  344. # Edit stdin and save it to primary clipboard
  345. #
  346. # Ideal for selecting snippets of output of arbitrary commands.
  347. # select the text, open terminal, call `xvx`, edit the text, move
  348. # back to the browser and paste it over the old (still selected) text.
  349. # That's it, no need to make up and save temporary files.
  350. #
  351. vipe | xop i
  352. }
  353. xxvxx() {
  354. #
  355. # Edit text in C-C clipboard and save it back
  356. #
  357. xop oo | vipe | xop ii
  358. }
  359. yt2ogg() {
  360. #
  361. # Download YouTube video as vorbis audio
  362. #
  363. local url=$1
  364. youtube-dl -x --audio-format vorbis "$url"
  365. }
  366. yum_hasbin() {
  367. #
  368. # Ask yum who has bin $1 (and make the output actually readable)
  369. #
  370. local bname
  371. for bname in "$@";
  372. do
  373. case $bname in
  374. */*) dnf provides "$bname" ;;
  375. *) dnf provides "*/bin/$bname" ;;
  376. esac \
  377. | grep '^.' \
  378. | grep -E '^[[:alnum:]_:.-]+ :'
  379. done
  380. }
  381. cal() {
  382. #
  383. # cal with the proper version pick
  384. #
  385. local cal_v
  386. local cal_es
  387. cal_v=$(command cal -V 2>/dev/null); cal_es=$?
  388. case $cal_es:$cal_v in
  389. 0:cal\ from\ util-linux\ 2.32.1)
  390. command cal --color=always --monday --week "$@"
  391. ;;
  392. 64:*)
  393. command cal -M "$@"
  394. ;;
  395. esac
  396. }
  397. ### .... ###
  398. ### BASH ###
  399. ### '''' ###
  400. HISTCONTROL=ignoredups:erasedups
  401. shopt -s histappend
  402. PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
  403. HISTIGNORE="$HISTIGNORE:ls:ll:la:cd"
  404. HISTIGNORE="$HISTIGNORE:git dc:git st"
  405. HISTIGNORE="$HISTIGNORE:reset"
  406. HISTIGNORE="$HISTIGNORE:se *:sc *"
  407. HISTSIZE=-1
  408. HISTFILESIZE=100000
  409. HISTTIMEFORMAT='%F %T '
  410. GLOBIGNORE=.:..
  411. shopt -s histverify
  412. # some more aliases
  413. alias cls='clear'
  414. alias czkrates='czkrates -v'
  415. alias ll='ls -lh'
  416. alias lla='ls -lha'
  417. alias open='gnome-open'
  418. alias diff='colordiff -u'
  419. alias dmesg='dmesg --time-format iso'
  420. alias pad4='sed -e "/./s/^/ /"'
  421. alias grep='grep --color'
  422. alias sc='se --direction=encz.cz'
  423. alias taskm='task modify'
  424. alias tasks='task rc.context:none'
  425. alias ts='ts "%F %T"'
  426. alias lsblk='lsblk -o +UUID,LABEL'
  427. alias virsh='virsh --connect qemu:///system'
  428. alias wttr='curl -s "wttr.in/?1&n&q"'
  429. alias xaa='xclip -o | audit2allow'
  430. alias xi='xop i'
  431. alias xii='xop ii'
  432. alias xo='xop o'
  433. alias xoo='xop oo'
  434. alias reboot="echo -n . ; sync ; echo -n . ; sync ; echo -n . ; systemctl reboot"
  435. alias poweroff="echo -n . ; sync ; echo -n . ; sync ; echo -n . ; systemctl poweroff"
  436. x4x() {
  437. #
  438. # Pad text from primary clipboard with 4 spaces; save to C-C clipboard
  439. #
  440. # ...that is, for inclusion as Markdown verbatim text.
  441. #
  442. xo | pad4 | xii
  443. }
  444. RV_TMP="/tmp/bash-rv"
  445. mkdir -p "$RV_TMP"
  446. ### ...... ###
  447. ### OTHERS ###
  448. ### '''''' ###
  449. export LC_COLLATE=C # make sort upper first
  450. export EDITOR=vim
  451. export LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s"
  452. export LESS=' -R '
  453. export PRETTY=color
  454. export PRETTY_DEBUG_EXCLUDE=inigrep
  455. export MDVIMB__CONVERTER=commonmark
  456. #shellcheck disable=SC2034
  457. {
  458. GIT_PS1_SHOWDIRTYSTATE=true
  459. GIT_PS1_SHOWUNTRACKEDFILES=true
  460. __SATURNIN_COMPLETE_DEBUG=true
  461. GIT_DISABLED_COMMANDS="$HOME/.gittum/disabled-commands"
  462. }
  463. export GIT_PAGER='less -S'
  464. # disable mounting things like SFTP to ~/.gvfs when accessed
  465. # via GIO (used by nautilus etc.)
  466. export GVFS_DISABLE_FUSE=1
  467. # disable the terrible beep sound (only for X; for tty?, blacklist pcspkr)
  468. xhost >& /dev/null && xset b off
  469. # get rid of those .pyc files once and for all
  470. export PYTHONDONTWRITEBYTECODE=1
  471. ssh-add -l >& /dev/null || ssh-add