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.

282 lines
6.3 KiB

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