My dotfiles. Period.

main.bashrc 11KB

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