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.

264 lines
6.2 KiB

5 years ago
9 years ago
  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=/usr/local/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 ~/.fzf/bin
  38. prepend_to_path ~/bin
  39. # }}}
  40. # General environment / config: {{{
  41. # Debian stuff - define DEBFULLNAME, DEBEMAIL here:
  42. if [ -f ~/.sh_common_debconfig ]; then
  43. . ~/.sh_common_debconfig
  44. fi
  45. # Explicitly set default editor:
  46. export EDITOR=`which vim`
  47. # Configure less(1):
  48. export LESS='-FiRSX'
  49. # Configure fzf fuzzyfinder:
  50. export FZF_CTRL_R_OPTS='-e' # exact match by default - see fzf(1)
  51. # For mediawiki/core/docker-compose.yml
  52. export MW_DOCKER_UID=$(id -u)
  53. export MW_DOCKER_GID=$(id -g)
  54. # }}}
  55. # Aliases, various and sundry: {{{
  56. alias ac='apt-cache'
  57. alias agu='sudo apt-get update && sudo apt-get upgrade'
  58. alias c='commandlog'
  59. alias fd='fdfind'
  60. alias g='git'
  61. alias gitsu='git submodule sync && git submodule update'
  62. alias m='make'
  63. alias mk='mark'
  64. alias mka='mark add'
  65. alias s='sudo'
  66. alias v='vim'
  67. alias vt='vim `todaydir`'
  68. # How big is the terminal?
  69. alias dim='echo $(tput cols)x$(tput lines)'
  70. # vi life:
  71. alias :e='nvim'
  72. alias :q='exit'
  73. alias :wq='exit'
  74. alias :r='cat'
  75. # }}}
  76. # Functions: {{{
  77. # p1k3 navigation:
  78. function p {
  79. cd "$P1K3_ROOT"
  80. }
  81. function ct {
  82. cd $(todaydir)
  83. }
  84. if type exa &> /dev/null; then
  85. # If exa is installed, replace the traditional ls aliases with exa
  86. # invocations.
  87. function l {
  88. exa --group-directories-first $@
  89. }
  90. function ll {
  91. exa --group-directories-first -l --git $@
  92. }
  93. function la {
  94. # This would typically be `ls -A`, for "almost all", which excludes . and ..
  95. # - exa appears to do that by default, and so doesn't have an -A option
  96. exa --group-directories-first -a $@
  97. }
  98. function lal {
  99. # ls -Al
  100. exa --group-directories-first -al --git $@
  101. }
  102. function lah {
  103. # ls -Alh
  104. # exa uses human-readable sizes by default, so doesn't have the -h flag.
  105. exa --group-directories-first -al --git $@
  106. }
  107. else
  108. # No exa, so set up traditional ls aliases as functions.
  109. function l {
  110. ls --group-directories-first --color -CF $@
  111. }
  112. function ll {
  113. ls --group-directories-first -l --color $@
  114. }
  115. function la {
  116. # Almost all - exclude . and ..:
  117. ls --group-directories-first -A $@
  118. }
  119. function lal {
  120. ls --group-directories-first -Al $@
  121. }
  122. function lah {
  123. ls --group-directories-first -Alh $@
  124. }
  125. fi
  126. # Get a persistent directory history menu in fzf - originally written for dmenu
  127. # and inspired by jholland at:
  128. #
  129. # http://hints.macworld.com/article.php?story=20050806202859392
  130. #
  131. # I get the feeling that there's probably a more idiomatic way to do what
  132. # I want here, but for the moment it's nice.
  133. #
  134. # See: https://p1k3.com/2016/5/17/
  135. function h {
  136. if [ ! -z "$@" ]; then
  137. cd $(tail -2500 ~/.directory_history | tac | unsorted-unique | fzf --no-sort --height=50% -q $@)
  138. else
  139. cd $(tail -2500 ~/.directory_history | tac | unsorted-unique | fzf --no-sort --height=50%)
  140. fi
  141. }
  142. function b {
  143. if [ ! -z "$@" ]; then
  144. echo $(realpath "$@") >> ~/.directory_bookmarks
  145. else
  146. cd $(sort ~/.directory_bookmarks | uniq | fzf --no-sort --height=50%)
  147. fi
  148. }
  149. # Create a directory (if it doesn't exist) and cd to it:
  150. function mcd {
  151. mkdir -p $1 && cd $1
  152. }
  153. # Use official git prompt, if it exists, or fall back to
  154. # parse_git_branch:
  155. if [ -f "$HOME/.git-prompt.sh" ]; then
  156. GIT_PS1_SHOWCOLORHINTS=1
  157. GIT_PS1_SHOWUNTRACKEDFILES=1
  158. GIT_PS1_SHOWDIRTYSTATE=1
  159. GIT_PS1_SHOWUPSTREAM="auto verbose"
  160. GIT_PS1_SHOWSTASHSTATE=1
  161. # shellcheck source=/home/brennen/.git-prompt.sh
  162. . "$HOME/.git-prompt.sh"
  163. else
  164. # Emulate __git_ps1 interface which takes 2 params - before part
  165. # and after part:
  166. function __git_ps1 {
  167. PS1="$1 $(parse_git_branch)$2"
  168. }
  169. fi
  170. # Get some information about a git repo
  171. function parse_git_branch {
  172. # if [ ! -z "$has_git_prompt" ]; then
  173. # __git_ps1
  174. # return
  175. # fi
  176. # Formerly:
  177. # ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  178. # echo "("${ref#refs/heads/}") "
  179. # Originally borrowed from:
  180. # https://github.com/robacarp/config_files/commit/5d983240d509bf4f7815ed822c0e868ccce08a79
  181. git_status=$(git status --porcelain -b 2> /dev/null) || return
  182. echo $git_status | awk '
  183. BEGIN {
  184. status["untracked"] = 0
  185. status["mods"] = 0
  186. branch_status = ""
  187. }
  188. $0 ~ /.*\[.*\]$/ {
  189. branch_status = " " $3 " " $4
  190. }
  191. $1 ~ /##/ {
  192. gsub(/\.\.\..*/, "")
  193. branch_name = $2
  194. }
  195. $1 ~ /\?\?/ { status["untracked"] ++ }
  196. $1 ~ /M/ { status["mods"] ++ }
  197. END {
  198. printf "(%s m%i/u%i%s) ", branch_name, status["mods"], status["untracked"], branch_status
  199. }
  200. '
  201. }
  202. # Render a simple horizontal rule / title bar in the terminal. Again, a
  203. # variation on robacarp:
  204. # https://robacarp.io/2018/07/19/bash-hr-function-to-draw-a-line-across-the-terminal.html
  205. function titlebar {
  206. # Text written into the horizontal rule, left justified
  207. text=${1:-}
  208. length=$(echo "$text" | wc -m)
  209. # set the color
  210. echo -e -n "\033[30;47m"
  211. # print the message
  212. echo -n "$text"
  213. # finish the line across the console
  214. cols=$(expr "$(tput cols)" - $length)
  215. printf " %${cols}s"
  216. # clear the background color and start a new line
  217. echo -e "\033[0m"
  218. }
  219. # }}}