My dotfiles. Period.

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