My dotfiles. Period.

main.bashrc 12KB

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