-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
dot.zshrc
executable file
·399 lines (312 loc) · 13 KB
/
dot.zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# .zshrc
# sourced only on interactive/TTY
# sourced when you type ZSH
[[ -n "$TMUX" ]] && DKO_SOURCE="${DKO_SOURCE} -> ____TMUX____ {"
export DKO_SOURCE="${DKO_SOURCE} -> .zshrc {"
# export to global and dedupe entries (lowercase are arrays that shadow PATH,
# FPATH, etc). zsh docs recommends setting the flag for both interfaces (i.e.,
# add both PATH var and path array)
typeset -gU cdpath PATH path FPATH fpath MANPATH manpath
. "${DOTFILES}/shell/interactive.sh"
# ============================================================================
# Interactive vars
# ============================================================================
export HISTFILE="${HOME}/.local/zsh_history"
# ============================================================================
# nocorrect aliases
# These may be re-aliased later (e.g. rm=trash from trash-cli node module)
# ============================================================================
alias cp="nocorrect cp"
alias mv="nocorrect mv"
alias rm="nocorrect rm"
alias mkdir="nocorrect mkdir"
# ============================================================================
# zinit
# ============================================================================
# file is required and missing on busybox
__dko_has 'file' && __dko_has 'git' && {
declare -A ZINIT
ZINIT[HOME_DIR]="${XDG_DATA_HOME}/zinit"
ZINIT[COMPINIT_OPTS]=-C;
# part of zinit's install, found by compaudit
mkdir -p "${ZINIT[HOME_DIR]}" && chmod g-rwX "${ZINIT[HOME_DIR]}"
alias unzinit='rm -rf "${ZINIT[HOME_DIR]}"'
function {
local zinit_dest="${ZINIT[HOME_DIR]}/bin"
local zinit_script="${zinit_dest}/zinit.zsh"
. "$zinit_script" 2>/dev/null || {
# install if needed
command git clone https://github.com/zdharma-continuum/zinit "${zinit_dest}" &&
. "$zinit_script"
}
}
}
if __dko_has 'zinit'; then
. "${ZDOTDIR}/zinit.zsh" 2>/dev/null
autoload -Uz _zinit && (( ${+_comps} )) && _comps[zinit]=_zinit
# the last zinit plugin will run zicompinit which inits compinit
alias zup='zini update --parallel'
else
autoload -Uz compinit && compinit
fi
# ============================================================================
# Finish up managed completions
# ============================================================================
compdef g=git
compdef e=nvim
__dko_has 'pipx' && eval "$(register-python-argcomplete pipx)"
# ============================================================================
# Options
# In the order of `man zshoptions`
# ============================================================================
# disable ^S and ^Q terminal freezing
unsetopt flowcontrol
# Changing Directories
setopt AUTO_PUSHD # pushd instead of cd
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_SILENT # hide stack after cd
setopt PUSHD_TO_HOME # go home if no d specified
# Completion
setopt AUTO_LIST # list completions
setopt AUTO_MENU # TABx2 to start a tab complete menu
setopt NO_COMPLETE_ALIASES # no expand aliases before completion
setopt LIST_PACKED # variable column widths
# Expansion and Globbing
setopt EXTENDED_GLOB # like ** for recursive dirs
# History
setopt APPEND_HISTORY # append instead of overwrite file
setopt EXTENDED_HISTORY # extended timestamps
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE # omit from history if space prefixed
setopt HIST_REDUCE_BLANKS
setopt HIST_VERIFY # verify when using history cmds/params
# Initialisation
# Input/Output
setopt ALIASES # autocomplete switches for aliases
setopt AUTO_PARAM_SLASH # append slash if autocompleting a dir
setopt COMBINING_CHARS # unicode allowed when using wezterm
setopt CORRECT
# Job Control
setopt CHECK_JOBS # prompt before exiting shell with bg job
setopt LONGLISTJOBS # display PID when suspending bg as well
setopt NO_HUP # do not kill bg processes
# Prompting
setopt PROMPT_SUBST # allow variables in prompt
# Scripts and Functions
# Shell Emulation
setopt INTERACTIVE_COMMENTS # allow comments in shell
# Shell State
# Zle
setopt NO_BEEP
setopt VI
# ============================================================================
# Modules
# ============================================================================
# color complist
zmodload -i zsh/complist
#autoload -Uz colors; colors
# hooks -- used for prompt too
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
# automatically fix things when pasted, works with url-quote-magic
autoload -Uz bracketed-paste-magic
zle -N bracketed-paste bracketed-paste-magic
# automatically quote URLs as they are typed
autoload -Uz url-quote-magic
zle -N self-insert url-quote-magic
. "${ZDOTDIR}/prompt.zsh"
# ============================================================================
# Keybindings
#
# Find defaults in
# https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html#Zle-Widgets
# \e is the same as ^[ is the escape code for <Esc>
# Prefer ^[ since it mixes better with the letter form [A
#
# `cat -e` - test what the terminal emulator is reporting for a keypress
#
# `bindkey '^w'` - shows what is bound to ^w. If it reports something
# different than is here, it may be coming from .inputrc
#
# Tested on macbook iterm2 and magic keyboard+arch, xterm-256color
# - Need both normal mode and vicmd mode
# ============================================================================
# VI mode, and make -M main === -M viins
bindkey -v
# ----------------------------------------------------------------------------
# Keybindings - Completion with tab
# Cancel and reset prompt with ctrl-c
# ----------------------------------------------------------------------------
# shift-tab to select previous result
bindkey -M menuselect '^[[Z' reverse-menu-complete
# fix prompt (and side-effect of exiting menuselect) on ^C
bindkey -M menuselect '^C' reset-prompt
# ----------------------------------------------------------------------------
# Keybindings - Movement keys
# The 1;3 variants are for wezterm and kitty
# ----------------------------------------------------------------------------
# C-Left
bindkey -M viins '^[[1;5D' vi-backward-word
bindkey -M vicmd '^[[1;5D' vi-backward-word
bindkey -M viins '^b' vi-backward-word
bindkey -M vicmd '^b' vi-backward-word
# C-Right
bindkey -M viins '^[[1;5C' vi-forward-word
bindkey -M vicmd '^[[1;5C' vi-forward-word
# normally it is vi-backward-backward-kill-word
bindkey -M viins '^w' vi-forward-word
bindkey -M vicmd '^w' vi-forward-word
bindkey -M viins '^e' vi-forward-word-end
bindkey -M vicmd '^e' vi-forward-word-end
# Home/Fn-Left
bindkey -M viins '^[[H' vi-beginning-of-line
bindkey -M vicmd '^[[H' vi-beginning-of-line
# End/Fn-Right
bindkey -M viins '^[[F' vi-end-of-line
bindkey -M vicmd '^[[F' vi-end-of-line
# ----------------------------------------------------------------------------
# Keybindings: Editing keys
# ----------------------------------------------------------------------------
# Opt-Left kill left
bindkey -M viins '^[^[[D' vi-backward-kill-word
bindkey -M vicmd '^[^[[D' vi-backward-kill-word
bindkey -M viins '^[[1;3D' vi-backward-kill-word
# Opt-Right kill right
bindkey -M viins '^[^[[C' kill-word
bindkey -M vicmd '^[^[[C' kill-word
bindkey -M viins '^[[1;3C' kill-word
# fix delete - Fn-delete
# Don't bind in vicmd mode
bindkey '^[[3~' delete-char
# Allow using backspace from :normal [A]ppend
bindkey -M viins '^?' backward-delete-char
# ----------------------------------------------------------------------------
# Keybindings: History navigation
# Don't bind in vicmd mode, so I can edit multiline commands properly.
# ----------------------------------------------------------------------------
# Up/Down search history filtered using already entered contents
bindkey '^[[A' history-search-backward
bindkey '^[[B' history-search-forward
# PgUp/Dn navigate through history like regular up/down
bindkey '^[[5~' up-history
bindkey '^[[6~' down-history
# ============================================================================
# FZF keybindings
# ============================================================================
if __dko_has 'fzf'; then
if . "${XDG_CONFIG_HOME}/fzf/fzf.zsh" 2>/dev/null || {
# linux package managers throw it here
. "/usr/share/fzf/completion.zsh" 2>/dev/null
. "/usr/share/fzf/key-bindings.zsh" 2>/dev/null
} || {
# apt / debian
. "/usr/share/doc/fzf/examples/completion.zsh" 2>/dev/null
. "/usr/share/doc/fzf/examples/key-bindings.zsh" 2>/dev/null
}; then
DKO_SOURCE="${DKO_SOURCE} -> fzf"
fi
# <A-b> to open git branch menu and switch to one
# changed from <C-b> since that's my tmux bind
__dkofzfbranch() {
fzf-git-branch
zle accept-line
}
zle -N __dkofzfbranch
bindkey '^[b' __dkofzfbranch
# <A-w> to open git worktree list and cd into one
__dkofzfworktree() {
local wt
wt="$(fzf-git-worktree)"
[ -d "$wt" ] && cd "${wt}"
zle accept-line
}
zle -N __dkofzfworktree
bindkey '^[w' __dkofzfworktree
fi
# ============================================================================
# Completion settings
# Order by * specificity
# ============================================================================
# --------------------------------------------------------------------------
# Completion: Caching
# --------------------------------------------------------------------------
zstyle ':completion:*' use-cache true
zstyle ':completion:*' cache-path "$ZSH_CACHE_DIR"
# --------------------------------------------------------------------------
# Completion: Display
# --------------------------------------------------------------------------
# group all by the description above
zstyle ':completion:*' group-name ''
# colorful completion
#zstyle ':completion:*' list-colors ''
# Updated to respect LS_COLORS
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-dirs-first yes
# go into menu mode on second tab (like current vim wildmenu setting)
# only if there's more than two things to choose from
zstyle ':completion:*' menu select=2
# show descriptions for options
zstyle ':completion:*' verbose yes
# in Bold, specify what type the completion is, e.g. a file or an alias or
# a cmd
zstyle ':completion:*:descriptions' format '%F{black}%B%d%b%f'
# --------------------------------------------------------------------------
# Completion: Matching
# --------------------------------------------------------------------------
# use case-insensitive completion if case-sensitive generated no hits
zstyle ':completion:*' matcher-list \
'm:{[:lower:][:upper:]}={[:upper:][:lower:]}'
# don't complete usernames
zstyle ':completion:*' users ''
# don't autocomplete homedirs
zstyle ':completion::complete:cd:*' tag-order '! users'
# --------------------------------------------------------------------------
# Completion: Output transformation
# --------------------------------------------------------------------------
# expand completions as much as possible on tab
# e.g. start expanding a path up to wherever it can be until error
zstyle ':completion:*' expand yes
# process names
zstyle ':completion:*:processes-names' command \
'ps c -u ${USER} -o command | uniq'
# rsync and SSH use hosts from ~/.ssh/config
[ -r "${HOME}/.ssh/config" ] && {
# Vanilla parsing of config file :)
# @see {@link https://github.com/Eriner/zim/issues/46#issuecomment-219344931}
hosts=($h ${${${(@M)${(f)"$(cat ~/.ssh/config)"}:#Host *}#Host }:#*[*?]*})
#hosts=($(egrep '^Host ' "$HOME/.ssh/config" | grep -v '*' | awk '{print $2}' ))
zstyle ':completion:*:ssh:*' hosts $hosts
zstyle ':completion:*:rsync:*' hosts $hosts
}
# colorful kill command completion -- probably overridden by fzf
zstyle ':completion:*:*:kill:*:processes' list-colors \
"=(#b) #([0-9]#)*=36=31"
# complete .log filenames if redirecting stderr
zstyle ':completion:*:*:-redirect-,2>,*:*' file-patterns '*.log'
# ----------------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------------
# terse zsh-specific up
up() {
local limit=1
local d=""
[[ $1 =~ '^[0-9]+$' ]] && limit=$1
while (( limit-- )); do d="../${d}"; done
cd "$d"
}
# ============================================================================
# version managers
# ============================================================================
# asdf actually added by omz in zinit
(( $+commands[asdf] )) && {
. "${ASDF_DATA_DIR}/plugins/java/set-java-home.zsh" 2>/dev/null
}
# ============================================================================
# Local
# ============================================================================
. "${DOTFILES}/shell/after.sh"
[[ -f "${LDOTDIR}/zshrc" ]] && . "${LDOTDIR}/zshrc"
# ============================================================================
DKO_SOURCE="${DKO_SOURCE} }"
#vim: ft=zsh