My dotfiles. Period.

main.bashrc 7.3KB

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