]>
Commit | Line | Data |
---|---|---|
1 | ### options | |
2 | setopts=( | |
3 | ## 16.2.1 Changing Directories | |
4 | AUTO_CD CDABLE_VARS | |
5 | ## 16.2.2 Completion | |
6 | COMPLETE_IN_WORD GLOB_COMPLETE REC_EXACT | |
7 | ## 16.2.3 Expansion and Globbing | |
8 | EXTENDED_GLOB GLOB_DOTS GLOB_STAR_SHORT MAGIC_EQUAL_SUBST MARK_DIRS | |
9 | NUMERIC_GLOB_SORT | |
10 | ## 16.2.4 History | |
11 | # NOTE: NO_HIST_SAVE_BY_COPY to allow saving histfile if updating another | |
12 | # user's histfile. this is for compatibility with zsu. | |
13 | HIST_FCNTL_LOCK HIST_IGNORE_DUPS HIST_IGNORE_SPACE HIST_LEX_WORDS | |
14 | HIST_NO_STORE HIST_REDUCE_BLANKS NO_HIST_SAVE_BY_COPY SHARE_HISTORY | |
15 | ## 16.2.6 Input/Output | |
16 | NO_CLOBBER CLOBBER_EMPTY CORRECT_ALL INTERACTIVE_COMMENTS | |
17 | HASH_EXECUTABLES_ONLY | |
18 | ## 16.2.7 Job Control | |
19 | AUTO_CONTINUE LONG_LIST_JOBS | |
20 | ## 16.2.12 Zle | |
21 | NO_BEEP | |
22 | ) | |
23 | setopt $setopts | |
24 | unset setopts | |
25 | ||
26 | ### exports | |
27 | ## common | |
28 | export EDITOR=${$(whence -p nvim vim vi micro nano emacs)[(f)1]} | |
29 | export PAGER=${$(whence -p less micro nano more)[(f)1]:s/micro/& -readonly true -multiopen tab/:s/nano/& --view} | |
30 | ## grep | |
31 | # XXX: deprecated in GNU | |
32 | export GREP_OPTIONS=--color=auto | |
33 | ## histfile | |
34 | export HISTFILE=~/.histfile | |
35 | export HISTSIZE=10000 | |
36 | export SAVEHIST=$HISTSIZE | |
37 | ## python | |
38 | export PYTHONSTARTUP=${XDG_CONFIG_HOME:-~/.config}/pythonrc | |
39 | ## vim | |
40 | export VIMINIT='let$MYVIMRC=($XDG_CONFIG_HOME??($HOME."/.config"))."/vim/.vimrc"|execute"source"$MYVIMRC' | |
41 | ||
42 | ### imports | |
43 | autoload -Uz zmv | |
44 | autoload -Uz zmathfunc && zmathfunc | |
45 | ||
46 | ## vcs | |
47 | zstyle ':vcs_info:*' enable git | |
48 | #zstyle ':vcs_info:git*' check-for-changes true #too slow | |
49 | zstyle ':vcs_info:git*:dotfiles' check-for-changes true | |
50 | zstyle ':vcs_info:git*' check-for-staged-changes true | |
51 | autoload -Uz vcs_info | |
52 | ||
53 | ## compinit | |
54 | zstyle ':completion:*' auto-description '[arg] %d' | |
55 | zstyle ':completion:*' expand suffix | |
56 | zstyle ':completion:*' format '# %d' | |
57 | zstyle ':completion:*' group-name '' | |
58 | zstyle ':completion:*' ignore-parents parent | |
59 | zstyle ':completion:*' insert-unambiguous false | |
60 | zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} | |
61 | zstyle ':completion:*' list-prompt '%B%i%b' | |
62 | zstyle ':completion:*' list-suffixes true | |
63 | zstyle ':completion:*' matcher-list '' 'm:{[:lower:]}={[:upper:]}' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]} r:|[._-]=* r:|=*' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]} l:|=* r:|=*' | |
64 | zstyle ':completion:*' menu select=1 | |
65 | zstyle ':completion:*' original false | |
66 | zstyle ':completion:*' select-prompt '%B%l%b' | |
67 | zstyle ':completion:*' verbose true | |
68 | autoload -Uz compinit | |
69 | cache=${XDG_CACHE_HOME:-~/.cache}/zsh | |
70 | [[ -d $cache ]] || mkdir -p $cache | |
71 | compinit -d $cache/.zcompdump | |
72 | unset cache | |
73 | ||
74 | ### keys | |
75 | ## set up zkbd-style key array | |
76 | if [[ ! -v _sev_force_zkbd && ( -v terminfo || -v termcap ) ]] { | |
77 | # use application (keypad transmit) mode if the terminal supports it | |
78 | # NOTE: we have to do this because termcap/terminfo keys are usually | |
79 | # defined in application mode. terminals that do not define these | |
80 | # capabilities are assumed to always be in application mode. | |
81 | if [[ ( -v terminfo[smkx] && -v terminfo[rmkx] ) || | |
82 | ( -v termcap[ks] && -v terminfo[ke] ) ]] { | |
83 | autoload -Uz add-zle-hook-widget | |
84 | function _enter-application-mode { | |
85 | [[ -v terminfo[smkx] ]] && echoti smkx || echotc ks | |
86 | } | |
87 | add-zle-hook-widget line-init _enter-application-mode | |
88 | function _exit-application-mode { | |
89 | [[ -v terminfo[rmkx] ]] && echoti rmkx || echotc ke | |
90 | } | |
91 | add-zle-hook-widget line-finish _exit-application-mode | |
92 | trap _exit-application-mode EXIT | |
93 | } | |
94 | # pull keys from terminfo/termcap | |
95 | # TODO: Menu and more Shift- modifiers | |
96 | typeset -gA key | |
97 | typeset -lA newkey | |
98 | if [[ -v terminfo ]] { | |
99 | src=terminfo | |
100 | newkey=(F1 kf1 F2 kf2 F3 kf3 F4 kf4 F5 kf5 F6 kf6 F7 kf7 F8 kf8 F9 kf9 | |
101 | F10 kf10 F11 kf11 F12 kf12 | |
102 | Backspace kbs | |
103 | Shift-Tab kcbt | |
104 | Insert kich1 | |
105 | Home khome | |
106 | PageUp kpp | |
107 | Delete kdch1 | |
108 | End kend | |
109 | PageDown knp | |
110 | Up kcuu1 | |
111 | Down kcud1 | |
112 | Left kcub1 | |
113 | Right kcuf1 | |
114 | ) | |
115 | } elif [[ -v termcap ]] { | |
116 | src=termcap | |
117 | newkey=(F1 k1 F2 k2 F3 k3 F4 k4 F5 k5 F6 k6 F7 k7 F8 k8 F9 k9 | |
118 | F10 k\; F11 F1 F12 F2 | |
119 | Backspace kb | |
120 | Shift-Tab kB | |
121 | Insert kI | |
122 | Home kh | |
123 | PageUp kP | |
124 | Delete kD | |
125 | End @7 | |
126 | PageDown kN | |
127 | Up ku | |
128 | Down kd | |
129 | Left kl | |
130 | Right kr | |
131 | ) | |
132 | } | |
133 | for k v (${(kv)newkey}) { | |
134 | key[$k]=${${(P)src}[$v]} | |
135 | } | |
136 | unset k v src newkey | |
137 | } else { | |
138 | # use zkbd if termcap/terminfo unavailable | |
139 | function find_keymap { | |
140 | for f in ${ZDOTDIR:-~}/.zkbd/$TERM{-${DISPLAY:-$VENDOR-$OSTYPE},} | |
141 | [[ -f $f ]] && keymap=$f && break | |
142 | } | |
143 | find_keymap | |
144 | if [[ -z $keymap ]] { | |
145 | if read -q "?Can't read terminfo. Add new zkbd keymap? [y/N]"; then | |
146 | echo | |
147 | autoload -Uz zkbd && zkbd | |
148 | unfunction zkbd | |
149 | find_keymap | |
150 | fi | |
151 | echo | |
152 | } | |
153 | if [[ -n $keymap ]] { | |
154 | source $keymap | |
155 | } else { | |
156 | echo -E "Failed to source keymap file $keymap" >&2 | |
157 | } | |
158 | unfunction find_keymap; unset keymap | |
159 | } | |
160 | ||
161 | ## load history search | |
162 | autoload -Uz up-line-or-beginning-search down-line-or-beginning-search | |
163 | zle -N up-line-or-beginning-search | |
164 | zle -N down-line-or-beginning-search | |
165 | ||
166 | ## bind keys in both viins and vicmd modes | |
167 | # NOTE: cursor keys are bound to normal/raw/nontransmit mode strings by | |
168 | # default, and keys like Home and End are not bound at all, so we rebind | |
169 | # everything to cover all scenarios | |
170 | typeset -A a | |
171 | a=( | |
172 | #key viins vicmd | |
173 | Backspace 'backward-delete-char vi-backward-char' | |
174 | Insert 'overwrite-mode vi-insert' | |
175 | Home 'beginning-of-line' | |
176 | PageUp 'up-history -' | |
177 | Delete 'delete-char' | |
178 | End 'end-of-line' | |
179 | PageDown 'down-history -' | |
180 | Up 'up-line-or-beginning-search vi-up-line-or-history' | |
181 | Down 'down-line-or-beginning-search vi-down-line-or-history' | |
182 | Left 'backward-char' | |
183 | Right 'forward-char' | |
184 | ) | |
185 | for k v (${(kv)a}) { | |
186 | k=$key[$k] | |
187 | if [[ -z $k ]] { continue } | |
188 | v=($=v) | |
189 | bindkey -- $k $v[1] | |
190 | if [[ $v[2] == - ]] { | |
191 | # copy viins to vicmd verbatim | |
192 | bindkey -a -- $k $v[1] | |
193 | } elif (( $#v != 1 )) { | |
194 | # set vicmd to any other value | |
195 | bindkey -a -- $k $v[2] | |
196 | } else { | |
197 | # copy viins to vicmd and prepend vi- to it | |
198 | bindkey -a -- $k vi-$v[1] | |
199 | } | |
200 | } | |
201 | unset a k v | |
202 | ||
203 | ### aliases | |
204 | ## builtins | |
205 | alias rehash='_sev_setpath; rehash' | |
206 | ## utils | |
207 | alias h='fc -l -25' | |
208 | alias j='jobs -l' | |
209 | alias l='ls -AF' | |
210 | if [[ "$OSTYPE" =~ '^(free|net)bsd' ]] { | |
211 | alias ll='ls -lAFho' | |
212 | } else { | |
213 | alias ll='ls -lAFh' | |
214 | } | |
215 | alias p=\$PAGER | |
216 | alias e=\$EDITOR | |
217 | alias se=sudoedit | |
218 | alias syncwatch='sync & watch -d grep -Fe Dirty: -e Writeback: /proc/meminfo' | |
219 | if [[ -v commands[grep] ]] { | |
220 | [[ -v commands[fgrep] ]] || alias fgrep='grep -F' | |
221 | [[ -v commands[egrep] ]] || alias fgrep='grep -E' | |
222 | } | |
223 | for x (cat cmp diff grep test update) { | |
224 | [[ -v commands[zutils-z$x] ]] || alias z$x=zutils-z$x | |
225 | } | |
226 | # be paranoid | |
227 | alias cp='cp -ip' | |
228 | alias mv='mv -i' | |
229 | # zsh zmv with noglob wildcards | |
230 | alias zm='noglob zmv -WiM' | |
231 | alias zc='noglob zmv -WiC' | |
232 | alias zl='noglob zmv -WiL' | |
233 | alias sm='noglob zmv -Wip"sudo mv"' | |
234 | alias sc='noglob zmv -Wip"sudo cp"' | |
235 | alias sl='noglob zmv -Wip"sudo ln"' | |
236 | if [[ "$OSTYPE" =~ '^freebsd' ]] { | |
237 | # don't confirm if only a few files are deleted | |
238 | alias rm='rm -I' | |
239 | } else { | |
240 | # TODO: similar behavior for non-freebsd, or impliment in zsh | |
241 | alias rm='rm -i' | |
242 | } | |
243 | [[ -v commands[trash-put] ]] && alias t=trash-put | |
244 | # ps | |
245 | if [[ -v commands[pstree] && $commands[pstree]:A:t != busybox ]] { | |
246 | # use pstree, but NOT busybox pstree because it kinda sucks | |
247 | ps='pstree -wg3' | |
248 | } elif [[ "$OSTYPE" =~ '^freebsd' ]] { | |
249 | ps='ps -aSdfxwwouser=USR -ogroup=GRP -opid,nice=NI \ | |
250 | -o%cpu,%mem,tty,stat,start=START -oetime,command' | |
251 | } elif [[ $commands[ps]:A:t == busybox ]] { | |
252 | # busybox compatible | |
253 | ps="ps -eouser='USR ' -ogroup='GRP ' \ | |
254 | -opid=' PID' -onice=' NI' -ovsz=' MEM' \ | |
255 | -otty,stat,etime,comm" | |
256 | } else { | |
257 | # XXX: untested, posix | |
258 | # TODO: support gnu ps | |
259 | ps='ps -eouser=USR -ogroup=GRP -opid,nice=NI \ | |
260 | -opcpu=CPU -ovsz=MEM -otty,stat,etime,comm' | |
261 | } | |
262 | if [[ "$(basename "$PAGER")" = "less" ]] { | |
263 | ps="$ps | less -S" | |
264 | } else { | |
265 | ps="$ps | \"${PAGER:-more}\"" | |
266 | } | |
267 | alias pa=$ps | |
268 | alias spa="sudo $ps" | |
269 | unset ps | |
270 | ## py venv | |
271 | alias va='source bin/activate' | |
272 | alias vd=deactivate | |
273 | alias vu="python3 -mvenv --upgrade" | |
274 | alias svu="sudo python3 -mvenv --upgrade" | |
275 | ## git | |
276 | alias g=git | |
277 | alias gd='git diff' | |
278 | alias gdh='git diff HEAD' | |
279 | alias gds='git diff --staged' | |
280 | alias ga='git add' | |
281 | alias ga.='git add .' | |
282 | alias gai='git add -i' | |
283 | alias gap='git add -p' | |
284 | alias gc='git commit' | |
285 | alias gca='git commit --amend' | |
286 | alias gp='git push' | |
287 | alias gu='git pull' | |
288 | alias gl='git log' | |
289 | alias gt='git tree' # from gitconfig | |
290 | alias gs='git show' | |
291 | alias gst='git stash' | |
292 | alias gsp='git stash pop' | |
293 | ## cd/zoxide | |
294 | function up { | |
295 | \cd $(printf '../%.0s' {1..${1:-1}}) | |
296 | } | |
297 | alias u=up | |
298 | if [[ -v commands[zoxide] ]] { | |
299 | # https://github.com/ajeetdsouza/zoxide/issues/513 | |
300 | eval "${$(zoxide init zsh):s#_files -/#_cd#}" | |
301 | alias cd=z | |
302 | } | |
303 | alias cd..=up | |
304 | ## dotfiles | |
305 | alias dfu='function { | |
306 | pushd -q ${$(echo -E - ~/.zshenv):P:h:h} | |
307 | git pull && | |
308 | git submodule init && | |
309 | git submodule sync && | |
310 | git submodule update | |
311 | popd -q | |
312 | }' | |
313 | ## nocorrect | |
314 | # zsh doesnt really handle sudo very well, so ignore it | |
315 | alias sudo='nocorrect sudo' | |
316 | ## docker compose | |
317 | alias dcp='sudo docker compose pull' | |
318 | alias dcu='sudo docker compose up -d' | |
319 | alias dcr='sudo docker compose restart' | |
320 | alias dcl='sudo docker compose logs -f' | |
321 | ||
322 | ### hooks | |
323 | autoload -Uz add-zsh-hook | |
324 | typeset -gi _sev_exectime | |
325 | function sev_preexec { | |
326 | # change terminal title to show command | |
327 | print -n "\e]2;$(print -P '%#')${SSH_CLIENT+$USER@$HOST:}$1\e\\" | |
328 | # save last exec time for bell | |
329 | # XXX: does not run for blank cmdline | |
330 | _sev_exectime=$SECONDS | |
331 | # update gpg forward, to always have unique filename and avoid clashes | |
332 | if [[ -v _GNUPG_SOCK_DEST_EXT ]] { | |
333 | export _GNUPG_SOCK_DEST_EXT=$(date +%s).$RANDOM | |
334 | export _GNUPG_SOCK_DEST=$_GNUPG_SOCK_DEST_BASE.$_GNUPG_SOCK_DEST_EXT | |
335 | } | |
336 | } | |
337 | add-zsh-hook preexec sev_preexec | |
338 | function sev_precmd { | |
339 | # change terminal title | |
340 | # TODO: update and send BEL when job status changes | |
341 | print -Pn "\e]2;%(1j,%j,)%#${SSH_CLIENT+$USER@$HOST:}%~\e\\" | |
342 | # bell if exec takes 5s | |
343 | if (( SECONDS - _sev_exectime >= 5 )) print "\a" | |
344 | # we could update vcs_info here, but let prompt take care of it | |
345 | # if it doesn't use vcs, it can be ignored safely | |
346 | } | |
347 | add-zsh-hook precmd sev_precmd | |
348 | function sev_chpwd { | |
349 | # echo dir on cwd change | |
350 | ls -AF | |
351 | } | |
352 | add-zsh-hook chpwd sev_chpwd | |
353 | ||
354 | ### system-specific configs and aliases | |
355 | case $OSTYPE; in | |
356 | freebsd*) | |
357 | ## colors | |
358 | export CLICOLOR= | |
359 | export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30' | |
360 | ||
361 | ## sound | |
362 | function s { sysctl hw.snd.default_unit${1:+\=$1} } | |
363 | alias vol mixer | |
364 | ||
365 | ## install port dependencies from pkg (like pkgsrc `bmake bin-install') | |
366 | # XXX: should probably use package-depends where possible, breaks when | |
367 | # port name is different to package name | |
368 | # (eg. graphics/sdl20 == sdl2, devel/glib20 == glib2, etc) | |
369 | function portpkg { | |
370 | case "$1" { | |
371 | build|run) | |
372 | sudo pkg install -AU $(make ${1}-depends-list | | |
373 | sed 's_/usr/ports/_ _' | tr -d '\n') | |
374 | ;; | |
375 | *) echo "Usage: \`portpkg <build|run>' in a port directory" | |
376 | return 1;; | |
377 | } | |
378 | };; | |
379 | netbsd) | |
380 | ## sound | |
381 | function s { | |
382 | if [[ -z "$1" ]] { | |
383 | ll /dev/mixer /dev/sound /dev/audio | |
384 | return | |
385 | } | |
386 | for x in mixer sound audio; do | |
387 | ln -sf /dev/$x"$1" /dev/$x | |
388 | done | |
389 | } | |
390 | function vol { | |
391 | if [[ -z "$1" ]] { | |
392 | for x in $(mixerctl -a | grep 'outputs\.master'); do | |
393 | echo -E - $x | |
394 | done | |
395 | return | |
396 | } | |
397 | mixerctl -w outputs.master"$2"="$1" | |
398 | };; | |
399 | *) | |
400 | ## sound | |
401 | # TODO: test alsa/oss/sndio/portaudio/pulse/pipewire in order of importance | |
402 | function s {} | |
403 | function vol {} | |
404 | esac | |
405 | ||
406 | ### prompt | |
407 | autoload -Uz promptinit && promptinit | |
408 | prompt arrows | |
409 | ||
410 | ### load site-specific | |
411 | load-site-dotfile zshrc |