-
Notifications
You must be signed in to change notification settings - Fork 4
/
dot_zshrc
288 lines (229 loc) · 9.42 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
# vim:fileencoding=utf-8:foldmethod=marker
# Amar Paul's zshrc
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
export TERM="xterm-256color"
export EDITOR="nvim"
# Load all stock functions (from $fpath files) called below.
autoload -Uz compinit
# plugins: moving to antibody from antigen
ANTIBODY_HOME="$(antibody home)"
DISABLE_AUTO_UPDATE="true"
# for now use omz for better completions: todo remove this BLOAT
export ZSH="$ANTIBODY_HOME"/https-COLON--SLASH--SLASH-github.com-SLASH-robbyrussell-SLASH-oh-my-zsh
export _Z_DATA="$HOME/.cache/z"
# statically load plugins. generate with:
# antibody bundle < ~/.zsh_plugins.txt > ~/.zsh_plugins.zsh
source ~/.zsh_plugins.zsh
# plugin settings: {{{
# atuin: {{{
# previously, i used a hard-coded replacement of the atuin init script to set the following:
# - if something has already been typed ($BUFFER is non-empty), use --search-mode=prefix
# - only if cwd != $HOME, use --filter-mode=directory
# however, i didn't feel like tracking and fixing the init script with each release
# (also, i tend NOT to actually use atuin search all that frequently anyway)
# atuin init zsh -> piped to:
# sed -e '/SC2048/a [[ -n $BUFFER ]] && argv+=--search-mode=prefix'
# sed -e '/SC204/a [[ "$(pwd)" != $HOME ]] && argv+=--filter-mode=directory'
eval "$(ATUIN_NOBIND=true atuin init zsh)"
bindkey '^p' atuin-search
# }}}
# forgit: {{{
# set forgit operations that show a graph to show top-down instead of bottom-up
export FORGIT_LOG_FZF_OPTS="--reverse"
export FORGIT_REBASE_FZF_OPTS="--reverse"
export FORGIT_REVERT_COMMIT_FZF_OPTS="--reverse"
export FORGIT_FIXUP_FZF_OPTS="--reverse"
export FORGIT_CHECKOUT_COMMIT_FZF_OPTS="--reverse"
# order branches in `gcb` by last committed date
export FORGIT_CHECKOUT_BRANCH_BRANCH_GIT_OPTS='--sort=-committerdate'
# temporary, for remembering forgit aliases:
echo_forgit_aliases () {
perl -ne 'print if /``` bash/../^```$/' < $ANTIBODY_HOME/https-COLON--SLASH--SLASH-github.com-SLASH-wfxr-SLASH-forgit/README.md | grep '^forgit'
}
# }}}
# }}}
# remove forward-char, vi-forward-char from accept choices, use as partial instead
export ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=(
end-of-line
vi-end-of-line
vi-add-eol
)
ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS+=(forward-char vi-forward-char)
# User configuration
# PATH changes
[[ -r ~/.profile ]] && source ~/.profile
# shell aliases
[[ -r ~/.shell_aliases ]] && source ~/.shell_aliases
# a few zsh-specific aliases
alias dh='dirs -v'
# filter on bool $f: needs to be expanded inline to work
alias _filter='for f in ${arr[@]}; do if func $f &>/dev/null; then echo $f; fi; done'
# commands for which zsh globbing gets in the way
alias curl='noglob curl'
alias wget='noglob wget'
alias git='noglob git'
# zsh options
# autopushd : make cd act like pushd (alias doesn't work properly)
# pushdminus : use -1 instead of +1
# pushdsilent : prevents printing stack on each cd
# pushdtohome : `pushd` to ~/
# pushdignoredups : dont add duplicates to stack
DIRSTACKSIZE=8
setopt autopushd pushdminus pushdsilent pushdtohome pushdignoredups
# better globs
setopt extendedglob
# allow comments (i.e. ending with '#')
setopt interactivecomments
# History
HISTCONTROL=ignoreboth
HISTFILESIZE=1000000
HISTSIZE=10000000 # number of history lines kept internally
SAVEHIST=10000000 # max number of history lines saved
setopt APPEND_HISTORY # history appends to existing file
setopt HIST_REDUCE_BLANKS # trim multiple insignificant blanks in history
setopt HIST_IGNORE_ALL_DUPS # ignore ALL duplicates (even if not immediately previous)
setopt histignoredups # ignore duplicates during search
# ignore if beginning with space
setopt HIST_IGNORE_SPACE
setopt HIST_NO_STORE
# save time and how long cmd ran
setopt EXTENDED_HISTORY
# zsh: other look-and-feel, configs, aliases
# don't write invalid commands into history (they are still cached locally)
# wip: _also_ ignore failed cmds?
zshaddhistory () {
if whence ${${(z)1}[1]} >| /dev/null; then
# blacklist certain commands from history
if [[ ${${(z)1}[1]} =~ "^(pass|z|clear)$" ]]; then
return 1
elif [[ ${${(z)1}[1]} == "source" ]] && [[ ${${(z)1}[2]} =~ "^[/\"]" ]]; then
return 1
elif [[ ${${(z)1}[1]} == "emulate" ]] && [[ ${${(z)1}[2]} =~ "^/" ]]; then
return 1
elif [[ ${${(z)1}[1]} =~ "^/Users" ]] || [[ ${${(z)1}[1]} =~ "^/home" ]]; then
# don't add any command that uses realpath to a non-system file
return 1
elif [[ ${${(z)1}[1]} == "builtin" ]]; then
return 1
fi
else
return 1
fi
}
# auto-source virtualenvs
chpwd_venv () {
# prefer .venv to venv, but support both
if [[ -r ./.venv/bin/activate ]]; then
source ./.venv/bin/activate
elif [[ -r ./venv/bin/activate ]]; then
source ./venv/bin/activate
fi
}
chpwd_sdk () { [[ -r ./.sdkmanrc ]] && command -v sdk >/dev/null && sdk env }
chpwd () {
chpwd_venv
chpwd_sdk
}
# some custom completions, and make sure $fpath gets loaded properly
fpath+=~/.zsh/completions
fpath+=${HOMEBREW_PREFIX}/share/zsh/site-functions
# case- and hyphen-insensitive matching
zstyle ':completion:*' matcher-list 'm:{a-zA-Z-_}={A-Za-z_-}' 'r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' menu select
zmodload zsh/complist
# load completions after complist is done?
# https://gist.github.com/ctechols/ca1035271ad134841284
# (if we modify fpath ourselves, compinit should be done AFTER)
if [[ -n ~/.zcompdump(#qN.mh+24) ]]; then
compinit -i
else
compinit -C
fi
compdef _zshz ${ZSHZ_CMD:-${_Z_CMD:-z}}
# NOTE: reordering compinit calls seems to have broken how _pass completion is loaded
source ${HOMEBREW_PREFIX}/share/zsh/site-functions/_pass 2>/dev/null
compdef _pass pass
# omz conflicts with common user alias mcd
compdef -d mcd
# completions for zpy (for some reason it they don't get added to fpath...?)
if command -v .zpy_expose_funcs >/dev/null; then
.zpy_expose_funcs
fi
# bindkey mappings, custom functions, and notes
# use ^Xa to expand aliases
# very common commands
# TODO: i want to turn these into widgets that check content of LBUFFER, so that they're only typed on empty lines?
bindkey -s '^[l' 'll\n'
bindkey -s '^[ ' 'clear\n'
bindkey -s '^[h' 'cd ..\n'
bindkey -s '^[^[h' 'bd 2\n'
bindkey -s '^[^[^[h' 'bd 3\n'
bindkey -s '^[^[^[^[h' 'bd 4\n'
bindkey -s '^[k' ' cd -\n'
function __fuzzy-dh () {
set -o pipefail
if ! result="$(dirs -v | fzf --reverse --cycle +m --header='dirs -v' | while read -r item; do echo "$item" | awk '{print $2}'; done)"; then
return 1
fi
# replace ~ with value of $HOME
echo "${result/#\~/$HOME}"
# possible TODO: i actually want to bind <enter> to immediately cd to a given result,
# but i'm not sure how that plays from within a function?
}
bindkey -s '^[^[k' ' _FZF_DIRS_TARGET=$(__fuzzy-dh) && cd ${_FZF_DIRS_TARGET}\n'
bindkey -s '^[g' 'git st\n'
# alt+, mirrors alt+. (insertion of command from last line vs insertion of last arg from last cmd)
# insert-first-word () { zle insert-last-word -- -1 1 }
# zle -N insert-first-word
# bindkey '^[,' insert-first-word
# fix(?) - built-in alt of above (zsh-autosuggestions steamrolls the above version?)
bindkey -s '^[,' '^[0^[.'
# Use ctrl-p/n for up/down arrow (instead of default prev/next cmds)
bindkey "^P" up-line-or-beginning-search
bindkey "^N" down-line-or-beginning-search
# temporarily clear cmd so i can inspect, ls, man, etc
bindkey "^q" push-line-or-edit
# override ^U to also copy to clipboard
copy-and-kill-whole-line () {
printf "%s" "$BUFFER" | pbcopy
zle kill-whole-line
}
zle -N copy-and-kill-whole-line
bindkey "^U" copy-and-kill-whole-line
# shift line by one space (helpful for avoiding history)
bindkey -s "^[>" '^e^a ^e'
# delete space at the beginning of line (helpful after debugging a command)
bindkey -s "^[<" '^a^[[3~^e'
# for hardcore scripting: surround line with $()
bindkey -s "^[o" '^a$(^e)^a'
# save output
bindkey -s "^o" '> output.txt'
# easier echo to a cmd
bindkey -s "^X^G^O" "^aecho '^e'"
# Upon menu completion, enter directories with ctrl-o
bindkey -M menuselect "^o" accept-and-infer-next-history
# use the vi navigation keys in menu completion
bindkey -M menuselect "h" vi-backward-char
bindkey -M menuselect "k" vi-up-line-or-history
bindkey -M menuselect "l" vi-forward-char
bindkey -M menuselect "j" vi-down-line-or-history
# plugin-specific
bindkey "^ " autosuggest-execute
export SHELL=$(which zsh)
# make the pass completion provider faster (should i put this in a different file?)
# Copied the _pass_complete_entries_helper func from the pass' formula's definition here:
# $(brew --prefix)/share/zsh/site-functions/_pass (from pass formula)
_pass_complete_entries_helper () {
local IFS=$'\n'
local prefix
zstyle -s ":completion:${curcontext}:" prefix prefix || prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
# modified from _pass's original one-liner (using `find`)
_values -C 'passwords' ${$(fd . "$prefix" -e gpg -X echo {//}/{/.} | sed -e "s#${prefix}/\{0,1\}##g" -e 's#\\#\\\\#g' -e 's#:#\\:#g' | tr ' ' '\n'):-""}
}
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh