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.

269 lines
7.5 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=15000
  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. zstyle :compinstall filename '/home/brennen/.zshrc'
  34. compinit
  35. # }}}
  36. # keybindings {{{
  37. # You can get bindkey strings with Ctrl-v followed by your key sequence.
  38. # https://wiki.archlinux.org/index.php/Zsh#History_search
  39. # Search up/down for matching thing on arrows - you can still use ctrl-n /
  40. # ctrl-p to move up and down in the overall command history:
  41. autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
  42. zle -N up-line-or-beginning-search
  43. zle -N down-line-or-beginning-search
  44. [[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-beginning-search
  45. [[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-beginning-search
  46. # https://wiki.archlinux.org/index.php/Zsh#File_manager_key_binds
  47. # Alt-Left for previous directory and Alt-Up for parent
  48. cd_undo_key () {
  49. echo
  50. popd
  51. zle reset-prompt
  52. ls
  53. zle reset-prompt
  54. }
  55. cd_parent_key () {
  56. echo
  57. pushd ..
  58. zle reset-prompt
  59. ls
  60. zle reset-prompt
  61. }
  62. zle -N cd_parent_key
  63. zle -N cd_undo_key
  64. bindkey '^[[1;3A' cd_parent_key
  65. bindkey '^[[1;3D' cd_undo_key
  66. # Invoke h history function from .sh_common with Alt-H:
  67. invoke_h () {
  68. h
  69. zle reset-prompt
  70. }
  71. zle -N invoke_h
  72. bindkey '^[H' invoke_h
  73. # fzf fuzzyfinder for use with Alt-c, Ctrl-r, Ctrl-t:
  74. [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
  75. # }}}
  76. # prompt configuration {{{
  77. # Grab some colors:
  78. autoload colors zsh/terminfo
  79. if [[ "$terminfo[colors]" -ge 8 ]]; then
  80. colors
  81. fi
  82. for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
  83. eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
  84. eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
  85. (( count = $count + 1 ))
  86. done
  87. PR_NO_COLOR="%{$terminfo[sgr0]%}"
  88. # Next, do per-host color and sigils.
  89. #
  90. # Since this will be used in a $PS1, colors can be either of the named values
  91. # from the above loop, or numerical escapes like %F{nnn} for Foreground and
  92. # %K{nnn} for background. The latter will only work on 256 color terminals,
  93. # although I'm not sure what the failure mode looks like.
  94. #
  95. # Great for sigils:
  96. # https://en.wikipedia.org/wiki/Unicode_Geometric_Shapes
  97. # https://en.wikipedia.org/wiki/List_of_symbols
  98. PR_SIGIL=\$
  99. PR_PATH_COLOR=$PR_BLUE;
  100. PR_TIME_COLOR=$PR_LIGHT_BLUE
  101. PR_GIT_COLOR=$PR_NO_COLOR
  102. PR_SIGIL_COLOR=$PR_NO_COLOR
  103. case "$HOST" in
  104. 'catastrophe')
  105. PR_HOST_COLOR=$PR_LIGHT_BLUE
  106. ;;
  107. 'desiderata')
  108. PR_HOST_COLOR=$PR_LIGHT_BLUE
  109. PR_SIGIL=
  110. ;;
  111. 'escalation')
  112. PR_HOST_COLOR=$PR_LIGHT_GREEN
  113. PR_SIGIL=
  114. ;;
  115. 'errata')
  116. PR_HOST_COLOR=$PR_LIGHT_BLUE
  117. PR_SIGIL=
  118. ;;
  119. 'externality')
  120. PR_HOST_COLOR=$PR_LIGHT_GREEN
  121. PR_SIGIL=
  122. ;;
  123. 'exuberance')
  124. PR_HOST_COLOR=$PR_LIGHT_YELLOW
  125. PR_SIGIL=
  126. ;;
  127. 'fragility')
  128. PR_HOST_COLOR=$PR_WHITE
  129. PR_SIGIL=
  130. ;;
  131. 'inertia')
  132. PR_HOST_COLOR='%F{27}'
  133. PR_TIME_COLOR='%F{29}'
  134. PR_GIT_COLOR='%F{93}'
  135. PR_SIGIL_COLOR=$PR_HOST_COLOR
  136. # PR_SIGIL=▣
  137. PR_SIGIL=$(fragment-bullet)
  138. ;;
  139. 'metaphor')
  140. PR_HOST_COLOR='%F{69}'
  141. PR_TIME_COLOR='%F{161}'
  142. PR_GIT_COLOR='%F{32}'
  143. PR_PATH_COLOR='%F{242}'
  144. PR_SIGIL_COLOR='%F{220}'
  145. # see bin/fragment-bullet:
  146. PR_SIGIL=$(fragment-bullet)
  147. ;;
  148. 'novena-edward-norway')
  149. PR_HOST_COLOR=$PR_LIGHT_BLUE
  150. PR_SIGIL=
  151. ;;
  152. 'pisces')
  153. PR_HOST_COLOR=$PR_GREEN
  154. ;;
  155. 'raspberrypi')
  156. PR_HOST_COLOR=$PR_MAGENTA
  157. ;;
  158. 'kropotkin')
  159. PR_HOST_COLOR=$PR_BLUE
  160. ;;
  161. *)
  162. PR_HOST_COLOR=$PR_BLUE
  163. ;;
  164. esac
  165. # Stash values here for inclusion at beginning of prompt:
  166. pr_meta=""
  167. # Run to install some color swatches in the prompt for experimenting with
  168. # host colors and such:
  169. function colortest {
  170. colortest=""
  171. for c in `seq 0 256`; do
  172. colortest="$colortest %K{$c} $c "
  173. done
  174. pr_meta="$colortest "
  175. }
  176. # Reset the prompt on every command to get that parse_git_branch function to
  177. # run. Because this runs before every command, there's no need to set PS1
  178. # elsewhere. Also sets xterm title (which works for other terminals as well):
  179. function precmd {
  180. # http://tldp.org/HOWTO/Xterm-Title-4.html
  181. case $TERM in
  182. xterm*)
  183. print -Pn "\e]0;%n@%m: %~\a"
  184. ;;
  185. esac
  186. pr_time="$PR_TIME_COLOR%*$PR_NO_COLOR"
  187. pr_userhost="$PR_HOST_COLOR%n@%m$PR_NO_COLOR"
  188. pr_path="$PR_PATH_COLOR%~$PR_NO_COLOR"
  189. # A conditional expression - if there're one or more background jobs,
  190. # display [number of jobs]
  191. # http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
  192. pr_jobs="%1(j.[%j] .)"
  193. pr_sigil="$PR_SIGIL_COLOR$PR_SIGIL$PR_NO_COLOR"
  194. # __git_ps1 takes 2 parameters: Stuff for before the git prompt and stuff
  195. # for after. See .sh_common - if the git prompt is available, it'll use
  196. # that; otherwise it's a fallback to the much simpler parse_git_branch
  197. # defined there.
  198. __git_ps1 "$pr_meta$pr_time $pr_userhost:$pr_path" " $pr_jobs$pr_sigil "
  199. }
  200. # This will reset the prompt every so often - it definitely shouldn't happen
  201. # in situations where, for example, the git status prompt might be really
  202. # expensive - but it _is_ kind of neat:
  203. # TMOUT=15
  204. # TRAPALRM() { zle reset-prompt; }
  205. # }}}
  206. # history {{{
  207. # Record directory history to a simple text file:
  208. function chpwd {
  209. echo "$PWD" >> ~/.directory_history
  210. }
  211. # Record command history using commandlog:
  212. # https://code.p1k3.com/gitea/brennen/commandlog
  213. # https://p1k3.com/topics/commandlog/
  214. function bpb_preexec {
  215. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  216. # Log the current command, if commandlog is available - $commands seems
  217. # to be where available commands are hashed:
  218. (( $+commands[commandlog] )) && commandlog add "$@"
  219. }
  220. add-zsh-hook preexec bpb_preexec
  221. # }}}
  222. # syntax highlighting {{{
  223. # https://github.com/zsh-users/zsh-syntax-highlighting
  224. # Fish-style syntax highlighting - must be last thing sourced, install with:
  225. # sudo apt-get install zsh-syntax-highlighting
  226. ZSH_SYNTAX_HIGHLIGHTING_PATH=/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  227. [ -f "$ZSH_SYNTAX_HIGHLIGHTING_PATH" ] && source "$ZSH_SYNTAX_HIGHLIGHTING_PATH"
  228. # }}}