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.

274 lines
6.2 KiB

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