/
dot_zshrc
309 lines (281 loc) · 8.99 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
### Added by Zinit's installer
if [[ ! -f $HOME/.zinit/bin/zinit.zsh ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}DHARMA%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f"
command mkdir -p "$HOME/.zinit" && command chmod g-rwX "$HOME/.zinit"
command git clone https://github.com/zdharma-continuum/zinit "$HOME/.zinit/bin" && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi
source "$HOME/.zinit/bin/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit
### End of Zinit's installer chunk
# Global profile (shared with bash)
# keep as a comment while testing if .zprofile works
# [[ -f "$HOME/.profile" ]] && . "$HOME/.profile"
# PROMPT
function gitprompt() {
# will be overridden by woefe/git-prompt.zsh
# the blank function here suppresses potential error from lazy loading
:
}
function kubeprompt() {
command kubectl config current-context 2>/dev/null || true
}
ZSH_THEME_GIT_PROMPT_PREFIX=" "
ZSH_THEME_GIT_PROMPT_SUFFIX=""
ZSH_THEME_GIT_PROMPT_UNTRACKED=".."
ZSH_THEME_GIT_PROMPT_UNMERGED="%{$fg[red]%}x"
ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[green]%}o"
ZSH_THEME_GIT_PROMPT_UNSTAGED="%{$fg[red]%}+"
PROMPT_ACCENT_COLOR="$( test -n "$SSH_CLIENT" && echo cyan || echo 112 )"
PROMPT='%F{${PROMPT_ACCENT_COLOR}}[ %3~ ]%F{white}$(gitprompt) %F{239}$(kubeprompt)
%F{${PROMPT_ACCENT_COLOR}}» '
# Load plugins
zinit light-mode for \
zdharma-continuum/zinit-annex-as-monitor \
zdharma-continuum/zinit-annex-bin-gem-node \
zdharma-continuum/zinit-annex-patch-dl \
zdharma-continuum/zinit-annex-rust
zinit wait lucid for \
atinit"ZINIT[COMPINIT_OPTS]=-C; zicompinit; zicdreplay" \
zdharma-continuum/fast-syntax-highlighting \
blockf \
zsh-users/zsh-completions \
atload"!_zsh_autosuggest_start" \
zsh-users/zsh-autosuggestions
zinit ice depth"1" wait lucid
zinit light-mode for \
mafredri/zsh-async \
chrissicool/zsh-256color \
woefe/git-prompt.zsh \
mollifier/cd-gitroot \
yuki-yano/zeno.zsh \
junegunn/fzf \
atusy/gh-fzf
bindkey -e
# History
HISTFILE=~/.zsh_history
HISTSIZE=100000
SAVEHIST=100000
setopt \
hist_ignore_all_dups \
hist_ignore_space \
hist_no_store \
hist_reduce_blanks \
hist_save_no_dups \
hist_verify \
share_history \
inc_append_history
# cd
setopt autocd auto_pushd pushd_ignore_dups
# else
setopt \
extended_glob \
mark_dirs \
interactive_comments \
noautoremoveslash \
nomatch \
notify \
no_beep \
no_flow_control \
no_list_beep \
numeric_glob_sort \
print_eight_bit \
prompt_subst
# completion
LISTMAX=1000
setopt \
always_last_prompt \
auto_list \
auto_menu \
auto_param_keys \
auto_param_slash \
complete_aliases \
complete_in_word \
list_types \
magic_equal_subst \
menu_complete
## zstyle
### Basic Configuration
zstyle ':completion:*' verbose yes
zstyle ':completion:*' menu select interactive
zstyle ':completion:*' group-name ''
zstyle ':completion:*' completer _expand _complete _match _prefix _approximate _list
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' special-dirs true
zstyle ':completion:*' auto-description 'Specify: %d'
zstyle ':completion:*' select-prompt %SScrolling Active: Current selection at %p%s
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
### Highlight
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*:messages' format "$YELLOW" '%d' "$DEFAULT"
zstyle ':completion:*:warnings' format "$RED" 'No matches for:' "$YELLOW" '%d' "$DEFAULT"
zstyle ':completion:*:descriptions' format "$YELLOW" 'Completing %B%d%b' "$DEFAULT"
zstyle ':completion:*:corrections' format "$YELLOW" '%B%d% ' "$RED" '(Errors: %e)%b' "$DEFAULT"
### Separator
zstyle ':completion:*' list-separator ' ==> '
zstyle ':completion:*:manuals' separate-sections true
### Cache
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path "$HOME/.zsh/cache"
### Others
zstyle ':completion:*' remote-access false
zstyle ':completion:*:sudo:*' command-path $path
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
zstyle ':completion::complete:*' use-cache true
# Edit command line with favorite editor
autoload -Uz edit-command-line
function edit-command-line2() EDITOR="${EDITOR_CMD:-$EDITOR}" edit-command-line
zle -N edit-command-line2
bindkey "^x^e" edit-command-line2
# Aliases and some useful functions
alias sudo='sudo '
if command -v zoxide > /dev/null; then
if [[ "$(command -v zi)" =~ "^alias .+" ]]; then
unalias zi
fi
eval "$(zoxide init zsh)"
fi
if command -v htop > /dev/null; then
alias top='htop'
fi
if command -v duf > /dev/null; then
alias df='duf'
else
alias df='df -h'
fi
if command -v procs > /dev/null; then
alias ps='procs'
fi
if command -v gdu > /dev/null; then
alias du='gdu'
fi
if command -v exa > /dev/null; then
alias ls='exa -l --icons --time-style=iso --color=always'
fi
if command -v bat > /dev/null; then
alias cat='bat'
fi
if command -v wget2 > /dev/null
then
alias wget='wget2 -c' # resumable
elif command -v wget > /dev/null; then
alias wget='wget -c' # resumable
fi
if command -v nvim > /dev/null; then
alias vim='nvim'
fi
if command -v xdg-open > /dev/null; then
function open() {
xdg-open "$@" 2>/dev/null
}
fi
if which cd-gitroot 1>/dev/null; then
alias gd='cd-gitroot'
fi
if command -v kubectx > /dev/null; then
function _kubectx() {
command kubectx && export KUBE_CONTEXT="$( command kubectl config current-context || echo "" )"
}
alias kubectx='_kubectx'
fi
if command -v kubectl > /dev/null; then
function _kubectl() {
if [[ -z "${KUBE_CONTEXT:-}" ]]; then
echo "Check the current context and run below running kubectl" 1>&2
echo ': export KUBE_CONTEXT="$( command kubectl current-context )"' 1>&2
return 1
else
local CURRENT_CONTEXT="$( command kubectl config current-context )"
if [[ "$KUBE_CONTEXT" != "$CURRENT_CONTEXT" ]]; then
echo "Current context has been changed to ${CURRENT_CONTEXT} from ${KUBE_CONTEXT}." 1>&2
echo "The change probably comes from other sessions." 1>&2
echo "To continue, reconfigure current context and export the value as KUBE_CONTEXT." 1>&2
echo ': export KUBE_CONTEXT="{{CONTEXT}}"' 1>&2
echo ': command kubectl config set current-context "${KUBE_CONTEXT}"' 1>&2
else
command kubectl "$@"
fi
fi
}
alias kubectl='_kubectl'
fi
if command -v pyenv >/dev/null; then
function pyset() {
local PYSET_IMPLICIT_GLOBAL=${PYSET_IMPLICIT_GLOBAL:-true}
local PYSET_MAX_HEIGHT=${PYSET_MAX_HEIGHT:-10}
local SCOPE="${1:-local}"
local PATTERN="${2:-${PYSET_PATTERN:-^[0-9]+\.}}"
local TARGET="$(
CANDIDATES="$(
pyenv install --list \
| awk '{ print $1 }' \
| command grep -E "$PATTERN"
[[ -f .python-version ]] && echo "$( cat .python-version ) (.python-version)"
)"
HEIGHT="$(
N="$(echo "$CANDIDATES" | wc -l)"
echo "$(( $N + 2 ))\n${PYSET_MAX_HEIGHT}" \
| sort -nu \
| head -n 1
)"
echo "$CANDIDATES" | command fzf --height="$HEIGHT" --tac | awk '{ print $1 }'
)"
if [[ "$TARGET" == "" ]]
then
if ! $PYSET_IMPLICIT_GLOBAL
then
return 1
fi
echo "Using global python..." 1>&2
TARGET="$( python --version | sed -E -e 's/^Python\s+//' )"
fi
pyenv install --skip-existing "$TARGET"
pyenv "$SCOPE" "$TARGET"
pyenv "$SCOPE"
}
fi
function rename_wezterm_title() {
local PWD="$( pwd )"
local GIT_DIR="$( git rev-parse --git-common-dir )"
if [[ -z "$GIT_DIR" ]]; then
local TITLE="$( basename "$PWD" )"
else
local BASE="$( cd "${GIT_DIR}/../.." && pwd )"
local TITLE="$( realpath --relative-to="$BASE" "$PWD" )"
fi
echo "\x1b]1337;SetUserVar=panetitle=$(echo "$TITLE" | base64)\x07"
}
# Plugin: zsh-autosuggestions
ZSH_AUTOSUGGEST_USE_ASYNC=1
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=244'
# Plugin: zeno
if [[ -n $ZENO_LOADED ]]; then
bindkey ' ' zeno-auto-snippet
bindkey '^m' zeno-auto-snippet-and-accept-line
bindkey '^i' zeno-completion
bindkey '^r' zeno-history-selection
bindkey '^x^s' zeno-insert-snippet
bindkey '^x^f' zeno-ghq-cd
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(zeno-auto-snippet-and-accept-line)
fi
# direnv
if command -v direnv >/dev/null; then
eval "$(direnv hook zsh)"
fi
# local configurations
[[ -f "$HOME/.config/zsh/local.zshrc" ]] && . "$HOME/.config/zsh/local.zshrc"
# zcompile {{{
# .zshrc
# if [[ ! -f ~/.zshrc.zwc ]] || [[ "$(readlink ~/.zshrc)" -nt ~/.zshrc.zwc ]]; then
if [[ ! -f ~/.zshrc.zwc ]] || [[ ~/.zshrc -nt ~/.zshrc.zwc ]]; then
zcompile ~/.zshrc
fi
# zinit
if [[ ! -f ~/.zinit/bin/zinit.zsh.zwc ]] || [[ ~/.zinit/bin/zinit.zsh -nt ~/.zinit/bin/zinit.zsh.zwc ]]; then
zcompile ~/.zinit/bin/zinit.zsh
fi
# }}}