My dotfiles. Period.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. dt() {
  12. #
  13. # Open $1 new terminals
  14. #
  15. __bashum_run_n "${1:-1}" urxvt
  16. }
  17. dT() {
  18. #
  19. # Open $1 new alterminals
  20. #
  21. __bashum_run_n "${1:-1}" xfce4-terminal
  22. }
  23. __bashum_run_n() {
  24. #
  25. # Run $2 in background $1 times
  26. #
  27. local n=${1:-1}
  28. while test "$n" -gt 0;
  29. do
  30. "$2" &
  31. ((n--))
  32. done
  33. }
  34. git() {
  35. #
  36. # /usr/bin/git wrapper just to disable dangerous commands
  37. #
  38. # In particular, `git-am` is a a close typo of my favorite
  39. # aliases `ap` (`git-add --patch`) and `cm` (`git-commit`)
  40. # but it's dangerous to call by accident as it destroys
  41. # workdir changes.
  42. #
  43. if grep -Fwqse "$1" "$GIT_DISABLED_COMMANDS"; then
  44. echo "You don't want this." >&2
  45. return 1
  46. else
  47. command git "$@"
  48. fi
  49. }
  50. grepr() {
  51. #
  52. # Grep recursively, keeping numbers and ignoring common dirs
  53. #
  54. # Number one tool for refactoring!
  55. #
  56. local p=$1; shift
  57. grep --color -n --exclude-dir=".git" -e "$p" -r "$@"
  58. }
  59. greph() {
  60. #
  61. # Grep through bash history
  62. #
  63. history | sed 's/^ *//; s/ / /' | cut -d' ' -f2- | grep "$@"
  64. }
  65. gitcd() {
  66. #
  67. # Deep in git repo, cd to the git root
  68. #
  69. cd "$(git rev-parse --show-toplevel)"
  70. }
  71. haste() {
  72. #
  73. # Send stdin to hastebin.com and print resulting URL
  74. #
  75. local a=$(cat)
  76. curl -X POST -s -d "$a" http://hastebin.com/documents \
  77. | awk -F '"' '{print "http://hastebin.com/"$4}'
  78. }
  79. clsz() {
  80. #
  81. # Clear screen and move the prompt to bottom
  82. #
  83. tput clear; tput cup "$(tput lines)" 0
  84. }
  85. bcdiff() {
  86. #
  87. # Call bcompare only if objects actually differ
  88. #
  89. test $# -eq 2 && diff "$@" >/dev/null && return
  90. bcompare "$@" &
  91. }
  92. strip_colors() {
  93. #
  94. # Strip color codes from stdin
  95. #
  96. # Stolen from http://unix.stackexchange.com/a/4533/9365
  97. #
  98. sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
  99. }
  100. vims() {
  101. #
  102. # Show "abandoned" Vim .swp files in a nice table
  103. #
  104. # Call this after crash or poweroff to get back and recover
  105. # files open before the fact. Example:
  106. #
  107. # 2015-07-10 06:10:48.603953758 +0200 ~/TODO.todo
  108. #
  109. # Depends on .swp settings defined in my mydots repo.
  110. #
  111. local swap="$HOME/.local/share/vim/swap"
  112. find "$swap" -type f -print0 \
  113. | xargs -0 -r stat -c "%y %n" \
  114. | sed "
  115. s| $swap/| |
  116. s|%|/|g
  117. s|.swp$||
  118. s| $HOME| ~|
  119. s| \(..:..\):[^ ]* +.... | \\1 |
  120. " \
  121. | sort \
  122. | tac
  123. }
  124. fixnl() {
  125. #
  126. # Fix newlines in stdin and pass to stdout
  127. #
  128. # Note: We need to read whole stdin; this cannot work
  129. # for infinite stream.
  130. #
  131. # Fix annoying cases:
  132. #
  133. # * If multi-line text is missing newline at the
  134. # end---add the missing newline.
  135. #
  136. # * If -c is passed, and single-line text does have
  137. # a newline at the end, remove it.
  138. #
  139. # This will not touch properly terminated multi-line texts,
  140. # or zero-line texts (ie. oly chars with no newline at all).
  141. #
  142. local arg=$1 # argument passed
  143. local cache # cache to keep stream in
  144. local nlcount # count of newlines in the stream
  145. local lastchr # hex dump (2-digit, lowercase) of last char
  146. local single=keep # single-line streams:
  147. # keep: do nothing
  148. # chop: drop last char if it's a LF
  149. case $arg in
  150. -c|--chop-single) single=chop ;;
  151. esac
  152. cache="$(mktemp -t fixnl.XXXXXXXX)"
  153. cat >"$cache"
  154. nlcount=$(<"$cache" wc -l)
  155. lastchr=$(<"$cache" tail -c1 | hexdump -e '"%02x"')
  156. case $nlcount:$lastchr:$single in
  157. 0:??:*) cat "$cache" ;; # 'abc' => keep as is
  158. 1:0a:chop) head -c -1 "$cache" ;; # 'abc\n' => -c passed: chop!
  159. 1:0a:keep) cat "$cache" ;; # 'abc\n' => keep as is
  160. *:0a:*) cat "$cache" ;; # 'a\nb\nc\n' => keep as is
  161. *:??:*) cat "$cache"; echo ;; # 'a\nb\nc' => add the missing NL
  162. esac
  163. rm "$cache"
  164. }
  165. xop() {
  166. #
  167. # Common clipboard operations with fixnl() on top
  168. #
  169. local op=$1
  170. local file=${2:-/dev/stdin}
  171. case $op in
  172. oo) xclip -selection clipboard -o | fixnl ;;
  173. o) xclip -o | fixnl ;;
  174. ii) fixnl -c | xclip -selection clipboard -i ;;
  175. i) fixnl -c | xclip -i ;;
  176. esac <"$file"
  177. }
  178. xod() {
  179. #
  180. # Like mktemp but get the content from clipboard
  181. #
  182. # Dump xo in a file named $1 somewhere in /tmp; echo the path
  183. # usage: scp $(xod useful_name.txt) eg:some/shared/place
  184. # instead of xo>useful_name.txt; scp useful_name.txt eg:some/shared/place; rm useful_name.txt
  185. #
  186. local name="${1:-clipboard_dump.txt}"
  187. local tmp=$(mktemp -d -t "xod.XXXXXXXX")
  188. xclip -o > "$tmp/$name"
  189. echo "$tmp/$name"
  190. }
  191. xood() {
  192. #
  193. # Just like xod() but using xoo (i.e. 'clipboard' clipboard)
  194. #
  195. local name="${1:-clipboard_dump.txt}"
  196. local tmp=$(mktemp -d -t "xood.XXXXXXXX")
  197. xclip -selection clipboard -o > "$tmp/$name"
  198. echo "$tmp/$name"
  199. }
  200. yum_hasbin() {
  201. #
  202. # Ask yum who has bin $1 (and make the output actually readable)
  203. #
  204. local bname
  205. for bname in "$@";
  206. do
  207. case $bname in
  208. */*) dnf provides "$bname" ;;
  209. *) dnf provides "*/bin/$bname" ;;
  210. esac \
  211. | grep '^.' \
  212. | grep -E '^[[:alnum:]_:.-]+ :'
  213. done
  214. }
  215. ### .... ###
  216. ### BASH ###
  217. ### '''' ###
  218. HISTCONTROL=ignoredups:erasedups
  219. shopt -s histappend
  220. PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
  221. HISTIGNORE="$HISTIGNORE:ls:ll:la:cd"
  222. HISTIGNORE="$HISTIGNORE:git dc:git st"
  223. HISTIGNORE="$HISTIGNORE:reset"
  224. #export HISTIGNORE="$HISTIGNORE:se *:sc *"
  225. HISTSIZE=-1
  226. HISTFILESIZE=100000
  227. GLOBIGNORE=.:..
  228. shopt -s histverify
  229. # some more aliases
  230. alias cal='cal -m'
  231. alias cls='clear'
  232. alias ll='ls -lh'
  233. alias lla='ls -lha'
  234. alias open='gnome-open'
  235. alias diff='colordiff -u'
  236. alias dmesg='dmesg --time-format iso'
  237. alias pad4='sed -e "/./s/^/ /"'
  238. alias grep='grep --color --binary-files=without-match'
  239. alias sc='se --direction=encz.cz'
  240. alias taskm='task modify'
  241. alias tasks='task rc.context:none'
  242. alias ts='ts "%F %T"'
  243. alias lsblk='lsblk -o +UUID,LABEL'
  244. alias virsh='virsh --connect qemu:///system'
  245. alias xaa='xclip -o | audit2allow'
  246. alias xi='xop i'
  247. alias xii='xop ii'
  248. alias xo='xop o'
  249. alias xoo='xop oo'
  250. alias reboot="echo -n . ; sync ; echo -n . ; sync ; echo -n . ; systemctl reboot"
  251. alias poweroff="echo -n . ; sync ; echo -n . ; sync ; echo -n . ; systemctl poweroff"
  252. RV_TMP="/tmp/bash-rv"
  253. mkdir -p "$RV_TMP"
  254. ### ...... ###
  255. ### OTHERS ###
  256. ### '''''' ###
  257. export LC_COLLATE=C # make sort upper first
  258. export EDITOR=vim
  259. export PRETTY=color
  260. export PRETTY_DEBUG_EXCLUDE=inigrep
  261. # make green git bash trinket even cooler
  262. GIT_PS1_SHOWDIRTYSTATE=true
  263. GIT_PS1_SHOWUNTRACKEDFILES=true
  264. GIT_DISABLED_COMMANDS="$HOME/.gittum/disabled-commands"
  265. export GIT_PAGER='less -S'
  266. # disable mounting things like SFTP to ~/.gvfs when accessed
  267. # via GIO (used by nautilus etc.)
  268. export GVFS_DISABLE_FUSE=1
  269. # disable the terrible beep sound (only for X; for tty?, blacklist pcspkr)
  270. xhost >& /dev/null && xset b off
  271. # get rid of those .pyc files once and for all
  272. export PYTHONDONTWRITEBYTECODE=1
  273. ssh-add -l >& /dev/null || ssh-add