My dotfiles. Period.

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