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.

287 lines
8.1 KiB

  1. # My zsh config file. One day, I may actually learn about zsh instead
  2. # of just haphazardly tossing crap at it.
  3. # basic setup / options {{{
  4. # Common path, aliases, functions, etc. for both zsh and bash:
  5. source ~/.sh_common
  6. autoload -U add-zsh-hook
  7. autoload -Uz compinit
  8. export HISTFILE=~/.histfile
  9. export HISTSIZE=90000
  10. export SAVEHIST=9999999
  11. # vi-style cycling through completions on repeated tab presses:
  12. setopt menu_complete
  13. # Allow arrow-key selection of completion items from a menu:
  14. zstyle ':completion:*' menu select
  15. # Auto-rehash so that when stuff is installed you can tab-complete it
  16. # immediately (I have no idea what performance penalty this incurs):
  17. zstyle ':completion:*' rehash true
  18. setopt prompt_subst
  19. setopt inc_append_history
  20. setopt hist_ignore_space
  21. setopt hist_ignore_dups
  22. setopt autocd
  23. setopt extendedglob
  24. setopt autopushd pushdminus
  25. setopt printexitvalue
  26. unsetopt beep
  27. bindkey -e
  28. # Add completion for custom git commands I've written as shell scripts:
  29. # https://stackoverflow.com/questions/38725102/how-to-add-custom-git-command-to-zsh-completion
  30. zstyle ':completion:*:*:git:*' user-commands sel-changed:'select from changed files' \
  31. edit-changed:'edit from selected changed files' \
  32. do:'execute command from top of repo'
  33. # Completion for Wikimedia production hosts:
  34. # https://wikitech.wikimedia.org/wiki/Wmf-sre-laptop
  35. zstyle ':completion:*:hosts' known-hosts-files /home/brennen/.ssh/known_hosts /home/brennen/.ssh/known_hosts.d/wmf-prod
  36. zstyle :compinstall filename '/home/brennen/.zshrc'
  37. compinit
  38. # Fix weirdness with mcd completion -
  39. # https://unix.stackexchange.com/questions/496379/treat-command-like-another-for-completion-purposes
  40. compdef _directories mcd
  41. # }}}
  42. # keybindings {{{
  43. # You can get bindkey strings with Ctrl-v followed by your key sequence.
  44. # https://wiki.archlinux.org/index.php/Zsh#History_search
  45. # Search up/down for matching thing on arrows - you can still use ctrl-n /
  46. # ctrl-p to move up and down in the overall command history:
  47. autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
  48. zle -N up-line-or-beginning-search
  49. zle -N down-line-or-beginning-search
  50. [[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-beginning-search
  51. [[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-beginning-search
  52. # https://wiki.archlinux.org/index.php/Zsh#File_manager_key_binds
  53. # Alt-Left for previous directory and Alt-Up for parent
  54. cd_undo_key () {
  55. echo
  56. popd
  57. zle reset-prompt
  58. }
  59. cd_parent_key () {
  60. echo
  61. pushd ..
  62. zle reset-prompt
  63. }
  64. zle -N cd_parent_key
  65. zle -N cd_undo_key
  66. bindkey '^[[1;3A' cd_parent_key
  67. bindkey '^[[1;3D' cd_undo_key
  68. # Invoke h history function from .sh_common with Alt-H:
  69. invoke_h () {
  70. h
  71. zle reset-prompt
  72. }
  73. zle -N invoke_h
  74. bindkey '^[H' invoke_h
  75. # fzf fuzzyfinder for use with Alt-c, Ctrl-r, Ctrl-t:
  76. [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
  77. # }}}
  78. # prompt configuration {{{
  79. # Grab some colors:
  80. autoload colors zsh/terminfo
  81. if [[ "$terminfo[colors]" -ge 8 ]]; then
  82. colors
  83. fi
  84. for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
  85. eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
  86. eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
  87. (( count = $count + 1 ))
  88. done
  89. PR_NO_COLOR="%{$terminfo[sgr0]%}"
  90. # Next, do per-host color and sigils.
  91. #
  92. # Since this will be used in a $PS1, colors can be either of the named values
  93. # from the above loop, or numerical escapes like %F{nnn} for Foreground and
  94. # %K{nnn} for background. The latter will only work on 256 color terminals,
  95. # although I'm not sure what the failure mode looks like.
  96. #
  97. # Great for sigils:
  98. # https://en.wikipedia.org/wiki/Unicode_Geometric_Shapes
  99. # https://en.wikipedia.org/wiki/List_of_symbols
  100. PR_SIGIL=\$
  101. PR_PATH_COLOR=$PR_BLUE;
  102. PR_TIME_COLOR=$PR_LIGHT_BLUE
  103. PR_GIT_COLOR=$PR_NO_COLOR
  104. PR_SIGIL_COLOR=$PR_NO_COLOR
  105. # Display a moon phase emoji, if it's available:
  106. PR_MOON=""
  107. if type "phasemoji" > /dev/null; then
  108. PR_MOON="$(phasemoji) "
  109. fi
  110. case "$HOST" in
  111. 'catastrophe')
  112. PR_HOST_COLOR=$PR_LIGHT_BLUE
  113. ;;
  114. 'desiderata')
  115. PR_HOST_COLOR=$PR_LIGHT_BLUE
  116. PR_SIGIL=
  117. ;;
  118. 'escalation')
  119. PR_HOST_COLOR=$PR_LIGHT_GREEN
  120. PR_SIGIL=
  121. ;;
  122. 'errata')
  123. PR_HOST_COLOR=$PR_LIGHT_BLUE
  124. PR_SIGIL=
  125. ;;
  126. 'externality')
  127. PR_HOST_COLOR=$PR_LIGHT_GREEN
  128. PR_SIGIL=
  129. ;;
  130. 'exuberance')
  131. PR_HOST_COLOR=$PR_LIGHT_YELLOW
  132. PR_SIGIL=
  133. ;;
  134. 'fragility')
  135. PR_HOST_COLOR=$PR_WHITE
  136. PR_SIGIL=
  137. ;;
  138. 'inertia')
  139. PR_HOST_COLOR='%F{27}'
  140. PR_TIME_COLOR='%F{29}'
  141. PR_GIT_COLOR='%F{93}'
  142. PR_SIGIL_COLOR=$PR_HOST_COLOR
  143. # PR_SIGIL=▣
  144. PR_SIGIL=$(fragment-bullet)
  145. ;;
  146. 'metaphor')
  147. PR_HOST_COLOR='%F{69}'
  148. PR_TIME_COLOR='%F{161}'
  149. PR_GIT_COLOR='%F{32}'
  150. PR_PATH_COLOR='%F{242}'
  151. PR_SIGIL_COLOR='%F{220}'
  152. # see bin/fragment-bullet:
  153. PR_SIGIL=$(fragment-bullet)
  154. ;;
  155. 'novena-edward-norway')
  156. PR_HOST_COLOR=$PR_LIGHT_BLUE
  157. PR_SIGIL=
  158. ;;
  159. 'pisces')
  160. PR_HOST_COLOR=$PR_GREEN
  161. ;;
  162. 'raspberrypi')
  163. PR_HOST_COLOR=$PR_MAGENTA
  164. ;;
  165. 'kropotkin')
  166. PR_HOST_COLOR=$PR_BLUE
  167. ;;
  168. *)
  169. PR_HOST_COLOR=$PR_BLUE
  170. ;;
  171. esac
  172. # Run to install some color swatches in the prompt for experimenting with
  173. # host colors and such:
  174. function colortest {
  175. colortest=""
  176. for c in `seq 0 256`; do
  177. colortest="$colortest %K{$c} $c "
  178. done
  179. }
  180. # Stash values here for inclusion at beginning of prompt:
  181. # for var in colortest SSH_AGENT; do
  182. # pr_meta="$var=${$var}\n"
  183. # done
  184. # Reset the prompt on every command to get that parse_git_branch function to
  185. # run. Because this runs before every command, there's no need to set PS1
  186. # elsewhere. Also sets xterm title (which works for other terminals as well):
  187. function precmd {
  188. # http://tldp.org/HOWTO/Xterm-Title-4.html
  189. case $TERM in
  190. xterm*)
  191. print -Pn "\e]0;%n@%m: %~\a"
  192. ;;
  193. esac
  194. NEWLINE=$'\n'
  195. if [ -z "$BPB_NOTE" ]; then
  196. else
  197. pr_meta="$BPB_NOTE${NEWLINE}"
  198. fi
  199. pr_time="$PR_TIME_COLOR$PR_MOON%*$PR_NO_COLOR"
  200. pr_userhost="$PR_HOST_COLOR%n@%m$PR_NO_COLOR"
  201. pr_path="$PR_PATH_COLOR%~$PR_NO_COLOR"
  202. # A conditional expression - if there're one or more background jobs,
  203. # display [number of jobs]
  204. # http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
  205. pr_jobs="%1(j.[%j] .)"
  206. pr_sigil="$PR_SIGIL_COLOR$PR_SIGIL$PR_NO_COLOR"
  207. # __git_ps1 takes 2 parameters: Stuff for before the git prompt and stuff
  208. # for after. See .sh_common - if the git prompt is available, it'll use
  209. # that; otherwise it's a fallback to the much simpler parse_git_branch
  210. # defined there.
  211. __git_ps1 "$pr_meta$pr_time $pr_userhost:$pr_path" " $pr_jobs$pr_sigil "
  212. }
  213. # This will reset the prompt every so often - it definitely shouldn't happen
  214. # in situations where, for example, the git status prompt might be really
  215. # expensive - but it _is_ kind of neat:
  216. # TMOUT=15
  217. # TRAPALRM() { zle reset-prompt; }
  218. # }}}
  219. # history {{{
  220. # Record directory history to a simple text file:
  221. function chpwd {
  222. echo "$PWD" >> ~/.directory_history
  223. }
  224. # Record command history using commandlog:
  225. # https://code.p1k3.com/gitea/brennen/commandlog
  226. # https://p1k3.com/topics/commandlog/
  227. function bpb_preexec {
  228. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  229. # Log the current command, if commandlog is available - $commands seems
  230. # to be where available commands are hashed:
  231. (( $+commands[commandlog] )) && eval $(commandlog add "$@")
  232. }
  233. add-zsh-hook preexec bpb_preexec
  234. # }}}
  235. # syntax highlighting {{{
  236. # https://github.com/zsh-users/zsh-syntax-highlighting
  237. # Fish-style syntax highlighting - must be last thing sourced, install with:
  238. # sudo apt-get install zsh-syntax-highlighting
  239. ZSH_SYNTAX_HIGHLIGHTING_PATH=/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  240. [ -f "$ZSH_SYNTAX_HIGHLIGHTING_PATH" ] && source "$ZSH_SYNTAX_HIGHLIGHTING_PATH"
  241. # }}}