Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

.sh_common 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Paths: {{{
  2. #
  3. # This accounts for, at least:
  4. #
  5. # - a custom Firefox install
  6. # - some bin dirs in my homedir
  7. # - golang, ruby
  8. #
  9. # .sh_common is sourced by ~/bin/xmonad.start to provide for entire x session
  10. # getting these vars.
  11. #
  12. # Beware that other modifications to path might be made in .zshrc or .bashrc
  13. # It's a tangled mess which certainly warrants a TODO.
  14. export GOPATH=~/code/go
  15. export GOROOT=/opt/go
  16. export GEM_HOME=~/gems
  17. # export PATH=~/firefox:~/bin:~/notes/bin:~/.xmonad:~/.local/bin/:$GEM_HOME/bin:$GOPATH/bin:$PATH
  18. # The below stuff may break and need reverted on Debian - above line contains ~/bin, use that one -- bpb 2019-03-10:
  19. # Put a dir at the start of path, IFF it's not already in the path:
  20. prepend_to_path () {
  21. local dir="$1"
  22. if [[ -d "${dir}" ]]; then
  23. if [[ ":$PATH:" != *"${dir}"* ]]; then
  24. export PATH="${dir}:${PATH}"
  25. fi
  26. fi
  27. }
  28. prepend_to_path "$GOROOT/bin"
  29. prepend_to_path "$GOPATH/bin"
  30. prepend_to_path "$GEM_HOME/bin"
  31. prepend_to_path ~/.local/bin
  32. prepend_to_path ~/.xmonad
  33. prepend_to_path ~/notes/bin
  34. prepend_to_path ~/firefox
  35. prepend_to_path ~/bin
  36. # }}}
  37. # General environment / config: {{{
  38. # Debian stuff - define DEBFULLNAME, DEBEMAIL here:
  39. if [ -f ~/.sh_common_debconfig ]; then
  40. . ~/.sh_common_debconfig
  41. fi
  42. # Explicitly set default editor:
  43. export EDITOR=`which vim`
  44. # Configure less(1):
  45. export LESS='-FiRSX'
  46. # Configure fzf fuzzyfinder:
  47. export FZF_CTRL_R_OPTS='-e' # exact match by default - see fzf(1)
  48. # }}}
  49. # Aliases, various and sundry: {{{
  50. alias ac='apt-cache'
  51. alias agu='sudo apt-get update && sudo apt-get upgrade'
  52. alias c="commandlog"
  53. alias g='git'
  54. alias gitsu='git submodule sync && git submodule update'
  55. alias l='ls -CF'
  56. alias la='ls -A'
  57. alias lal='ls -Al'
  58. alias lah='ls -Alh'
  59. alias ll='ls -l'
  60. alias ls='ls --color'
  61. alias m='make'
  62. alias mk='mark'
  63. alias mka='mark add'
  64. alias s='sudo'
  65. alias v='vim'
  66. alias vt='vim `todaydir`'
  67. # How big is the terminal?
  68. alias dim='echo $(tput cols)x$(tput lines)'
  69. # vi life:
  70. alias :e='vim'
  71. alias :q='exit'
  72. alias :wq='exit'
  73. alias :r='cat'
  74. # Navigation:
  75. alias p='cd ~/p1k3'
  76. alias ct='cd `todaydir`'
  77. # }}}
  78. # Functions: {{{
  79. # Get a persistent directory history menu in fzf - originally written for dmenu
  80. # and inspired by jholland at:
  81. # http://hints.macworld.com/article.php?story=20050806202859392
  82. # I get the feeling that there's probably a more idiomatic way to do what
  83. # I want here, but for the moment it's nice. See: https://p1k3.com/2016/5/17/
  84. function h {
  85. if [ ! -z "$@" ]; then
  86. cd $(tail -3000 ~/.directory_history | tac | unsorted-unique | fzf --no-sort --height=50% -q $@)
  87. else
  88. cd $(tail -3000 ~/.directory_history | tac | unsorted-unique | fzf --no-sort --height=50%)
  89. fi
  90. }
  91. function b {
  92. if [ ! -z "$@" ]; then
  93. echo $(realpath "$@") >> ~/.directory_bookmarks
  94. else
  95. cd $(tail -3000 ~/.directory_bookmarks | tac | unsorted-unique | fzf --no-sort --height=50%)
  96. fi
  97. }
  98. # Create a directory (if it doesn't exist) and cd to it:
  99. function mcd {
  100. mkdir -p $1 && cd $1
  101. }
  102. # Get some information about a git repo - useful for
  103. function parse_git_branch {
  104. # Formerly:
  105. # ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  106. # echo "("${ref#refs/heads/}") "
  107. # Originally borrowed from:
  108. # https://github.com/robacarp/config_files/commit/5d983240d509bf4f7815ed822c0e868ccce08a79
  109. git_status=$(git status --porcelain -b 2> /dev/null) || return
  110. echo $git_status | awk '
  111. BEGIN {
  112. status["untracked"] = 0
  113. status["mods"] = 0
  114. branch_status = ""
  115. }
  116. $0 ~ /.*\[.*\]$/ {
  117. branch_status = " " $3 " " $4
  118. }
  119. $1 ~ /##/ {
  120. gsub(/\.\.\..*/, "")
  121. branch_name = $2
  122. }
  123. $1 ~ /\?\?/ { status["untracked"] ++ }
  124. $1 ~ /M/ { status["mods"] ++ }
  125. END {
  126. printf "(%s m%i/u%i%s) ", branch_name, status["mods"], status["untracked"], branch_status
  127. }
  128. '
  129. }
  130. # Render a simple horizontal rule / title bar in the terminal. Again, a
  131. # variation on robacarp:
  132. # https://robacarp.io/2018/07/19/bash-hr-function-to-draw-a-line-across-the-terminal.html
  133. function titlebar {
  134. # Text written into the horizontal rule, left justified
  135. text=${1:-}
  136. length=$(echo "$text" | wc -m)
  137. # set the color
  138. echo -e -n "\033[30;47m"
  139. # print the message
  140. echo -n "$text"
  141. # finish the line across the console
  142. cols=$(expr "$(tput cols)" - $length)
  143. printf " %${cols}s"
  144. # clear the background color and start a new line
  145. echo -e "\033[0m"
  146. }
  147. # }}}