-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzshrc
More file actions
313 lines (251 loc) · 8.52 KB
/
zshrc
File metadata and controls
313 lines (251 loc) · 8.52 KB
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
[ -f ~/.zshrc.before ] && source ~/.zshrc.before
if [[ $FORCE_TMUX == '1' ]] && [[ ! -v TMUX ]] && [[ ! -v NVIM ]]; then
tmux attach || tmux
exit 0
fi
if [ $(uname) = 'Darwin' ]; then
if ! [ -x "$(command -v brew)" ]; then
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
FPATH=$FPATH:$(brew --prefix)/share/zsh/site-functions
fi
# 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
# =============================== zinit start ================================ #
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
[ ! -d $ZINIT_HOME ] && mkdir -p "$(dirname $ZINIT_HOME)"
[ ! -d $ZINIT_HOME/.git ] && git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
source "${ZINIT_HOME}/zinit.zsh"
zinit ice depth=1
zinit light romkatv/powerlevel10k
zinit light zsh-users/zsh-autosuggestions
zinit light zdharma-continuum/fast-syntax-highlighting
zinit light hlissner/zsh-autopair
zinit light zsh-users/zsh-completions
export NVM_COMPLETION=true
export NVM_SYMLINK_CURRENT="true"
zinit wait lucid light-mode for lukechilds/zsh-nvm
zvm_after_init_commands+=('[ -f $HOME/.fzf.zsh ] && source $HOME/.fzf.zsh')
zvm_config() {
ZVM_CURSOR_STYLE_ENABLED=true
ZVM_INSERT_MODE_CURSOR=$ZVM_CURSOR_BLOCK
}
zinit ice depth=1
zinit light jeffreytse/zsh-vi-mode
zinit snippet OMZL::clipboard.zsh
zinit snippet OMZL::completion.zsh
zinit snippet OMZL::history.zsh
zinit snippet OMZP::gitignore
autoload -Uz compinit && compinit
zinit light Aloxaf/fzf-tab
# disable sort when completing `git checkout`
zstyle ':completion:*:git-checkout:*' sort false
# set list-colors to enable filename colorizing
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# force zsh not to show completion menu, which allows fzf-tab to capture the unambiguous prefix
zstyle ':completion:*' menu no
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza --icons --git -l --color=always $realpath'
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'eza --icons --git -l --color=always $realpath'
# ================================ zinit end ================================= #
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# run the command, but won't clear the actual commandline
bindkey '^\' accept-and-hold
# Customize to your needs...
[ -f ~/.shared_profile.zsh ] && source ~/.shared_profile.zsh
# ================================ envs ================================= #
export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple
export PIPENV_PYPI_MIRROR=${PIP_INDEX_URL}
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/
export BAT_THEME='kanagawa'
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
# https://github.com/romkatv/powerlevel10k/issues/524
export GPG_TTY=$TTY
export GOPATH=${HOME}/go
function _prepend_path() {
if [[ -d "$1" ]] && [[ ":${PATH}:" != *":$1:"* ]]; then
PATH="${1}${PATH:+":$PATH"}"
fi
}
function _append_path() {
if [[ -d "$1" ]] && [[ ":${PATH}:" != *":$1:"* ]]; then
PATH="${PATH:+"$PATH:"}$1"
fi
}
_prepend_path "${HOME}/.cargo/bin"
export PYENV_ROOT="$HOME/.pyenv"
_prepend_path "${PYENV_ROOT}/bin"
_prepend_path "${HOME}/.krew/bin"
_prepend_path "${HOME}/.wasme/bin"
_prepend_path "${HOME}/.local/share/bob/nvim-bin"
_prepend_path "${GOPATH}/bin"
export PATH
unfunction _prepend_path
unfunction _append_path
# python
[ -f ~/.startup.py ] && export PYTHONSTARTUP=${HOME}/.startup.py
if [[ -d "${PYENV_ROOT}" ]]; then
eval "$(pyenv init -)"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
fi
# ================================ aliases ================================= #
function _exists() { (( $+commands[$1])) }
_exists eza && alias ls='eza --icons --git'
_exists htop && alias top='htop'
_exists fdfind && alias fd='fdfind'
_exists batcat && alias bat='batcat'
_exists bat && alias cat='bat'
_exists free && alias free='free -h'
_exists less && export PAGER=less
_exists less && alias more='less'
_exists kubectl && alias kubesys='kubectl --namespace kube-system'
_exists ag && alias grep='ag'
_exists rg && alias grep='rg'
_exists curlie && alias curl='curlie'
_exists direnv && export DIRENV_LOG_FORMAT='' && eval "$(direnv hook zsh)"
_exists docker && alias dis='docker images | sort -k7 -h'
_exists neovide && alias vide='neovide'
_exists zoxide && eval "$(zoxide init zsh)"
_exists fzf && [ ! -f $HOME/.fzf.zsh ] && fzf --zsh > ~/.fzf.zsh
alias ll='ls -l'
alias llh='ls -lh'
alias cpwd='pwd | clipcopy'
alias piplist="pip freeze | awk -F'==' '{print \$1}'"
alias genpass="date +%s | sha256sum | base64 | head -c 14"
if _exists nvim; then
export EDITOR=nvim
export VISUAL=nvim
alias vim='nvim'
alias vi='nvim'
fi
function man() {
if [ -t 1 ]; then
# Output is a terminal, use Neovim
MANPAGER='nvim +Man!' command man "$@"
else
MANPAGER='cat' command man "$@"
fi
}
unfunction _exists
# ================================ functions ================================= #
function howto() {
local input
if [ $# -gt 0 ]; then
input="$@"
elif [ ! -t 0 ]; then
# read input from flags or stdin if no tty
input=$(cat -)
else
# prompt user to enter input
echo -n "> "
read input
fi
# escape double quotes
input=${input//\"/\\\"}
# suppressing '[job_id] pid' output
setopt LOCAL_OPTIONS NO_MONITOR NO_NOTIFY
spinner --style dots &
local spinner_pid=$!
# trap SIGINT to handle Ctrl-C
trap 'kill $spinner_pid 2>/dev/null' INT TERM
local output=$(echo $input | llm-cli)
kill $spinner_pid 2>/dev/null
wait
print -z "$output"
}
# commit for me
function cfm() {
# suppressing '[job_id] pid' output
setopt LOCAL_OPTIONS NO_MONITOR NO_NOTIFY
spinner --style dots --suffix " Generating commit message..." &
local spinner_pid=$!
# trap SIGINT to handle Ctrl-C
trap 'kill $spinner_pid 2>/dev/null' INT TERM
local commit_msg
if ! commit_msg=$(git diff --staged | llm-cli --role=commit 2>&1); then
# Stop the spinning animation by killing its process
kill $spinner_pid
wait $spinner_pid 2>/dev/null # Wait for the process to terminate and suppress error messages
echo ''
echo "Failed to generate the commit message: ${commit_msg}"
echo "Set SKIP_LLM_GITHOOK=1 to skip this hook"
exit 1
fi
kill $spinner_pid
wait $spinner_pid 2>/dev/null
git commit -e -m "${commit_msg}" $@
}
function tgo() {
local tgo_path="${HOME}/workspace/tgo"
mkdir -p "${tgo_path}"
# check if the first argument is exists
if [[ -n "${1}" ]]; then
local tmp="$(mktemp -p ${tgo_path} -d "${1}_$(date +%Y%m%d)_XXXXXXXX")"
(
cd ${tmp}
go mod init "$(basename "${tmp}")"
cat > "main.go" << EOF
package main
func main() {
}
EOF
cat > "main_test.go" << EOF
package main_test
import (
"testing"
)
func BenchmarkMain(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
}
}
EOF
nvim -p main.go main_test.go
echo ${tmp}
)
else
local choice=$(find "${tgo_path}" -maxdepth 1 -type d -exec basename {} \; | fzf)
if [[ -z "${choice}" ]]; then
return
fi
(cd "${tgo_path}/${choice}" && \
nvim -p main.go main_test.go)
fi
}
function mkcd() {
mkdir -p "$1" && cd "$1"
}
function zsh-stats() {
fc -l 1 | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n25
}
function tm() {
local name=${1:-${PWD##*/}}
name=${name//./_}
if ! tmux has-session -t "=$name" 2>/dev/null; then
tmux new-session -d -s "$name"
fi
if [[ -n $TMUX ]]; then
tmux switch-client -t "=$name"
else
tmux attach-session -t "=$name"
fi
}
function oc() {
# Find available port
local port=4096
while [ $port -lt 5096 ]; do
if ! lsof -i :$port >/dev/null 2>&1; then
break
fi
port=$((port + 1))
done
export OPENCODE_PORT=$port
opencode --port $port "$@"
}