My dotfiles. Period.

main.bashrc 12KB

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