My dotfiles. Period.

main.bashrc 6.9KB

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