-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathshrc.ext
613 lines (507 loc) · 21 KB
/
shrc.ext
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# vim:set et ft=sh sw=4 ts=4:
export __MYKEYBOARD=${__MYKEYBOARD:-common}
# [[ -n $__LOADED_SHRC_EXT ]] && echo "[WARNING] shrc.ext already loaded. Stopped." && return
export __LOADED_SHRC_EXT=true
alias pacman='pacman --disable-download-timeout'
alias py=python
alias py3=python3
alias py2=python2
alias ipy=ipython
alias ipy3=ipython3
alias ipy2=ipython2
alias bpy='python -m bpython'
# alias bpy=bpython
# alias bpy3=bpython3
# alias bpy2=bpython2
alias vi=vim
alias nv=nvim
alias mux=tmuxinator
alias toboard='xclip -selection clipboard'
alias xy=proxychains4
# /* others apps */
export EDITOR=nvim
export PAGER='less -F'
export VISUAL=$EDITOR
export SVN_EDITOR=$EDITOR
export PIPX_HOME=/opt/pipx
export PIPX_BIN_DIR=/usr/local/bin
export PIPX_MAN_DIR=/usr/local/share/man
for _tmp_terminal in alacritty xfce4-terminal urxvt; do
export TERMINAL=$(command -v $_tmp_terminal)
[[ -n $TERMINAL ]] && unset _tmp_terminal && break
done
for _tmp_browser in chromium chrome firefox elinks; do
export BROWSER=$(command -v $_tmp_browser)
[[ -n $BROWSER ]] && unset _tmp_browser && break
done
export RIPGREP_CONFIG_PATH=~/.config/ripgreprc
# export CATALINA_HOME=/usr/share/tomcat8 # tomcat
# # /* bat */
# alias bat='bat --theme="GitHub" --style="auto"'
#######################################################################
# appearance #
#######################################################################
export COLORTERM='truecolor'
unset GREP_COLOR
export GREP_COLORS="1;32"
alias neofetch_local='neofetch --w3m ~/Pictures/LOCKSCREENS/kieslowski4-resize.png' # neofetch
# alias grep='grep --color=auto'
alias diff='diff --color=auto'
alias sdcv='sdcv --color ' # stardict
# /* prompt */
# If not running interactively, don't do anything
# PS1='
# \u \t \033[31;1m>\033[0m \w
# \$ '
# colorful man
# man() {
# LESS_TERMCAP_md=$'\e[01;31m' \
# LESS_TERMCAP_me=$'\e[0m' \
# LESS_TERMCAP_se=$'\e[0m' \
# LESS_TERMCAP_so=$'\e[01;44;33m' \
# LESS_TERMCAP_ue=$'\e[0m' \
# LESS_TERMCAP_us=$'\e[01;32m' \
# command man "$@"
# }
# /* less */
# export LESS=-R
# export LESS_TERMCAP_mb=$'\E[1;31m' # begin bold
# export LESS_TERMCAP_md=$'\E[1;36m' # begin blink
# export LESS_TERMCAP_me=$'\E[0m' # reset bold/blink
# export LESS_TERMCAP_so=$'\E[01;44;33m' # begin reverse video
# export LESS_TERMCAP_se=$'\E[0m' # reset reverse video
# export LESS_TERMCAP_us=$'\E[1;32m' # begin underline
# export LESS_TERMCAP_ue=$'\E[0m' # reset underline
#######################################################################
# custom funcs #
#######################################################################
# ex - archive extractor
# usage: ex <file>
ex() {
if [ -f "$1" ]; then
case $1 in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
#######################################################################
# conda #
#######################################################################
__initialize_my_conda() {
local __my_conda_root
local __conda_setup
for p in /opt/miniconda /opt/miniconda3 ${HOME}/.local/miniconda ${HOME}/.local/miniconda3 /opt/homebrew/Caskroom/miniconda/base; do
[[ -d $p ]] && __my_conda_root=$p && break
done
[[ -z $__my_conda_root ]] && return
echo "[conda] Found conda dir: $__my_conda_root . Initializing..."
__conda_setup="$('${__my_conda_root}/bin/conda' 'shell.zsh' 'hook' 2>/dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "${__my_conda_root}/etc/profile.d/conda.sh" ]; then
. "${__my_conda_root}/etc/profile.d/conda.sh"
else
export PATH="${__my_conda_root}/bin:$PATH"
fi
fi
alias workon='conda activate'
}
__initialize_my_conda
#######################################################################
# fzf #
#######################################################################
if command -v fzf >/dev/null; then
if [ -f ~/.fzf.zsh ]; then
source ~/.fzf.zsh
echo "[fzf] Sourced ~/.fzf.zsh"
else
source <(fzf --zsh)
echo "[fzf] Loaded via 'fzf --zsh'"
# TODO (k): <2024-12-05 14:27> fix zsh plugin
fi
# use fd
# export FZF_DEFAULT_COMMAND="fd --type f --hidden --follow --ignore-file ~/.config/search_ignore"
# use the silver searcher
if command -v 'fd' >/dev/null; then
export FZF_DEFAULT_COMMAND="fd --type f --strip-cwd-prefix --hidden --follow --exclude .git"
elif command -v 'rg' >/dev/null; then
export FZF_DEFAULT_COMMAND="rg -. --sort path -l . -g '!.git/'"
elif command -v 'ag' >/dev/null; then
export FZF_DEFAULT_COMMAND="ag --hidden --nocolor -U -g ''"
else
echo "fd/ag needs to be installed"
fi
export FZF_CTRL_T_COMMAND=$FZF_DEFAULT_COMMAND
# for fzf
# export FZF_DEFAULT_OPTS="--preview '[ -f {} ] && file {} && head -30 {} '
# export FZF_DEFAULT_OPTS="--preview 'bat --style=numbers --color=always --line-range :50 {}'"
# --color 'fg:#bbccdd,fg+:#ddeeff,bg:#334455,preview-bg:#223344,border:#778899'
# export FZF_DEFAULT_OPTS="--inline-info --height 50% --reverse --border=horizontal --preview-window=down:50% --color fg:yellow,fg+:bright-yellow"
fzf_opts_basic="--bind 'ctrl-/:toggle-preview'"
export FZF_DEFAULT_OPTS="${fzf_opts_basic} --inline-info --height 50% --reverse --border=horizontal --preview-window=down:50%:hidden --color fg:yellow,fg+:bright-yellow"
# export FZF_CTRL_T_OPTS="${fzf_opts_basic} --preview 'bash -c \"(bat --color=always --style=snip --theme=gruvbox-dark --line-range :40 {} || highlight -O ansi -l {} 2> /dev/null || cat {} || tree -C {}) 2> /dev/null | head -40\"'"
# export FZF_CTRL_R_OPTS="--preview 'echo {}' --preview-window down:3:hidden:wrap --bind '?:toggle-preview'"
export FZF_CTRL_T_OPTS="${fzf_opts_basic} --preview 'sh -c \"(bat --color=always --style=snip --theme=gruvbox-dark --line-range :40 {} || highlight -O ansi -l {} 2> /dev/null || cat {} || tree -C {}) 2> /dev/null | head -40\"'"
export FZF_CTRL_R_OPTS="${fzf_opts_basic} --preview 'echo {} | bat --color=always --style=snip --theme=gruvbox-dark --language=zsh' --preview-window down:3:hidden:wrap"
export FZF_TMUX=1
export FZF_TMUX_OPTS="-p 80%,60%"
# export FZF_COMPLETION_OPTS='--border --info=inline'
# - The first argument to the function ($1) is the base path to start traversal
# - See the source code (completion.{bash,zsh}) for the details.
# _fzf_compgen_path() {
# fd --hidden --follow --exclude ".git" . "$1"
# }
# # Use fd to generate the list for directory completion
# _fzf_compgen_dir() {
# fd --type d --hidden --follow --exclude ".git" . "$1"
# }
# (EXPERIMENTAL) Advanced customization of fzf options via _fzf_comprun function
# - The first argument to the function is the name of the command.
# - You should make sure to pass the rest of the arguments to fzf.
_fzf_comprun() {
local command=$1
shift
case "$command" in
cd) fzf "$@" --preview 'tree -C {} | head -200' ;;
export | unset) fzf "$@" --preview "eval 'echo \$'{}" ;;
ssh) fzf "$@" --preview 'dig {}' ;;
*) fzf "$@" ;;
esac
}
_fzf_complete_pj() {
_fzf_complete --multi --reverse --prompt="pj> " -- "$@" < <(
for bd in $PROJECT_PATHS; do
ls $bd
done
)
}
fi
# --------------------------------------------
# funcs
# --------------------------------------------
# fkill - kill process with fzf
fkill() {
local pid
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]; then
echo "$pid" | xargs kill -${1:-9}
fi
}
__fzf_parse_and_preview_ip() {
local ip infofile
infofile=~/.ssh/fssh/info
ip=$(echo $1 | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' -o)
if [[ -n $ip ]]; then
grep $ip $infofile
else
echo "No info"
fi
}
fssh() {
mkdir -p ~/.ssh/fssh
local cmd
cmd=$( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | sed -E 's/ *[0-9]*\*? *//' | grep '^ssh \+[^ ]\+' | sort -r | uniq | sed -E 's/\\/\\\\/g' |
fzf +s --tac --preview='ip=$(echo {} | grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" -o);
echo "IP: $ip";
if [[ -n $ip ]]; then
grep $ip ~/.ssh/fssh/info
else
echo "No info"
fi
')
if [[ -n $cmd ]]; then
echo "[$cmd]"
eval $cmd
fi
}
fworkon() {
workon $(workon | fzf)
}
# Run command/application and choose paths/files with fzf.
# Always return control of the terminal to user (e.g. when opening GUIs).
# The full command that was used will appear in your history just like any
# other (N.B. to achieve this I write the shell's active history to
# ~/.bash_history)
#
# Usage:
# f cd (hit enter, choose path)
# f cat (hit enter, choose files)
# f vim (hit enter, choose files)
# f vlc (hit enter, choose files)
f() {
# Store the arguments from fzf
IFS=$'\n' arguments=($(fzf --query="$2" --multi))
# If no arguments passed (e.g. if Esc pressed), return to terminal
if [ -z "${arguments}" ]; then
return 1
fi
# We want the command to show up in our bash history, so write the shell's
# active history to ~/.bash_history. Then we'll also add the command from
# fzf, then we'll load it all back into the shell's active history
history -w
# RUN THE COMMANDS ########################################################
# The cd command has no effect when run as background, and doesn't show up
# as a job the can be brought to the foreground. So we make sure not to add
# a '&' (more programs can be added separated by a '|')
if ! [[ $1 =~ ^(cd)$ ]]; then
$1 "${arguments[@]}" &
else
$1 "${arguments[@]}"
fi
# If the program is not on the list of GUIs (e.g. vim, cat, etc.) bring it
# to foreground so we can see the output. Also put cd on this list
# otherwise there will be errors)
if ! [[ $1 =~ ^(cd|zathura|vlc|eog|kolourpaint)$ ]]; then
fg %%
fi
# ADD A REPEATABLE COMMAND TO THE BASH HISTORY ############################
# Store the arguments in a temporary file for sanitising before being
# entered into bash history
: >/tmp/fzf_tmp
for file in ${arguments[@]}; do
echo $file >>/tmp/fzf_tmp
done
# Put all input arguments on one line and sanitise the command such that
# spaces and parentheses are properly escaped. More sanitisation
# substitutions can be added if needed
sed -i 's/\n//g; s/ /\\ /g; s/(/\\(/; s/)/\\)/' /tmp/fzf_tmp
# If the program is on the GUI list add a '&' to the command history
if [[ $1 =~ ^(zathura|vlc|eog|kolourpaint)$ ]]; then
sed -i '${s/$/ \&/}' /tmp/fzf_tmp
fi
# Grab the sanitised arguments
arguments=$(cat /tmp/fzf_tmp)
# Add the command with the sanitised arguments to our .bash_history
echo ${1} ${arguments} >>~/.bash_history
# Reload the ~/.bash_history into the shell's active history
# history -r
# Clean up temporary variables
echo >/tmp/fzf_tmp
}
#######################################################################
# tmux #
#######################################################################
# [ -z "$TMUX" ] && export TERM="xterm-256color"
# attach a default session (create it if not existed)
# and detach other connections bound to it
stmux() {
if [[ -z "$TMUX" ]]; then
local default_tmux_sname=k
tmuxinator $default_tmux_sname
# tmux new -s $default_tmux_sname -d
tmux choose-tree -t $default_tmux_sname -Zs
tmux attach-session -t $default_tmux_sname -d
else
tmux choose-tree -Zs
fi
}
#######################################################################
# for aur #
#######################################################################
export aurpref='https://aur.archlinux.org'
aurdl() {
local url=${aurpref}/${1}.git
echo "url: $url"
git clone $url ~/Downloads/aurdl/$1
}
#######################################################################
# for python #
#######################################################################
export VIRTUAL_ENV_DISABLE_PROMPT=
#######################################################################
# for git #
#######################################################################
alias yolo='git commit -m "$(curl -s https://whatthecommit.com/index.txt)"'
#######################################################################
# for alacritty #
#######################################################################
_init_alarcrity() {
command -v alacritty >/dev/null || return
local _shell=$(ps -ef | grep "^\w\+ \+$$" | sed 's/.* \+//')
[[ $_shell == *zsh* ]] && _shell=zsh
case $_shell in
bash)
local bash_complete_file=~/.config/alacritty/alacritty-completions.bash
if [[ ! -e $bash_complete_file ]]; then
local _url_for_bash=https://raw.githubusercontent.com/jwilm/alacritty/master/alacritty-completions.bash
wget $_url_for_bash -o $bash_complete_file
fi
source $bash_complete_file
;;
esac
}
_init_alarcrity
#######################################################################
# for zsh plugins #
#######################################################################
# pj
export PROJECT_PATHS=(~/Workspace ~/Localworks)
# virtualenv wrapper
export WORKON_HOME=~/.venvs
#######################################################################
# C# #
#######################################################################
if command -v dotnet >/dev/null; then
export PATH=$PATH:~/.dotnet/tools
fi
#######################################################################
# pyenv #
#######################################################################
if [[ -d "$HOME/.pyenv" ]]; then
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PATH:$PYENV_ROOT/bin"
# eval "$(pyenv init -)"
# eval "$(pyenv virtualenv-init -)"
fi
export PYTHON_CONFIGURE_OPTS="--enable-shared"
#######################################################################
# ruby #
#######################################################################
# if [[ -d "$HOME/.gem" ]]; then
# _MY_RUBY_BIN=$(find $HOME/.gem -regex '.*ruby/[^/]+/bin' | sort -r | head -n 1 | tr -d '\r')
# [[ -d $_MY_RUBY_BIN ]] && export PATH=$PATH:$_MY_RUBY_BIN
# fi
if which ruby >/dev/null && which gem >/dev/null; then
export PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi
#######################################################################
# go #
#######################################################################
if command -v go >/dev/null && [[ -z $GOPATH ]]; then
GOPATH=$(go env GOPATH | tr -d '\r')
fi
[[ -n $GOPATH ]] && export PATH=$PATH:$GOPATH/bin
#######################################################################
# rust #
#######################################################################
[[ -d "$HOME/.cargo/bin" ]] && export PATH=$PATH:$HOME/.cargo/bin
#######################################################################
# svn #
#######################################################################
svna() {
while read _f; do
[[ -n "$_f" ]] && svn add $_f
done <<<$(svn status | grep '?' | awk '{print $2}')
while read _f; do
[[ -n "$_f" ]] && svn delete $_f
done <<<$(svn st | grep '^!' | awk '{print $2}')
}
#######################################################################
# axel #
#######################################################################
keep_axel() {
[[ -z "$1" ]] && echo 'Invalid parameter.' && return
while true; do
axel "$1" -o "$2" && break
date
sleep 1
done
}
#######################################################################
# task #
#######################################################################
rt() {
while true; do
clear
date
echo "--------------------------------------------"
task
sleep 7
done
}
#######################################################################
# others #
#######################################################################
# draw a cow at start
command -v chuck_cow >/dev/null && chuck_cow
# load local config
[[ -e ~/.config/shrc.ext.local ]] && source ~/.config/shrc.ext.local && echo "Loaded ~/.config/shrc.ext.local"
alias weather='curl -4 http://wttr.in'
export USE_PROXYCHAINS_FIRST=true
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ff00ff,bg=cyan,bold,underline"
pkg_by_missing() {
local q=$(pacman -F $1)
echo -e "Found:\n${q}\n"
# TODO (k): <2022-03-03> use fzf to choose
if [[ -n "$q" ]]; then
local chosen=$(echo $q | grep '^\w' | head -n 1 | cut -d' ' -f 1)
echo "Installing the first one: $chosen"
sudo pacman -S $chosen --noconfirm
else
echo "No result."
fi
}
#######################################################################
# test #
#######################################################################
emoji-fzf() {
# TODO (k): <2022-03-07> cache
local cache_file=~/.cache/emoji-fzf-cache
if ! [[ -s $cache_file ]]; then
wget 'https://git.io/JXXO7' -O $cache_file
fi
selected_emoji=$(cat $cache_file | fzf)
echo $selected_emoji
}
#######################################################################
# POST #
#######################################################################
# __fix_path() {
# local line
# while read line; do
# done
# }
export PATH="$PATH:$HOME/.local/bin"
export PATH="$(perl -e 'print join(":", grep { not $seen{$_}++ } split(/:/, $ENV{PATH}))')"
#######################################################################
# NVM #
#######################################################################
# if command -v 'nvm' >/dev/null; then
# export NVM_DIR="$HOME/.nvm"
# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# fi
if [[ -f /usr/share/nvm/init-nvm.sh ]]; then
source /usr/share/nvm/init-nvm.sh
echo "[nvm] initialized with init-nvm.sh"
elif [[ -d "$HOME/.nvm" ]]; then
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
echo "[nvm] initialized from $NVM_DIR"
else
echo ":x: nvm init failed"
fi
[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm" && echo "[gvm] loaded"
#######################################################################
# kd #
#######################################################################
# if [[ -n $TMUX ]]; then
# __kdwithtmuxpopup() {
# tmux display-popup "kd $@"
# }
# alias kd=__kdwithtmuxpopup
# fi
#######################################################################
# Wayland #
#######################################################################
export XMODIFIERS="@im=fcitx"
export QT_IM_MODULE=fcitx
#######################################################################
# DEBUG #
#######################################################################
export TERMINFO=/usr/share/terminfo