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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 P1K3_ROOT=~/workspace/p1k3
  18. # export PATH=~/firefox:~/bin:~/notes/bin:~/.xmonad:~/.local/bin/:$GEM_HOME/bin:$GOPATH/bin:$PATH
  19. # The below stuff may break and need reverted on Debian - above line contains ~/bin, use that one -- bpb 2019-03-10:
  20. # Put a dir at the start of path, IFF it's not already in the path:
  21. prepend_to_path () {
  22. local dir="$1"
  23. if [[ -d "${dir}" ]]; then
  24. if [[ ":$PATH:" != *"${dir}"* ]]; then
  25. export PATH="${dir}:${PATH}"
  26. fi
  27. fi
  28. }
  29. prepend_to_path "$GOROOT/bin" # golang
  30. prepend_to_path "$GOPATH/bin" # golang
  31. prepend_to_path "$GEM_HOME/bin" # ruby
  32. prepend_to_path "$HOME/.cargo/bin" # rust
  33. prepend_to_path ~/.local/bin
  34. prepend_to_path ~/.xmonad
  35. prepend_to_path ~/notes/bin
  36. prepend_to_path ~/firefox
  37. prepend_to_path ~/bin
  38. # }}}
  39. # General environment / config: {{{
  40. # Debian stuff - define DEBFULLNAME, DEBEMAIL here:
  41. if [ -f ~/.sh_common_debconfig ]; then
  42. . ~/.sh_common_debconfig
  43. fi
  44. # Explicitly set default editor:
  45. export EDITOR=`which vim`
  46. # Configure less(1):
  47. export LESS='-FiRSX'
  48. # Configure fzf fuzzyfinder:
  49. export FZF_CTRL_R_OPTS='-e' # exact match by default - see fzf(1)
  50. # }}}
  51. # Aliases, various and sundry: {{{
  52. alias ac='apt-cache'
  53. alias agu='sudo apt-get update && sudo apt-get upgrade'
  54. alias c='commandlog'
  55. alias fd='fdfind'
  56. alias g='git'
  57. alias gitsu='git submodule sync && git submodule update'
  58. alias m='make'
  59. alias mk='mark'
  60. alias mka='mark add'
  61. alias s='sudo'
  62. alias v='vim'
  63. alias vt='vim `todaydir`'
  64. # How big is the terminal?
  65. alias dim='echo $(tput cols)x$(tput lines)'
  66. # vi life:
  67. alias :e='vim'
  68. alias :q='exit'
  69. alias :wq='exit'
  70. alias :r='cat'
  71. # }}}
  72. # Functions: {{{
  73. # p1k3 navigation:
  74. function p {
  75. cd "$P1K3_ROOT"
  76. }
  77. function ct {
  78. cd $(todaydir)
  79. }
  80. if type exa &> /dev/null; then
  81. # If exa is installed, replace the traditional ls aliases with exa
  82. # invocations.
  83. function l {
  84. exa $@
  85. }
  86. function ll {
  87. exa -l --git $@
  88. }
  89. function la {
  90. # This would typically be `ls -A`, for "almost all", which excludes . and ..
  91. # - exa appears to do that by default, and so doesn't have an -A option
  92. exa -a $@
  93. }
  94. function lal {
  95. # ls -Al
  96. exa -al --git $@
  97. }
  98. function lah {
  99. # ls -Alh
  100. # exa uses human-readable sizes by default, so doesn't have the -h flag.
  101. exa -al --git $@
  102. }
  103. else
  104. # No exa, so set up traditional ls aliases as functions.
  105. function l {
  106. ls -CF $@
  107. }
  108. function ll {
  109. ls -l $@
  110. }
  111. function la {
  112. # Almost all - exclude . and ..:
  113. ls -A $@
  114. }
  115. function lal {
  116. ls -Al $@
  117. }
  118. function lah {
  119. ls -Alh $@
  120. }
  121. fi
  122. # Get a persistent directory history menu in fzf - originally written for dmenu
  123. # and inspired by jholland at:
  124. #
  125. # http://hints.macworld.com/article.php?story=20050806202859392
  126. #
  127. # I get the feeling that there's probably a more idiomatic way to do what
  128. # I want here, but for the moment it's nice.
  129. #
  130. # See: https://p1k3.com/2016/5/17/
  131. function h {
  132. if [ ! -z "$@" ]; then
  133. cd $(tail -2500 ~/.directory_history | tac | unsorted-unique | fzf --no-sort --height=50% -q $@)
  134. else
  135. cd $(tail -2500 ~/.directory_history | tac | unsorted-unique | fzf --no-sort --height=50%)
  136. fi
  137. }
  138. function b {
  139. if [ ! -z "$@" ]; then
  140. echo $(realpath "$@") >> ~/.directory_bookmarks
  141. else
  142. cd $(tail -3000 ~/.directory_bookmarks | tac | unsorted-unique | fzf --no-sort --height=50%)
  143. fi
  144. }
  145. # Create a directory (if it doesn't exist) and cd to it:
  146. function mcd {
  147. mkdir -p $1 && cd $1
  148. }
  149. # Get some information about a git repo - useful for
  150. function parse_git_branch {
  151. # Formerly:
  152. # ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  153. # echo "("${ref#refs/heads/}") "
  154. # Originally borrowed from:
  155. # https://github.com/robacarp/config_files/commit/5d983240d509bf4f7815ed822c0e868ccce08a79
  156. git_status=$(git status --porcelain -b 2> /dev/null) || return
  157. echo $git_status | awk '
  158. BEGIN {
  159. status["untracked"] = 0
  160. status["mods"] = 0
  161. branch_status = ""
  162. }
  163. $0 ~ /.*\[.*\]$/ {
  164. branch_status = " " $3 " " $4
  165. }
  166. $1 ~ /##/ {
  167. gsub(/\.\.\..*/, "")
  168. branch_name = $2
  169. }
  170. $1 ~ /\?\?/ { status["untracked"] ++ }
  171. $1 ~ /M/ { status["mods"] ++ }
  172. END {
  173. printf "(%s m%i/u%i%s) ", branch_name, status["mods"], status["untracked"], branch_status
  174. }
  175. '
  176. }
  177. # Render a simple horizontal rule / title bar in the terminal. Again, a
  178. # variation on robacarp:
  179. # https://robacarp.io/2018/07/19/bash-hr-function-to-draw-a-line-across-the-terminal.html
  180. function titlebar {
  181. # Text written into the horizontal rule, left justified
  182. text=${1:-}
  183. length=$(echo "$text" | wc -m)
  184. # set the color
  185. echo -e -n "\033[30;47m"
  186. # print the message
  187. echo -n "$text"
  188. # finish the line across the console
  189. cols=$(expr "$(tput cols)" - $length)
  190. printf " %${cols}s"
  191. # clear the background color and start a new line
  192. echo -e "\033[0m"
  193. }
  194. # }}}