My dotfiles. Period.

main.bashrc 9.0KB

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