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.

.zshrc 7.3KB

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