My dotfiles. Period.

main.bashrc 12KB

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