forked from you-n-g/deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcfile.sh
More file actions
496 lines (392 loc) · 15.1 KB
/
rcfile.sh
File metadata and controls
496 lines (392 loc) · 15.1 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# # Outlines: Zsh only
# NOTE:
# - This should be included after conda because it leverages some conda-related features.
if [ `basename "$SHELL"` = zsh -o "$0" = '-zsh' ]; then
ZF_CMD=$(cat<<"EOF"
z -l | sort -h -r | awk '{ print $2 }' | fzf --preview="echo {} | xargs ls -lat"
EOF
)
if [ ! -e ~/antigen.zsh ]; then
ln -s ~/.antigen.zsh ~/antigen.zsh
# https://github.com/zsh-users/antigen/issues/743
# NOTE: this is caused by a bug of antigen
fi
# Antigen runtime must be loaded before any `antigen bundle/theme/apply`.
if [ -f ~/.antigen.zsh ]; then
source ~/.antigen.zsh
fi
function zf() {
local dir=$(eval $ZF_CMD)
cd "$dir"
}
function hf() {
# select commands from history
history | grep -v -P '^ *\d+ history|less|ls' | fzf --tac -m | awk '{$1="";print}'
# Ref: https://stackoverflow.com/questions/2626274/print-all-but-the-first-three-columns
}
# antigen use oh-my-zsh
#
# just for highlighting the command which is being typed
# If we place it after autocomplete, it will raise error
antigen bundle zsh-users/zsh-syntax-highlighting
antigen bundle zsh-users/zsh-completions # add more completion options
antigen bundle zsh-users/zsh-autosuggestions
antigen bundle colored-man-pages
antigen bundle rupa/z z.sh
antigen bundle MichaelAquilina/zsh-you-should-use
# antigen bundle mafredri/zsh-async
# TODO: this give error now. But I can't remember why I need this plugin..
# 后来发现 pure 似乎不能用 antigen 安装了
# antigen bundle sindresorhus/pure
# antigen theme denysdovhan/spaceship-prompt
# TODO: 后面对spaceship-prompt 做做加速
# echo $SPACESHIP_PROMPT_ORDER
# NOTE: 这个会导致vim下,出现显示不出来的错误
antigen theme romkatv/powerlevel10k
antigen bundle paoloantinori/hhighlighter
# hhighlighter
# 1) 可以让一些暂时不支持高亮的代码 log 等等信息高亮
# 2) 充当不能筛选内容的grep的作用
# 有用的功能
# C-x C-e 被 vv 替代掉了
antigen bundle jeffreytse/zsh-vi-mode
# NOTE: fzf-tab conflicts with autocomplete
antigen bundle Aloxaf/fzf-tab
# It does not work well with vi-mode.
# antigen bundle marlonrichert/zsh-autocomplete
# It does not work due to permission error
# antigen bundle atuinsh/atuin@main
# antigen bundle zsh-users/zsh-syntax-highlighting
antigen apply
# 后面遇到问题是不是用 zplug可以替代
# DEBUG:
# 1. 如果发现 antigen一直不生效,但是在console中有效,请检查是不是在其他地方改了antigen
# - 我感觉这个错误还会引发 zsh的segment faults: https://github.com/rvm/rvm/issues/4214
# Ref:https://github.com/zsh-users/antigen/issues/297
# 2. 如果发现antigen 改配置后一直不生效, 可以考虑 antigen reset
# export PURE_CMD_MAX_EXEC_TIME=1
export SPACESHIP_TIME_SHOW=true
# export SPACESHIP_DIR_TRUNC=false
export SPACESHIP_PROMPT_ORDER=(time user dir host git package node docker venv conda dotnet exec_time battery jobs exit_code char)
# TODO: vi_mode pyenv can't be displayed correctly now..
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
P10K_CONF=~/deploy/configs/shell/p10k.zsh
[[ ! -f $P10K_CONF ]] || source $P10K_CONF
# shrink path
# export PROMPT='${ret_status} %{$fg[cyan]%}$(shrink_path -l -t) %{$reset_color%}'
# For showing time
# show right prompt with date ONLY when command is executed
# strlen () {
# FOO=$1
# local zero='%([BSUbfksu]|([FB]|){*})'
# LEN=${#${(S%%)FOO//$~zero/}}
# echo $LEN
# }
# 因为pure可以显示运行时间, prompt又加了prompt出现的时间节点,所以敲命令的时间和实际的执行时间都可以推算出来
# FIXME: 这里在tmux + pure主题下时,时间设置会多一行
# preexec () {
# DATE=$( date +"[%H:%M:%S]" )
# local len_right=$( strlen "$DATE" )
# len_right=$(( $len_right+1 ))
# local right_start=$(($COLUMNS - $len_right))
#
# local len_cmd=$( strlen "$@" )
# local len_prompt=$(strlen "$PROMPT" )
# local len_left=$(($len_cmd+$len_prompt))
#
# RDATE="\033[${right_start}C ${DATE}"
#
# if [ $len_left -lt $right_start ]; then
# # command does not overwrite right prompt
# # ok to move up one line
# echo -e "\033[1A${RDATE}"
# else
# echo -e "${RDATE}"
# fi
# }
# https://stackoverflow.com/a/26585789
# spaceship-prompt 可以直接显示时间
# export PROMPT="[%D{%H:%M:%S}] $PROMPT"
ZVM_VI_SURROUND_BINDKEY="s-prefix"
# NOTE:
# zsh vim mode 常常会和其他的插件起冲突(启用后会覆盖其他插件)
# 所以很多插件需要 后面再补一发启动
function zvm_after_init() {
# NOTE: fzf-tab conflicts with autocomplete
# fzf key-bindings are sourced later (after FZF_* env vars are set),
# otherwise fzf widgets may not be registered.
enable-fzf-tab
# 有时候光有 zvm_after_lazy_keybindings 似乎也不work
zvm_bindkey viins '^S^L' insert-last-word
}
function zvm_after_lazy_keybindings() {
# 这个按键绑定在 zvm 上不work
# bindkey -M viins '\e.' insert-last-word
# - 一般的shell中的就是 ESC + .
zvm_bindkey viins '^S^L' insert-last-word
}
# Allow autosuggestions to partially accept with forward-word
export ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(forward-word)
# Make sure forward-word is bound correctly in vi insert mode
# If I use ^J, it will incur a bug when sending content to zsh
bindkey -M viins '^L' forward-word
bindkey -M emacs '^L' forward-word
# for autocomplete
# 启用强力模糊匹配和大小写忽略
# 背景:解决当同时存在 GEMINI.md 和 gemini_history.md 时,输入 GEM 无法正确补全的问题。
# 如果不开启此设置,zsh 可能会因大小写敏感或匹配逻辑问题导致补全失败(如错误地补全为不存在的 GEMini)。
# 1. m:{a-z}={A-Z} -> 忽略大小写,使 gemini 能匹配 GEMINI
# 2. r:|[._-]=* r:|=* -> 允许在标点符号后进行模糊匹配
# 3. l:|=* r:|=* -> 允许任意位置模糊匹配
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*:*:-command-:*:*' matcher-list 'm:{a-z}={A-Z}' 'r:|[._-_-]=* r:|=*' 'l:|=* r:|=*'
source <(fzf --zsh)
fi
# # Outlines: Bash Only
# # Outlines: Common config
alias gitlog="git log --all --oneline --graph --decorate"
alias gitlogs="git log --all --pretty=short --abbrev-commit --graph --decorate"
alias mux=tmuxinator
alias mx=tmux
export PATH="$HOME/deploy/helper_scripts/bin/:$HOME/bin/:$HOME/apps/nodejs/bin/:$HOME/.luarocks/bin/:$PATH"
export PATH="$PATH:$HOME/.local/bin" # this is for pipx
export MANPATH="$HOME/.local/share/man/:$MANPATH"
export EDITOR=`which vim`
# sudo -E will keep the environment when run sudo. Many env variables like http_proxy need it.
alias sudo="sudo -E"
# export BETTER_EXCEPTIONS=1
# export LD_LIBRARY_PATH=/home/xiaoyang/lib/glibc/glibc-2.28-install/lib/:$LD_LIBRARY_PATH
function proxy_up() {
# don't capitalize them
export http_proxy=${proxy_addr:-127.0.0.1:6489}
export https_proxy=${proxy_addr:-127.0.0.1:6489}
export SOCKS_SERVER=${proxy_addr:-127.0.0.1:8964}
# NOTICE: the ip range my not works on some softwares !!!!!
export no_proxy=localhost,127.0.0.1,127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,.sock
# To make it works for all softwares, proxychains is recommended
# - socks4 should be used for `ssh -D` proxy.
# - I don't know why it doesn't work for tsocks when using specific software like thriftpy2(in jkdatasdk)
}
function proxy_down() {
unset http_proxy https_proxy SOCKS_SERVER no_proxy
}
# NOTE: add a function to check if we need proxy by testing connectivity
function proxy_check() {
local test_url=${1:-https://x.com}
# timeout 3 seconds; if failed, we may need proxy
if curl -Is --max-time 3 "$test_url" >/dev/null 2>&1; then
echo "Network OK. Proxy not needed."
return 0
else
echo "Network unreachable. Proxy may be needed."
return 1
fi
}
alias pypdb='python -m ipdb -c c'
alias pyprof='python -m cProfile -o stats_out'
alias ipify="curl cip.cc"
# using this alias name because the previous tool is ipify
# gemini with proxy (p) — auto-detect: if proxy needed, wrap with proxychains, else run normally
function myp() {
if proxy_check >/dev/null 2>&1; then
"$@"
else
proxychains -q "$@"
fi
}
# gemini with (r)ename — unified runner
function _with_tmux_rename() {
local title="$1"
shift
if [ -n "$TMUX" ]; then
local prev_name
prev_name=$(tmux display-message -p "#W")
tmux rename-window -t "$TMUX_PANE" "$title"
myp "$@"
tmux rename-window -t "$TMUX_PANE" "$prev_name"
else
myp "$@"
fi
}
# gemini with rename
function geminir() {
_with_tmux_rename gemini gemini "$@"
}
# codex with rename
function codexr() {
# _with_tmux_rename codex codex --dangerously-bypass-approvals-and-sandbox "$@"
AZURE_OPENAI_API_KEY=$(get-cred key gpt.gpg) _with_tmux_rename codex codex "$@"
}
# for fzf
# 文件太大常常没法正常运行
export FZF_ALT_C_COMMAND="command find -L . -maxdepth 1 \\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type d -print 2> /dev/null | cut -b3-"
# We don't want to follow the link. This usually consumes a lot of time
export FZF_CTRL_T_COMMAND="command find . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' -o -path '*/mlruns/*' -o -path '*/__pycache__' -o -name '*.pyc' \\) -prune \
-o -type f -print \
-o -type d -print \
-o -type l -print 2> /dev/null | cut -b3-"
export FZF_DEFAULT_COMMAND='fd --type f -L'
# for rvm and tmuxinator
# 优先使用个人账户下的rvm
if [ -e $HOME/.rvm/scripts/rvm ]; then
source $HOME/.rvm/scripts/rvm
fi
# ## Outlines: color
# enable color support of ls and also add handy aliases
if command -v dircolors >/dev/null 2>&1; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
fi
# Prefer GNU ls flags when available; fall back to BSD/macOS `-G`.
if ls --color=auto . >/dev/null 2>&1; then
alias ls='ls --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
elif ls -G . >/dev/null 2>&1; then
export CLICOLOR=1
export LSCOLORS="${LSCOLORS:-ExFxBxDxCxegedabagacad}"
alias ls='ls -G'
fi
if grep --color=auto "" </dev/null >/dev/null 2>&1; then
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# # Outlines: envs
export GOROOT="$HOME/apps/go/"
export PATH="$GOROOT/bin/:$PATH"
# config for go, vim-go依赖这一步
export GOPATH="$HOME/gopath/"
export PATH="$GOPATH/bin/:$PATH"
# If the / is full, some command like pip install may fail. So we change the tmpdir to home directly
export TMPDIR=~/tmp/
export GPG_TTY=$(tty) # for microsoft pull to use gpg to save
# # Outlines: tools
function tfts() {
# 只有用这个trick才能保证最后一行能被立马读出来
# 但是这个性能非常慢
# pip install rainbow
# tail -f -n 20 $1 | ts '[%Y-%m-%d %H:%M:%S]' | while read _line
# do
# # stdbuf -i0 -o0 -e0 echo "$_line"
# echo "$_line" #| rainbow -f $2
# echo 'xixihaha'
# done | rainbow -f $2 | sed "/xixihaha/d"
# 这个是无法支持实时数据流的
# tail -f -n 20 $1 | awk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'
LINE_N=${2:-100}
tail -f -n $LINE_N $1 | ts '[%Y-%m-%d %H:%M:%S]'
# 不需要协程下面的样子的
# tail -f -n 20 $1 | ts '[%Y-%m-%d %H:%M:%S]' | while read _line
# do
# echo "$_line"
# done
# 最后用的方法是 zsh 的插件h
}
alias black="uvx black"
alias wan="uvx --from wanot wan"
alias yapf="uvx yapf"
alias pre-commit="uvx pre-commit"
alias copier="uvx copier"
# ## Outlines: ranger
# ranger的安装依赖 deploy_apps/install_fav_py_pack.sh
alias .r=". ranger"
alias ranger='if [ -n "$TMUX" ]; then prev_name=$(tmux display-message -p "#W"); tmux rename-window -t "$TMUX_PANE" ranger; command uvx --from ranger-fm ranger ; tmux rename-window -t "$TMUX_PANE" "$prev_name"; else command ranger; fi' # if inside tmux, rename to 'ranger', run it, then restore name
# 其他
# -快捷键篇
# - r: 可以open_with调用当前文件,1是less/pager
# - 这个的好处是有时候vim比较慢,用其他工具就不会那么卡
# - 坑篇
# - 如果 发现 dd pp 无法剪切文件(但是能复制文件), 可能是权限 (这里的ranger是不会报错的)
# ## Outlines: conda
# Support tmux inherit the conda env
# this is not necessary, tmux will set the environment automatically
# env_expr=$(tmux show-environment conda_env 2> /dev/null)
# if [ $? -eq 0 -a "$env_expr" != "-conda_env" ]; then
# eval "export $env_expr"
# fi
# Conda
if [ -n "$conda_env" -a "$conda_env" != "base" ]; then
conda activate $conda_env
fi
function yxca() {
conda activate $1
export conda_env=$1
tmux setenv conda_env $1
}
function yxcd() {
conda deactivate
unset conda_env
tmux setenv -r conda_env
}
# pip env
if [ "$pip_env" = "1" ]; then
pipenv shell
fi
function pipenva() {
tmux setenv pip_env 1 # this must come before
pipenv shell
}
function pipenvd() {
tmux setenv -r pip_env
exit
}
# pdm env
if [ "$pdm_env" = "1" ]; then
`pdm venv activate`
fi
function pdma() {
tmux setenv pdm_env 1 # this must come before
`pdm venv activate`
}
function pdmd() {
tmux setenv -r pdm_env
echo "Please restart the shell to deactivate the pdm environment"
}
# venv, for supporting UV.
if [ -n "$venv_path" ]; then
source $venv_path
fi
function venva() {
venv_path="${1:-./.venv/bin/activate}"
if [ -e $venv_path ]
then
venv_path=$(realpath $venv_path)
tmux setenv venv_path $venv_path
source $venv_path
else
echo "venv not found"
fi
}
function venvd() {
tmux setenv -r venv_path
deactivate
}
function deploy_quant_libs() {
mkdir -p libs/
cd libs
pip install numpy
pip install --upgrade cython
git clone https://github.com/microsoft/qlib.git
cd qlib/
pip install -e '.[dev]'
git clone https://you-n-g@dev.azure.com/you-n-g/qutils/_git/qutils
cd qutils/
pip install -e .
}
# # Outlines: For apps
export AIDER_CHECK_UPDATE=False
export AIDER_SHOW_RELEASE_NOTES=False
export AIDER_GITIGNORE=False
# pet search
# export LANG=en_US.UTF-8
# export LC_ALL=en_US.UTF-8
# export LANGUAGE=en_US:en
# export TERM=xterm-256color
# # Outlines: 准备删掉的
# ## Outlines: nnn
export NNN_OPENER=~/apps/nnn/plugins/nuke