My dotfiles. Period.

main.bashrc 12KB

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