My dotfiles. Period.

main.bashrc 10KB

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