]> git.sev.monster Git - dotfiles.git/blame - base/.zshrc
fix gpg forward GNUPGHOME reset in cleanup
[dotfiles.git] / base / .zshrc
CommitLineData
43016843 1### options
2setopt NO_BEEP NO_CLOBBER
3## cd
4setopt AUTO_CD CDABLE_VARS
5## completion
6setopt GLOB_COMPLETE
79d4a356 7## globbing
43016843 8setopt EXTENDED_GLOB GLOB_DOTS MARK_DIRS NOMATCH NUMERIC_GLOB_SORT
43016843 9## jobs
79d4a356 10setopt AUTO_CONTINUE LONG_LIST_JOBS
43016843 11## history
973a2767 12setopt NO_HIST_SAVE_BY_COPY HIST_IGNORE_DUPS SHARE_HISTORY HIST_REDUCE_BLANKS
43016843 13
79d4a356 14### imports
15autoload -Uz zmv
16autoload -Uz zmathfunc && zmathfunc
17
18### exports
19## common
20export EDITOR=vim
21export PAGER=less
22## grep
23# XXX: deprecated in GNU
24export GREP_OPTIONS=--color=auto
25## histfile
26export HISTFILE=~/.histfile
27export HISTSIZE=1000
28export SAVEHIST=1000
29## python
30export PYTHONSTARTUP=~/.pythonrc
d23b28eb 31## wayland/sway
32# XXX: only do this if we're actually in wayland, but without a start script
33export XDG_SESSION_TYPE=wayland
34export QT_QPA_PLATFORM=wayland-egl
35export QT_WAYLAND_DISABLE_WINDOWDECORATION=-1
36export SDL_VIDEODRIVER=wayland
37export TERMINAL=footclient
79d4a356 38
43016843 39### keys
43016843 40bindkey -v
41KEYTIMEOUT=10
42## populate key array
43if (( $#terminfo == 0 )) {
44 # terminfo is not set or empty
45 function find_keymap {
79d4a356 46 for f in ${ZDOTDIR:-$HOME}/.zkbd/$TERM{-${DISPLAY:-$VENDOR-$OSTYPE},}
43016843 47 [[ -f $f ]] && keymap=$f && break
48 }
49 find_keymap
50 if [[ -z $keymap ]] {
51 if read -q "?Can't read terminfo. Add new zkbd keymap? [y/N]"; then
52 echo
53 autoload -Uz zkbd && zkbd
54 unfunction zkbd
55 find_keymap
56 fi
57 echo
58 }
59 if [[ -n $keymap ]] {
60 source $keymap
61 } else {
c3608beb 62 echo "Failed to source keymap file $keymap" >&2
43016843 63 }
64 unfunction find_keymap; unset keymap
65} else {
66 # activate application mode for zle so terminfo keys work
c3608beb 67 # NOTE: don't do this for zkbd since application mode shouldn't have been
68 # enabled by zkbd when the keymap file was generated
43016843 69 if [[ -v terminfo[smkx] && -v terminfo[rmkx] ]] {
70 autoload -Uz add-zle-hook-widget
71 function _enter-application-mode { echoti smkx }
72 add-zle-hook-widget line-init _enter-application-mode
73 function _exit-application-mode { echoti rmkx }
74 add-zle-hook-widget line-finish _exit-application-mode
75 trap _exit-application-mode EXIT
76 }
77 # match zkbd hash as best we can to terminfo
78 typeset -gA key
5b9a654a 79 key=(F1 kf1 F2 kf2 F3 kf3 F4 kf4 F5 kf5 F6 kf6 F7 kf7 F8 kf8 F9 kf9
80 F10 kf10 F11 kf11 F12 kf12
81 Backspace kbs
82 Backtab kcbt
83 Shift-Tab kcbt
84 Insert kich1
85 Home khome
86 PageUp kpp
87 Delete kdch1
88 End kend
89 PageDown knp
90 Up kcuu1
91 Down kcud1
92 Left kcub1
93 Right kcuf1
94 )
95 for k v in ${(kv)key}; do
96 key[$k]=$terminfo[$v]
97 done; unset k v
43016843 98}
5b9a654a 99## load history search
43016843 100autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
101zle -N up-line-or-beginning-search
43016843 102zle -N down-line-or-beginning-search
43016843 103
5b9a654a 104## bind keys in both viins and vicmd modes
105typeset -A a
106a=(
107 #key viins vicmd
108 Backspace 'backward-delete-char vi-backward-char'
109 Insert 'overwrite-mode vi-insert'
110 Home 'beginning-of-line'
111 PageUp 'up-history -'
112 Delete 'delete-char'
113 End 'end-of-line'
114 PageDown 'down-history -'
115 Up 'up-line-or-beginning-search vi-up-line-or-history'
116 Down 'down-line-or-beginning-search vi-down-line-or-history'
117 Left 'backward-char'
118 Right 'forward-char'
119)
120for k v in ${(kv)a}; do
121 k=$key[$k]
122 if [[ -z "$k" ]] { continue }
123 v=($=v)
124 bindkey -- $k $v[1]
125 if [[ $v[2] == '-' ]] {
126 # copy viins to vicmd verbatim
127 bindkey -a -- $k $v[1]
128 } elif (( $#v != 1 )) {
129 # set vicmd to any other value
130 bindkey -a -- $k $v[2]
131 } else {
132 # copy viins to vicmd and prepend vi- to it
133 bindkey -a -- $k vi-$v[1]
134 }
135done
136unset a k v
137
138### abbreviation aliases
139alias h='history -25'
140alias j='jobs -l'
141alias l='ls -AF'
43016843 142if [[ "$OSTYPE" =~ '^(free|net)bsd' ]] {
5b9a654a 143 alias ll='ls -lAFho'
43016843 144} else {
5b9a654a 145 alias ll='ls -lAFh'
43016843 146}
79d4a356 147alias p=${PAGER:-more} # TODO: make sure more is there or use alternate
148alias e=${EDITOR:-vi} # TODO: make sure vi is there or use alternate
43016843 149alias se=sudoedit
5b9a654a 150# be paranoid
43016843 151alias cp='cp -ip'
152alias mv='mv -i'
153if [[ "$OSTYPE" =~ '^freebsd' ]] {
154 # don't confirm if only a few files are deleted
155 alias rm='rm -I'
156} else {
157 # TODO: similar behavior for non-freebsd, or impliment in zsh
158 alias rm='rm -i'
159}
43016843 160## py venv
79d4a356 161alias va='source bin/activate'
162alias vd=deactivate
43016843 163## ps
43016843 164source ~/bin/.check-busybox
79d4a356 165if [[ -v commands[pstree] ]] && ! check-busybox pstree; then
43016843 166 # use pstree, but NOT busybox pstree because it kinda sucks
167 ps="pstree -wg3"
168elif [[ "$OSTYPE" =~ '^freebsd' ]]; then
169 ps="ps -aSdfxwwouser=USR -ogroup=GRP -opid,nice=NI \
170 -o%cpu,%mem,tty,stat,start=START -oetime,command"
171elif check-busybox ps; then
172 # busybox compatible
173 ps="ps -eouser='USR ' -ogroup='GRP ' \
174 -opid=' PID' -onice=' NI' -ovsz=' MEM' \
175 -otty,stat,etime,comm"
176else
177 # XXX: untested, posix
178 # TODO: support gnu ps
179 ps="ps -eouser=USR -ogroup=GRP -opid,nice=NI \
180 -opcpu=CPU -ovsz=MEM -otty,stat,etime,comm"
181fi
182unfunction check-busybox
183if [[ "$(basename "$PAGER")" = "less" ]] {
109fad20 184 ps="$ps | less -S"
43016843 185} else {
186 ps="$ps | \"${PAGER:-more}\""
187}
79d4a356 188alias pa=$ps
43016843 189alias spa="sudo $ps"
190unset ps
191
5b9a654a 192### specialized aliases
193## go up directories
194function up {
195 cd $(printf '../%.0s' {1..${1:-1}})
196}
79d4a356 197alias u=up
5b9a654a 198## zoxide
199[[ -v commands[zoxide] ]] && eval "$(zoxide init zsh)"
200
43016843 201### hooks
202autoload -Uz add-zsh-hook
203_sev_exectime=
204function sev_preexec {
205 # change terminal title to show command
206 print -n "\e]2;$(print -P '%#')${SSH_CLIENT+$USER@$HOST:}$1\e\\"
207 # save last exec time for bell
208 # XXX: does not run for blank cmdline
209 _sev_exectime=$SECONDS
1ef46e01 210 # update gpg forward, to always have unique filename and avoid clashes
211 if [[ -v _GNUPG_SOCK_DEST_EXT ]] {
212 export _GNUPG_SOCK_DEST_EXT=$(date +%s).$RANDOM
213 export _GNUPG_SOCK_DEST=$_GNUPG_SOCK_DEST_BASE.$_GNUPG_SOCK_DEST_EXT
214 }
43016843 215}
216add-zsh-hook preexec sev_preexec
217function sev_precmd {
218 # change terminal title
219 # TODO: update and send BEL when job status changes
220 print -Pn "\e]2;%(1j,%j,)%#${SSH_CLIENT+$USER@$HOST:}%~\e\\"
221 # bell if exec takes 5s
222 if (( SECONDS - _sev_exectime >= 5 )) print "\a"
223 # we could update vcs_info here, but let prompt take care of it
224 # if it doesn't use vcs, it can be ignored safely
225}
226add-zsh-hook precmd sev_precmd
227function sev_chpwd {
228 # echo dir on cwd change
229 ls -AF
230}
231add-zsh-hook chpwd sev_chpwd
232
233### system-specific configs and aliases
79d4a356 234case $OSTYPE; in
43016843 235 freebsd*)
236 # colors
237 export CLICOLOR=
238 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'
239
240 ## sound
241 function s { sysctl hw.snd.default_unit${1:+\=$1} }
242 alias vol mixer
243
244 ## install port dependencies from pkg (like pkgsrc `bmake bin-install')
245 # XXX: should probably use package-depends where possible, breaks when
246 # port name is different to package name
247 # (eg. graphics/sdl20 == sdl2, devel/glib20 == glib2, etc)
248 function portpkg {
249 case "$1" {
250 build|run)
251 sudo pkg install -AU $(make ${1}-depends-list |
252 sed 's_/usr/ports/_ _' | tr -d '\n')
253 ;;
254 *) echo "Usage: \`portpkg <build|run>' in a port directory"
255 return 1;;
256 }
257 };;
258 netbsd)
259 ## sound
260 function s {
261 if [[ -z "$1" ]] {
262 ll /dev/mixer /dev/sound /dev/audio
263 return
264 }
265 for x in mixer sound audio; do
266 ln -sf /dev/$x"$1" /dev/$x
267 done
268 }
269 function vol {
270 if [[ -z "$1" ]] {
271 for x in $(mixerctl -a | grep 'outputs\.master'); do
272 echo $x
273 done
274 return
275 }
276 mixerctl -w outputs.master"$2"="$1"
277 };;
278 *)
279 ## sound
280 # TODO: test alsa/oss/sndio/portaudio/pulse in order of importance
281 function s {}
282 function vol {}
283esac
284
285### modules & styles
286## vcs
287zstyle ':vcs_info:*' enable git
288#zstyle ':vcs_info:git*' check-for-changes true #too slow
289zstyle ':vcs_info:git*:dotfiles' check-for-changes true
290zstyle ':vcs_info:git*' check-for-staged-changes true
291autoload -Uz vcs_info
292
293## compinit
294zstyle ':completion:*' auto-description '[arg] %d'
295zstyle ':completion:*' expand suffix
296zstyle ':completion:*' format '# %d'
297zstyle ':completion:*' group-name ''
298zstyle ':completion:*' ignore-parents parent
299zstyle ':completion:*' insert-unambiguous false
300zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
301zstyle ':completion:*' list-prompt '%B%i%b'
302zstyle ':completion:*' list-suffixes true
303zstyle ':completion:*' matcher-list '' 'm:{[:lower:]}={[:upper:]}' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]} r:|[._-]=* r:|=*' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]} l:|=* r:|=*'
304zstyle ':completion:*' menu select=1
305zstyle ':completion:*' original false
306zstyle ':completion:*' select-prompt '%B%l%b'
307zstyle ':completion:*' verbose true
308autoload -Uz compinit && compinit
309
310### prompt
311autoload -Uz promptinit && promptinit
312prompt arrows
313
8eb81f95 314### load site-specific
315if [[ -f ~/.zshrc.local ]] { source ~/.zshrc.local }
abd1eae8 316
79d4a356 317# vim: et sts=4 sw=4 ts=8 tw=79
This page took 0.121408 seconds and 4 git commands to generate.