]> git.sev.monster Git - dotfiles.git/blame - etc/zsh/.zshrc
zshrc: update options and history
[dotfiles.git] / etc / zsh / .zshrc
CommitLineData
43016843 1### options
1795d46e 2setopts=(
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)
23setopt $setopts
24unset setopts
43016843 25
79d4a356 26### exports
27## common
8d4a98e1 28export EDITOR=${$(whence -p nvim vim vi micro nano emacs)[(f)1]}
29export PAGER=${$(whence -p less micro nano more)[(f)1]:s/micro/& -readonly true -multiopen tab/:s/nano/& --view}
79d4a356 30## grep
31# XXX: deprecated in GNU
32export GREP_OPTIONS=--color=auto
33## histfile
34export HISTFILE=~/.histfile
1795d46e 35export HISTSIZE=10000
36export SAVEHIST=$HISTSIZE
79d4a356 37## python
8d4a98e1 38export PYTHONSTARTUP=${XDG_CONFIG_HOME:-~/.config}/pythonrc
39## vim
e69caf64 40export VIMINIT='let$MYVIMRC=($XDG_CONFIG_HOME??($HOME."/.config"))."/vim/.vimrc"|execute"source"$MYVIMRC'
79d4a356 41
2bf94bab 42### imports
43autoload -Uz zmv
44autoload -Uz zmathfunc && zmathfunc
45
46## vcs
47zstyle ':vcs_info:*' enable git
48#zstyle ':vcs_info:git*' check-for-changes true #too slow
49zstyle ':vcs_info:git*:dotfiles' check-for-changes true
50zstyle ':vcs_info:git*' check-for-staged-changes true
51autoload -Uz vcs_info
52
53## compinit
54zstyle ':completion:*' auto-description '[arg] %d'
55zstyle ':completion:*' expand suffix
56zstyle ':completion:*' format '# %d'
57zstyle ':completion:*' group-name ''
58zstyle ':completion:*' ignore-parents parent
59zstyle ':completion:*' insert-unambiguous false
60zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
61zstyle ':completion:*' list-prompt '%B%i%b'
62zstyle ':completion:*' list-suffixes true
63zstyle ':completion:*' matcher-list '' 'm:{[:lower:]}={[:upper:]}' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]} r:|[._-]=* r:|=*' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]} l:|=* r:|=*'
64zstyle ':completion:*' menu select=1
65zstyle ':completion:*' original false
66zstyle ':completion:*' select-prompt '%B%l%b'
67zstyle ':completion:*' verbose true
68autoload -Uz compinit
69cache=${XDG_CACHE_HOME:-~/.cache}/zsh
70[[ -d $cache ]] || mkdir -p $cache
71compinit -d $cache/.zcompdump
72unset cache
73
43016843 74### keys
b484bab8 75## set up zkbd-style key array
76if [[ ! -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
43016843 139 function find_keymap {
8d4a98e1 140 for f in ${ZDOTDIR:-~}/.zkbd/$TERM{-${DISPLAY:-$VENDOR-$OSTYPE},}
43016843 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 {
294ed44e 156 echo -E "Failed to source keymap file $keymap" >&2
43016843 157 }
158 unfunction find_keymap; unset keymap
43016843 159}
2bf94bab 160
5b9a654a 161## load history search
43016843 162autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
163zle -N up-line-or-beginning-search
43016843 164zle -N down-line-or-beginning-search
43016843 165
5b9a654a 166## bind keys in both viins and vicmd modes
b484bab8 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
5b9a654a 170typeset -A a
171a=(
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)
b484bab8 185for k v (${(kv)a}) {
5b9a654a 186 k=$key[$k]
b484bab8 187 if [[ -z $k ]] { continue }
5b9a654a 188 v=($=v)
189 bindkey -- $k $v[1]
b484bab8 190 if [[ $v[2] == - ]] {
5b9a654a 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 }
b484bab8 200}
5b9a654a 201unset a k v
202
8a92a2c0 203### aliases
204## builtins
205alias rehash='_sev_setpath; rehash'
206## utils
8d4a98e1 207alias h='fc -l -25'
5b9a654a 208alias j='jobs -l'
209alias l='ls -AF'
43016843 210if [[ "$OSTYPE" =~ '^(free|net)bsd' ]] {
5b9a654a 211 alias ll='ls -lAFho'
43016843 212} else {
5b9a654a 213 alias ll='ls -lAFh'
43016843 214}
b053a418 215alias p=\$PAGER
216alias e=\$EDITOR
43016843 217alias se=sudoedit
5b9a654a 218# be paranoid
43016843 219alias cp='cp -ip'
220alias mv='mv -i'
bfbd0519 221alias zm='noglob zmv -WiM'
222alias zc='noglob zmv -WiC'
223alias zl='noglob zmv -WiL'
224alias sm='noglob zmv -Wip"sudo mv"'
225alias sc='noglob zmv -Wip"sudo cp"'
226alias sl='noglob zmv -Wip"sudo ln"'
43016843 227if [[ "$OSTYPE" =~ '^freebsd' ]] {
228 # don't confirm if only a few files are deleted
229 alias rm='rm -I'
230} else {
231 # TODO: similar behavior for non-freebsd, or impliment in zsh
232 alias rm='rm -i'
233}
8d4a98e1 234[[ -v commands[trash-put] ]] && alias t=trash-put
43016843 235## py venv
79d4a356 236alias va='source bin/activate'
237alias vd=deactivate
43016843 238## ps
8d4a98e1 239source ~/.local/bin/.check-busybox
79d4a356 240if [[ -v commands[pstree] ]] && ! check-busybox pstree; then
43016843 241 # use pstree, but NOT busybox pstree because it kinda sucks
242 ps="pstree -wg3"
243elif [[ "$OSTYPE" =~ '^freebsd' ]]; then
244 ps="ps -aSdfxwwouser=USR -ogroup=GRP -opid,nice=NI \
245 -o%cpu,%mem,tty,stat,start=START -oetime,command"
246elif check-busybox ps; then
247 # busybox compatible
248 ps="ps -eouser='USR ' -ogroup='GRP ' \
249 -opid=' PID' -onice=' NI' -ovsz=' MEM' \
250 -otty,stat,etime,comm"
251else
252 # XXX: untested, posix
253 # TODO: support gnu ps
254 ps="ps -eouser=USR -ogroup=GRP -opid,nice=NI \
255 -opcpu=CPU -ovsz=MEM -otty,stat,etime,comm"
256fi
257unfunction check-busybox
258if [[ "$(basename "$PAGER")" = "less" ]] {
109fad20 259 ps="$ps | less -S"
43016843 260} else {
261 ps="$ps | \"${PAGER:-more}\""
262}
79d4a356 263alias pa=$ps
43016843 264alias spa="sudo $ps"
265unset ps
2bf94bab 266## git
390f6e67 267alias g=git
4851c231 268alias gd='git diff'
2bf94bab 269alias gdh='git diff HEAD'
270alias ga='git add'
0ca7cf36 271alias ga.='git add .'
2bf94bab 272alias gc='git commit'
273alias gca='git commit --amend'
274alias gp='git push'
4851c231 275alias gu='git pull'
390f6e67 276alias gl='git log'
277alias gt='git tree' # from gitconfig
2bf94bab 278## cd/zoxide
5b9a654a 279function up {
b053a418 280 \cd $(printf '../%.0s' {1..${1:-1}})
5b9a654a 281}
79d4a356 282alias u=up
b053a418 283if [[ -v commands[zoxide] ]] {
8a92a2c0 284 # https://github.com/ajeetdsouza/zoxide/issues/513
285 eval "${$(zoxide init zsh):s#_files -/#_cd#}"
6c74cef6 286 alias cd=z
b053a418 287}
0ca7cf36 288alias cd..=up
4851c231 289## dotfiles
8ec56e91 290alias dfu='d=${$(echo -E - ~/.zshenv):P:h:h};GIT_DIR=$d/.git GIT_WORK_TREE=$d {
291 git pull &&
292 git submodules init &&
293 git submodules sync &&
294 git submodules update
295}'
5b9a654a 296
43016843 297### hooks
298autoload -Uz add-zsh-hook
299_sev_exectime=
300function sev_preexec {
301 # change terminal title to show command
302 print -n "\e]2;$(print -P '%#')${SSH_CLIENT+$USER@$HOST:}$1\e\\"
303 # save last exec time for bell
304 # XXX: does not run for blank cmdline
305 _sev_exectime=$SECONDS
1ef46e01 306 # update gpg forward, to always have unique filename and avoid clashes
307 if [[ -v _GNUPG_SOCK_DEST_EXT ]] {
308 export _GNUPG_SOCK_DEST_EXT=$(date +%s).$RANDOM
309 export _GNUPG_SOCK_DEST=$_GNUPG_SOCK_DEST_BASE.$_GNUPG_SOCK_DEST_EXT
310 }
43016843 311}
312add-zsh-hook preexec sev_preexec
313function sev_precmd {
314 # change terminal title
315 # TODO: update and send BEL when job status changes
316 print -Pn "\e]2;%(1j,%j,)%#${SSH_CLIENT+$USER@$HOST:}%~\e\\"
317 # bell if exec takes 5s
318 if (( SECONDS - _sev_exectime >= 5 )) print "\a"
319 # we could update vcs_info here, but let prompt take care of it
320 # if it doesn't use vcs, it can be ignored safely
321}
322add-zsh-hook precmd sev_precmd
323function sev_chpwd {
324 # echo dir on cwd change
325 ls -AF
326}
327add-zsh-hook chpwd sev_chpwd
328
329### system-specific configs and aliases
79d4a356 330case $OSTYPE; in
43016843 331 freebsd*)
8a92a2c0 332 ## colors
43016843 333 export CLICOLOR=
334 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'
335
336 ## sound
337 function s { sysctl hw.snd.default_unit${1:+\=$1} }
338 alias vol mixer
339
340 ## install port dependencies from pkg (like pkgsrc `bmake bin-install')
341 # XXX: should probably use package-depends where possible, breaks when
8a92a2c0 342 # port name is different to package name
343 # (eg. graphics/sdl20 == sdl2, devel/glib20 == glib2, etc)
43016843 344 function portpkg {
345 case "$1" {
346 build|run)
347 sudo pkg install -AU $(make ${1}-depends-list |
348 sed 's_/usr/ports/_ _' | tr -d '\n')
349 ;;
350 *) echo "Usage: \`portpkg <build|run>' in a port directory"
351 return 1;;
352 }
353 };;
354 netbsd)
355 ## sound
356 function s {
357 if [[ -z "$1" ]] {
358 ll /dev/mixer /dev/sound /dev/audio
359 return
360 }
361 for x in mixer sound audio; do
362 ln -sf /dev/$x"$1" /dev/$x
363 done
364 }
365 function vol {
366 if [[ -z "$1" ]] {
367 for x in $(mixerctl -a | grep 'outputs\.master'); do
294ed44e 368 echo -E - $x
43016843 369 done
370 return
371 }
372 mixerctl -w outputs.master"$2"="$1"
373 };;
374 *)
375 ## sound
8d4a98e1 376 # TODO: test alsa/oss/sndio/portaudio/pulse/pipewire in order of importance
43016843 377 function s {}
378 function vol {}
379esac
380
43016843 381### prompt
382autoload -Uz promptinit && promptinit
383prompt arrows
384
8eb81f95 385### load site-specific
4ced48ed 386load-site-dotfile zshrc
This page took 0.140093 seconds and 4 git commands to generate.