-
Notifications
You must be signed in to change notification settings - Fork 8
/
zshrc
executable file
·1664 lines (1526 loc) · 62 KB
/
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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env zsh
# Determine CPU type
CPUTYPE=${CPUTYPE:-$(uname -m)}
# Set alias for tmux on ARM-based Macs
[[ ${OSTYPE} == "darwin"* && ${CPUTYPE} == "arm"* ]] && alias tmux=/opt/homebrew/bin/tmux
# Check if tmux is installed
if type tmux >/dev/null 2>&1; then
# If not inside a tmux session
if [ -z $TMUX ]; then
echo "welcome to tmux"
USER="$(whoami)"
HOST="$(hostname)"
TMUX_TMPDIR_PREFIX="/tmp/tmux-sockets"
TMUX_TMPDIR="$TMUX_TMPDIR_PREFIX/$HOST"
export TMUX_PLUGIN_MANAGER_PATH="$HOME/.tmux/plugins"
TPM_PATH="$TMUX_PLUGIN_MANAGER_PATH/tpm"
if [ ! -d $TPM_PATH ]; then
git clone --depth 1 --recursive https://github.com/tmux-plugins/tpm $TPM_PATH
fi
# If connected via SSH
if [ ! -z "$SSH_CLIENT" ]; then
SSH_IP="${SSH_CLIENT%% *}"
TMUX_TMPDIR="$TMUX_TMPDIR_PREFIX/ssh-from-$SSH_IP"
echo "starting tmux for ssh $SSH_TTY from $SSH_CLIENT"
fi
export TMUX_TMPDIR=$TMUX_TMPDIR
# Create tmux temp directory if it doesn't exist
if mkdir -p $TMUX_TMPDIR; then
echo "Successfully created tmux temp directory on $TMUX_TMPDIR."
else
echo "Failed to create tmux temp directory on $TMUX_TMPDIR."
exit 1 # Exit if failed to create directory
fi
TMUX_SESSIONS=$(tmux ls 2>/dev/null) # Check for existing tmux sessions on the specified socket directory
if [ $? -ne 0 ]; then # Check for error from tmux command
if [ -f /.dockerenv ]; then # Docker specific settings
group=$(id -g)
# Ensure the user has access to the Docker socket
sudo chown -R $USER:$group /var/run/docker.sock
fi
echo "creating new tmux session at $TMUX_TMPDIR"
if TMUX_TMPDIR=$TMUX_TMPDIR tmux -2 new-session -n$USER -s$USER@$HOST; then
echo "created new tmux session for $TMUX_TMPDIR:$USER@$HOST"
else
echo "failed to create new tmux session for $TMUX_TMPDIR:$USER@$HOST"
exit 1 # Exit if failed to create tmux session
fi
else
SESSION_NAME="$(tmux ls | cut -d: -f1 | head -n 1)" # get the name of a session
if [ -z "$SESSION_NAME" ]; then
echo "No sessions found in $USER@$HOST, global tmux ls = $(tmux ls)"
exit 1 # Exit if no sessions found
fi
echo "attaching tmux session $SESSION_NAME at $TMUX_TMPDIR"
# Attach to an existing tmux session
if TMUX_TMPDIR=$TMUX_TMPDIR tmux -2 attach-session -t "$SESSION_NAME"; then
echo "attached tmux session $SESSION_NAME"
else
echo "failed to attach tmux session for $SESSION_NAME"
exit 1 # Exit if failed to attach tmux session
fi
fi
exit
fi
fi
if [ -z $DOTENV_LOADED ]; then
if type fastfetch >/dev/null 2>&1; then
fastfetch
elif type neofetch >/dev/null 2>&1; then
neofetch
fi
stty stop undef
stty start undef
setopt no_global_rcs
if [ -x /usr/libexec/path_helper ]; then
PATH=""
[ -z "$_lazy_path_helper" ] && {
eval "$(/usr/libexec/path_helper -s)"
_lazy_path_helper=1
}
fi
# environment var
export CHARSET=UTF-8
export LESSCHARSET=${CHARSET}
export XLANGCCUS=en_US
export XLANGCCJP=ja_JP
export LANG=${XLANGCCUS}.${CHARSET}
export LANGUAGE=${XLANGCCUS}:${XLANGCCJP}
export LC_ADDRESS="${XLANGCCUS}.${CHARSET}"
export LC_ALL=${XLANGCCUS}.${CHARSET}
export LC_COLLATE="${XLANGCCUS}.${CHARSET}"
export LC_CTYPE=${CHARSET}
export LC_IDENTIFICATION="${XLANGCCUS}.${CHARSET}"
export LC_MEASUREMENT="${XLANGCCUS}.${CHARSET}"
export LC_MESSAGES="${XLANGCCUS}.${CHARSET}"
export LC_MONETARY="${XLANGCCUS}.${CHARSET}"
export LC_NAME="${XLANGCCUS}.${CHARSET}"
export LC_NUMERIC="${XLANGCCUS}.${CHARSET}"
export LC_PAPER="${XLANGCCUS}.${CHARSET}"
export LC_TELEPHONE="${XLANGCCUS}.${CHARSET}"
export LC_TIME=${XLANGCCJP}.${CHARSET}
export MANLANG=${XLANGCCJP}.${CHARSET}
[ -z "$_lazy_fzf_zsh" ] && {
[ -f $HOME/.fzf.zsh ] && source $HOME/.fzf.zsh
_lazy_fzf_zsh=1
}
export SHELL=$(which zsh)
export USER=$(whoami)
export GIT_USER=kpango
if [[ ${OSTYPE} == "darwin"* && ${CPUTYPE} == "arm"* ]]; then
export PATH="/opt/homebrew/bin:$PATH"
fi
if type nproc >/dev/null 2>&1; then
export CPUCORES="$(nproc)"
else
export CPUCORES="$(getconf _NPROCESSORS_ONLN)"
fi
if type alacritty >/dev/null 2>&1; then
export TERMCMD="WINIT_UNIX_BACKEND=x11 alacritty -e $SHELL -c tmux -S /tmp/tmux.sock -q has-session && exec tmux -S /tmp/tmux.sock -2 attach-session -d || exec tmux -S /tmp/tmux.sock -2 new-session -n$USER -s$USER@$HOST"
elif type urxvtc >/dev/null 2>&1; then
export TERMCMD="urxvtc -e $SHELL -c tmux -q has-session && exec tmux -S /tmp/tmux.sock -2 attach-session -d || exec tmux -S /tmp/tmux.sock -2 new-session -n$USER -s$USER@$HOST"
fi
export XDG_CONFIG_HOME=$HOME/.config
export XDG_DATA_HOME=$HOME/.data
if type gcloud >/dev/null 2>&1; then
if [ -d /usr/local/lib/google-cloud-sdk ]; then
export GCLOUD_PATH="/usr/lib/google-cloud-sdk"
fi
export USE_GKE_GCLOUD_AUTH_PLUGIN=True
fi
if type php >/dev/null 2>&1; then
export PHP_BUILD_CONFIGURE_OPTS="--with-openssl=/usr/local/opt/openssl"
fi
if type python3 >/dev/null 2>&1; then
export PYTHON_CONFIGURE_OPTS="--enable-shared"
export PYTHONIOENCODING="utf-8"
fi
if type nvim >/dev/null 2>&1; then
export VIM=$(which nvim)
case ${OSTYPE} in
darwin*)
if [ -d /opt/homebrew/Cellar/neovim/*/share/nvim/runtime ]; then
export VIMRUNTIME="/opt/homebrew/Cellar/neovim/*/share/nvim/runtime"
fi
;;
linux*)
if [ -d /usr/share/nvim/runtime ]; then
export VIMRUNTIME="/usr/share/nvim/runtime"
fi
;;
esac
export NVIM_HOME=$XDG_CONFIG_HOME/nvim
export NVIM_LOG_FILE_PATH=$XDG_DATA_HOME/log
export NVIM_TUI_ENABLE_TRUE_COLOR=1
export NVIM_PYTHON_LOG_LEVEL=WARNING
export NVIM_PYTHON_LOG_FILE=$NVIM_LOG_FILE_PATH/nvim.log
elif type vim >/dev/null 2>&1; then
export VIM=$(which vim)
case ${OSTYPE} in
darwin*)
if [ -d /opt/homebrew/Cellar/neovim/*/share/nvim/runtime ]; then
export VIMRUNTIME="/opt/homebrew/Cellar/neovim/*/share/nvim/runtime"
fi
;;
linux*)
if [ -d /usr/share/nvim/runtime ]; then
export VIMRUNTIME="/usr/share/nvim/runtime"
fi
;;
esac
else
export VIM=$(which vi)
fi
export EDITOR=$VIM
export VISUAL=$VIM
export PAGER=$(which less)
export SUDO_EDITOR=$EDITOR
#ReactNative
export REACT_EDITOR=$EDITOR
export LD_LIBRARY_PATH=/lib:/usr/local/lib:${GCLOUD_PATH}/lib:/opt/containerd/lib:/opt/cuda/lib:${LD_LIBRARY_PATH}
if [ -d "/usr/local/lib/rust" ]; then
export RUST_HOME="/usr/local/lib/rust"
fi
export CARGO_HOME=$RUST_HOME/cargo
export RUSTUP_HOME=$RUST_HOME/rustup
if type go >/dev/null 2>&1; then
#GO
export GOPATH=$HOME/go
export GOROOT="$(go env GOROOT)"
export GOOS="$(go env GOOS)"
export GOARCH="$(go env GOARCH)"
export CGO_ENABLED=1
export GO111MODULE=on
export GOBIN=$GOPATH/bin
export GO15VENDOREXPERIMENT=1
export GOPRIVATE="*.yahoo.co.jp,github.com/vdaas/vald,github.com/vdaas/vald/apis,github.com/vdaas/vald-client-go"
export NVIM_GO_LOG_FILE=$XDG_DATA_HOME/go
# export CGO_LDFLAGS="-g -flto -march=native -fno-plt -Wl,-Ofast,--sort-common,--as-needed,-z,relro,-z,now -fdata-sections -ffunction-sections -Wl,--gc-sections -fvisibility=hidden"
# export CGO_CFLAGS=$CGO_LDFLAGS
fi
if type clang >/dev/null 2>&1; then
export CC=$(which clang)
export CXX=$(which clang++)
export CPP="$CXX -E"
export LD=/usr/bin/ldd
if type llvm-config >/dev/null 2>&1; then
export LD_LIBRARY_PATH=$(llvm-config --libdir):$LD_LIBRARY_PATH
export LLVM_CONFIG_PATH=$(which llvm-config)
else
export LD_LIBRARY_PATH=/usr/lib/clang/*/lib:$LD_LIBRARY_PATH
fi
# export LDFLAGS="-g -flto -march=native -fno-plt -Wl,-Ofast,--sort-common,--as-needed,-z,relro,-z,now -fdata-sections -ffunction-sections -Wl,--gc-sections -fvisibility=hidden -L$LLVM_HOME/lib:-L$QT_HOME/lib:-L/usr/local/opt/openssl/lib:-L/usr/local/opt/bison/lib:$LDFLAGS"
# export FFLAGS=$LDFLAGS
#CLANG
export CFLAGS=-I$LLVM_HOME/include:-I$QT_HOME/include:-I/usr/local/opt/openssl/include:$CFLAGS
export CPPFLAGS=$CFLAGS
export C_INCLUDE_PATH=$LLVM_HOME/include:$QT_HOME/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=$LLVM_HOME/include:$QT_HOME/include:$CPLUS_INCLUDE_PATH
fi
DOTFILE_URL="github.com/$GIT_USER/dotfiles"
if type ghq >/dev/null 2>&1; then
export DOTFILES_DIR="$(ghq root)/$DOTFILE_URL"
elif [ -d "$HOME/go/src/$DOTFILE_URL" ]; then
export DOTFILES_DIR="$HOME/go/src/$DOTFILE_URL"
else
export DOTFILES_DIR="$HOME/dotfiles"
fi
if [ -z $TMUX ]; then
export TERM="xterm-256color"
else
export TERM="tmux-256color"
fi
export LIBRARY_PATH=/lib:/usr/local/lib:${GCLOUD_PATH}/lib:/opt/containerd/lib:/opt/cuda/lib:${LD_LIBRARY_PATH}
export PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/share/npm/bin:/usr/local/go/bin:/opt/local/bin:$GOBIN:$HOME/.local/bin:$CARGO_HOME/bin:$GCLOUD_PATH/bin:/usr/lib/docker/cli-plugins/:$PATH"
if type deno >/dev/null 2>&1; then
export PATH="$(which deno):$PATH"
fi
if [ -d "$HOME/.rd/bin" ]; then
export PATH="$HOME/.rd/bin:$PATH"
fi
# for teleplesence disabling send analytics data anonymously
export SCOUT_DISABLE=1
export DOTENV_LOADED=1
fi
if type gpg >/dev/null 2>&1; then
export GPG_TTY=$(tty)
# export PINENTRY_USER_DATA="USE_CURSES=1"
fi
if [ ! -f "$HOME/.zshrc.zwc" -o "$HOME/.zshrc" -nt "$HOME/.zshrc.zwc" ]; then
zcompile $HOME/.zshrc
fi
if [ ! -f "$HOME/.zcompdump.zwc" -o "$HOME/.zcompdump" -nt "$HOME/.zcompdump.zwc" ]; then
zcompile $HOME/.zcompdump
fi
if [ -z $ZSH_LOADED ]; then
if type sheldon >/dev/null 2>&1; then
[ -z "$_lazy_sheldon" ] && {
eval "$(sheldon source)"
_lazy_sheldon=1
}
fi
# 色を使用出来るようにする
autoload -Uz colors
colors
# ヒストリの設定
HISTFILE=$HOME/.zsh_history
HISTSIZE=1000000
SAVEHIST=1000000
setopt APPEND_HISTORY
setopt SHARE_HISTORY
setopt hist_ignore_all_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
setopt hist_save_no_dups
LISTMAX=1000
WORDCHARS="$WORDCHARS|:"
# export PROMPT_COMMAND='hcmd=$(history 1); hcmd="${hcmd# *[0-9]* }"; if [[ ${hcmd%% *} == "cd" ]]; then pwd=$OLDPWD; else pwd=$PWD; fi; hcmd=$(echo -e "cd $pwd && $hcmd"); history -s "$hcmd"'
# プロンプト
# PROMPT="%F{045}%/ $ %f"
# PS1="%{${fg[green]}%}%/#%{${reset_color}%} %"
# 単語の区切り文字を指定する
autoload -Uz select-word-style
select-word-style default
# ここで指定した文字は単語区切りとみなされる
# / も区切りと扱うので、^W でディレクトリ1つ分を削除できる
zstyle ':zle:*' word-chars " /=;@:{},|"
zstyle ':zle:*' word-style unspecified
########################################
# 補完
# 補完機能を有効にする
autoload -Uz compinit -C && compinit -C
zstyle ':completion:*' format '%B%d%b'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' ignore-parents parent pwd ..
zstyle ':completion:*' keep-prefix
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' menu select
zstyle ':completion:*' squeeze-slashes true
zstyle ':completion:*' verbose yes
zstyle ':completion:*:(nano|vim|nvim|vi|emacs|e):*' ignored-patterns '*.(wav|mp3|flac|ogg|mp4|avi|mkv|webm|iso|dmg|so|o|a|bin|exe|dll|pcap|7z|zip|tar|gz|bz2|rar|deb|pkg|gzip|pdf|mobi|epub|png|jpeg|jpg|gif)'
zstyle ':completion:*:(rm|kill|diff):*' ignore-line other
zstyle ':completion:*:*:*:*:*' menu select
zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
zstyle ':completion:*:*:cd:*' tag-order local-directories directory-stack path-directories
zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
zstyle ':completion:*:-tilde-:*' group-order 'named-directories' 'path-directories' 'expand'
zstyle ':completion:*:corrections' format ' %F{green}-- %d (errors: %e) --%f'
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
# zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*:default' menu select=1
zstyle ':completion:*:descriptions' format ' %F{yellow}-- %d --%f'
# zstyle ':completion:*:functions' ignored-patterns '(_*|pre(cmd|exec)|prompt_*)'
zstyle ':completion:*:history-words' list false
zstyle ':completion:*:history-words' menu yes
zstyle ':completion:*:history-words' remove-all-dups yes
zstyle ':completion:*:history-words' stop yes
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.(^1*)' insert-sections true
zstyle ':completion:*:matches' group 'yes'
zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
zstyle ':completion:*:options' auto-description '%d'
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:processes' command 'ps x -o pid, s, args'
zstyle ':completion:*:rm:*' file-patterns '*:all-files'
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/X11R6/bin
zstyle ':completion:*:warnings' format ' %F{red}-- no matches found --%f'
zstyle ':completion::complete:*' cache-path "${ZDOTDIR:-${HOME}}/.zcompcache"
zstyle ':completion::complete:*' use-cache on
# zstyle ':zsh-kubectl-prompt:' separator ' | ns: '
# zstyle ':zsh-kubectl-prompt:' preprompt 'ctx: '
# zstyle ':zsh-kubectl-prompt:' postprompt ''
########################################
# vcs_info
autoload -Uz vcs_info
autoload -Uz add-zsh-hook
zstyle ':vcs_info:*' formats '(%s)-[%b]'
zstyle ':vcs_info:*' actionformats '%F{red}(%s)-[%b|%a]%f'
precmd() {
if [ ! -z $TMUX ]; then
tmux refresh-client -S
fi
}
_update_vcs_info_msg() {
vcs_info
# RPROMPT="%F{046}${vcs_info_msg_0_} %F{102}[%D{%Y-%m-%d %H:%M:%S}]"
# RPROMPT="%F{green}${vcs_info_msg_0_} %{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%} %F{gray}[%D{%Y-%m-%d %H:%M:%S}]"
}
add-zsh-hook precmd _update_vcs_info_msg
########################################
# オプション
setopt auto_cd # ディレクトリ名だけでcdする
setopt auto_list # 補完候補を一覧表示
setopt auto_menu # 補完候補が複数あるときに自動的に一覧表示する
setopt auto_param_keys # カッコの対応などを自動的に補完
setopt auto_param_slash
setopt auto_pushd # cd したら自動的にpushdする
setopt correct
setopt extended_glob
setopt ignore_eof
setopt interactive_comments # '#' 以降をコメントとして扱う
setopt list_packed # 補完候補を詰めて表示
setopt list_types # 補完候補一覧でファイルの種別をマーク表示
setopt magic_equal_subst # = の後はパス名として補完する
setopt no_beep # beep を無効にする
setopt no_flow_control # フローコントロールを無効にする
setopt noautoremoveslash # 最後のスラッシュを自動的に削除しない
setopt nonomatch
setopt notify # バックグラウンドジョブの状態変化を即時報告
setopt print_eight_bit # 日本語ファイル名を表示可能にする
setopt prompt_subst # プロンプト定義内で変数置換やコマンド置換を扱う
setopt pushd_ignore_dups # 重複したディレクトリを追加しない
########################################
# ^R で履歴検索をするときに * でワイルドカードを使用出来るようにする
bindkey -e
select-history() {
BUFFER=$(history -n -r 1 |
awk 'length($0) > 2' |
rg -v "^...$" |
rg -v "^....$" |
rg -v "^.....$" |
rg -v "^......$" |
rg -v "^exit$" |
uniq -u |
fzf-tmux --no-sort +m --query "$LBUFFER" --prompt="History > ")
CURSOR=$#BUFFER
}
zle -N select-history
bindkey '^r' select-history
fzf-z-search() {
local res=$(history -n 1 | tail -f | fzf)
if [ -n "$res" ]; then
BUFFER+="$res"
zle accept-line
else
return 0
fi
}
zle -N fzf-z-search
bindkey '^s' fzf-z-search
if type docker >/dev/null 2>&1; then
export DOCKER_BUILDKIT=1
export DOCKER_CLI_EXPERIMENTAL="enabled"
alias dls='docker ps'
alias dsh='docker run -it '
[ -z "$_lazy_docker_aliases" ] && {
[ -f $HOME/.aliases ] && source $HOME/.aliases
_lazy_docker_aliases=1
}
fi
if type octant >/dev/null 2>&1; then
export OCTANT_LISTENER_ADDR="0.0.0.0:8900"
fi
if type xsel >/dev/null 2>&1; then
alias pbcopy="xsel --clipboard --input"
alias pbpaste="xsel --clipboard --output"
else
if type wl-copy >/dev/null 2>&1; then
alias pbcopy="wl-copy"
fi
if type wl-paste >/dev/null 2>&1; then
alias pbpaste="wl-paste"
fi
fi
if type git >/dev/null 2>&1; then
# General Git aliases
alias gco="git checkout"
alias gsta="git status"
alias gcom="git commit -m"
alias gdiff="git diff"
alias gbra="git branch"
# Get current repository branch name
gitthisrepo() {
git symbolic-ref --short HEAD | tr -d "\n"
}
alias tb=gitthisrepo
# Get default branch name
gitdefaultbranch() {
git remote show origin | grep 'HEAD' | cut -d':' -f2 | sed -e 's/^ *//g' -e 's/ *$//g'
}
alias gitdb=gitdefaultbranch
# Check for merged branches that can be removed
gitremovalcheck() {
local db=$(gitdb)
git branch -r --merged "$db" | grep -v -e "$db" -e develop -e release | sed -E 's% *origin/%%'
git branch --merged "$db" | grep -vE '^\*|master$|develop$|main$'
}
alias grc=gitremovalcheck
# Common function for fetch, reset, and cleanup
git_fetch_reset() {
local tb=$(tb)
local db=$(gitdb)
git fetch --prune
git reset --hard origin/$tb || { echo "Failed to reset"; return 1; }
git branch -r --merged $db | grep -v -e $db -e develop -e release | sed -E 's% *origin/%%' | xargs -I% git push --delete origin % || { echo "Failed to delete merged branches"; return 1; }
git fetch --prune
git reset --hard origin/$tb || { echo "Failed to reset"; return 1; }
git branch --merged $db | grep -vE '^\*|master$|develop$|main$' | xargs -I % git branch -d % || { echo "Failed to delete local branches"; return 1; }
}
# Fetch, reset, and clean up branches
gfr() {
git_fetch_reset
}
alias gfr=gfr
# Fetch, reset, and update submodules
gfrs() {
gfr && git submodule foreach git pull origin "$(gitdb)" || { echo "Failed to update submodules"; return 1; }
}
alias gfrs=gfrs
# Pull with rebase
gitpull() {
git pull --rebase origin "$(tb)" || { echo "Failed to pull with rebase"; return 1; }
}
alias gpull=gitpull
# Push to current branch
gpush() {
git push -u origin "$(tb)" || { echo "Failed to push to origin"; return 1; }
}
alias gpush=gpush
# Commit, signoff, and push
gitcompush() {
git add -A
git commit --signoff -m "$1"
git push -u origin "$2"
}
alias gitcompush=gitcompush
# Commit, signoff, and push to current branch
gcp() {
gitcompush "$1" "$(tb)"
}
alias gcp=gcp
alias gfix="gcp fix"
# Commit, signoff, and force push with lease
gitcompushf() {
git add -A
git commit --signoff -m "$1"
git push --force-with-lease --set-upstream origin "$2"
}
alias gitcompushf=gitcompushf
# Commit, signoff, and force push to current branch
gcpf() {
gitcompushf "$1" "$(tb)"
}
alias gcpf=gcpf
# Amend last commit and force push
gfp() {
git add -A || { echo "Failed to stage changes"; return 1; }
git commit --signoff --amend || { echo "Failed to amend commit"; return 1; }
git push --force-with-lease || { echo "Failed to force push"; return 1; }
}
alias gfp=gfp
# Rebase and squash changes
grs() {
if [ $# -eq 1 ] || [ $# -eq 2 ]; then
local branch="$(tb)"
git checkout "$1" || { echo "Failed to checkout branch $1"; return 1; }
gfr
git checkout -b tmp || { echo "Failed to create tmp branch"; return 1; }
git merge --squash "$branch"
if [ $# -eq 2 ]; then
git checkout "$2" . || { echo "Failed to checkout files from $2"; return 1; }
fi
git branch -D "$branch" || { echo "Failed to delete branch $branch"; return 1; }
git branch -m "$branch" || { echo "Failed to rename branch"; return 1; }
else
echo "invalid argument, rebase branch name required"
return 1
fi
}
alias grs=grs
# Rebase, squash, and push changes
grsp() {
if [ $# -eq 1 ] || [ $# -eq 2 ]; then
local message="$(git log remotes/origin/$1..$branch --reverse --pretty=%s)"
grs "$@"
gcpf $message
else
echo "invalid argument, rebase branch name required"
return 1
fi
}
alias grsp=grsp
# Edit Git config
alias gedit="$EDITOR $HOME/.gitconfig"
# Fetch and merge from upstream
git-remote-merge() {
git fetch upstream || { echo "Failed to fetch from upstream"; return 1; }
git merge upstream/$(gitdb) || { echo "Failed to merge upstream branch"; return 1; }
}
alias grf=git-remote-merge
# Add and merge remote repository
git-remote-add-merge() {
git remote add upstream "$1" || { echo "Failed to add remote upstream"; return 1; }
grf
}
alias grfa=git-remote-add-merge
fi
if type rg >/dev/null 2>&1; then
alias grep=rg
if type curl >/dev/null 2>&1; then
listdomains() {
if [ $# -eq 1 ]; then
curl -fs $1 |
rg -Po '.*?//\K.*?(?=/)' |
rg -v "@" |
rg -v "\+" |
sort | uniq
else
echo "invalid argument, Domain or url is required"
fi
}
alias lsdomain=listdomains
fi
fi
# エイリアス
alias cp='cp -r'
alias mv='mv -i'
if type axel >/dev/null 2>&1; then
alias wget='axel -a -n 10'
else
alias wget='wget --no-cookies --no-check-certificate --no-dns-cache -4'
fi
alias mkdir='mkdir -p'
if type trans >/dev/null 2>&1; then
alias gtrans='trans -b -e google'
fi
# グローバルエイリアス
alias -g L='| less'
alias f="open ."
alias rm='rm -rf'
if type fd >/dev/null 2>&1; then
alias find='fd'
fi
if type dutree >/dev/null 2>&1; then
alias du='dutree'
fi
if type bat >/dev/null 2>&1; then
alias cat='bat'
fi
if type hyperfine >/dev/null 2>&1; then
alias time='hyperfine'
fi
if type procs >/dev/null 2>&1; then
alias ps='procs'
fi
if type btm >/dev/null 2>&1; then
alias top='btm'
alias htop='btm'
elif type htop >/dev/null 2>&1; then
alias top=htop
fi
if type lsd >/dev/null 2>&1; then
alias ks="lsd"
alias l="lsd"
alias ll='lsd -l'
alias la='lsd -aAlLh'
alias lla='lsd -aAlLhi'
alias tree='lsd --tree --total-size --human-readable'
alias ls='lsd'
alias lg='lsd -aAlLh | rg'
elif type erd >/dev/null 2>&1; then
alias ks='erd -H -d logical -I --level 1 --sort rsize -y inverted --dir-order first'
alias l='erd -H -d logical -I --level 1 --sort rsize -y inverted --dir-order first'
alias ll='erd -H -d logical -I --level 1 --sort rsize -y inverted --dir-order first'
alias la='erd -H -d logical -I -l --group --octal --time-format iso --sort rsize -y inverted --dir-order first --threads 32 --hidden --color force --level 1'
alias lla='erd -H -d logical -I -l --group --octal --time-format iso --sort rsize -y inverted --dir-order first --threads 32 --hidden --no-git --color force --level 2'
alias tree='erd -H -d logical -I -l --group --octal --time-format iso --sort rsize -y inverted --dir-order first --threads 32 --hidden --no-git --color force'
alias ls='erd -H -d logical -I --level 1 --sort rsize -y inverted --dir-order first'
alias lg='erd -H -d logical -I -l --group --octal --time-format iso --sort rsize -y inverted --dir-order first --threads 32 --hidden --color force --level 1 | rg'
elif type eza >/dev/null 2>&1; then
alias ks="eza -G"
alias l="eza -G "
alias ll='eza -l'
alias la='eza -aghHliS'
alias lla='eza -aghHliSm'
alias tree='eza -T'
alias ls='eza -G'
alias lg='la | rg'
else
alias ks="ls "
alias l="ls "
alias ll='ls -la'
alias la='ls -la'
alias lg='ls -a | rg'
fi
mkcd() {
if [[ -d $1 ]]; then
\cd $1
else
printf "Confirm to Make Directory? $1 [y/N]: "
if read -q; then
echo
\mkdir -p $1 && \cd $1
fi
fi
}
alias mkcd=mkcd
alias dl="\cd $HOME/Downloads"
alias dc="\cd $HOME/Documents"
alias ..='\cd ../'
alias ...='\cd ../../'
alias ....='\cd ../../../'
alias .....='\cd ../../../../'
alias ......='\cd ../../../../../'
alias ,,='\cd ../'
alias ,,,='\cd ../../'
alias ,,,,='\cd ../../../'
alias ,,,,,='cd ../../../../'
alias ,,,,,,='\cd ../../../../../'
if type fzf >/dev/null 2>&1; then
if type fzf-tmux >/dev/null 2>&1; then
if type fd >/dev/null 2>&1; then
alias s='mkcd $(fd -a -H -t d . | fzf-tmux +m)'
alias vf='vim $(fd -a -H -t f . | fzf-tmux +m)'
fi
if type rg >/dev/null 2>&1; then
fbr() {
git branch --all | rg -v HEAD | fzf-tmux +m | \sed -E "s/.* //" -e "s#remotes/[^/]*/##" | xargs git checkout
}
alias fbr=fbr
sshf() {
ssh $(rg "Host " $HOME/.ssh/config | awk '{print $2}' | rg -v "\*" | fzf-tmux +m)
}
alias sshf=sshf
fi
if type ghq >/dev/null 2>&1; then
alias g='mkcd $(ghq root)/$(ghq list | fzf-tmux +m)'
fi
fi
fi
if type ssh-keygen >/dev/null 2>&1; then
sshperm() {
sudo chown -R $(id -u):$(id -g) $HOME/.ssh
find $HOME/.ssh -type d -print | xargs sudo chmod 700
find $HOME/.ssh -type f -print | xargs sudo chmod 600
}
rsagen() {
ssh-keygen -t rsa -b 4096 -P $1 -f $HOME/.ssh/id_rsa -C $USER
sshperm
}
alias rsagen=rsagen
ecdsagen() {
ssh-keygen -t ecdsa -b 521 -P $1 -f $HOME/.ssh/id_ecdsa -C $USER
sshperm
}
alias ecdsagen=ecdsagen
edgen() {
ssh-keygen -t ed25519 -P $1 -f $HOME/.ssh/id_ed -C $USER
sshperm
}
alias edgen=edgen
alias sedit="$EDITOR $HOME/.ssh/config"
sshls() {
rg "Host " $HOME/.ssh/config | awk '{print $2}' | rg -v "\*"
}
alias sshls=sshls
sshinit() {
rm -rf $HOME/.ssh/known_hosts \
$HOME/.ssh/master_$GIT_USER@192.168.2.* \
/tmp/ssh-.*.sock
sshperm
}
alias sshinit=sshinit
fi
if type rails >/dev/null 2>&1; then
alias railskill="kill -9 $(ps aux | grep rails | awk '{print $2}')"
fi
if type tar >/dev/null 2>&1; then
alias tarzip="\tar Jcvf"
alias tarunzip="\tar Jxvf"
fi
if type duf >/dev/null 2>&1; then
alias df='\duf'
fi
if type ranger >/dev/null 2>&1; then
# rng() {
# if [ -z "$RANGER_LEVEL" ]; then
# \ranger $@
# else
# echo "other ranger process already running"
# fi
# }
# alias ranger=rng
alias rng=ranger
fi
if type tmux >/dev/null 2>&1; then
if [ -f /.dockerenv ]; then
tmux unbind C-b
tmux set -g prefix C-w
tmux bind C-w send-prefix
else
case ${OSTYPE} in
darwin*)
tmux unbind C-b
tmux set -g prefix C-g
tmux bind C-g send-prefix
;;
linux*)
tmux bind C-b send-prefix
;;
esac
fi
fi
alias tedit="$EDITOR $HOME/.tmux.conf"
zscompile() {
for f in $(find $HOME -name "*.zsh"); do
zcompile $f
done
}
alias zscompile=zscompile
zsup() {
rm -rf $HOME/.zcompd*
rm $HOME/.zshrc.zwc
# update sheldon package here
rm -rf $HOME/.bashrc
rm -rf $HOME/.fzf.bash
zscompile
}
alias zsup=zsup
zsinit() {
rm -rf $HOME/.zcompd*
rm -rf $HOME/.zshrc.zwc
}
alias zsinit=zsinit
zstime() {
for i in $(seq 1 $1); do
time $(zsh -i -c exit)
done
}
alias zstime=zstime
if { [ -L "$HOME/.zshrc" ] || [ -f "/.dockerenv" ]; } && [ -f "$DOTFILES_DIR/zshrc" ]; then
alias zedit="$EDITOR $DOTFILES_DIR/zshrc"
else
alias zedit="$EDITOR $HOME/.zshrc"
fi
alias zsback="cp $HOME/.zshrc $HOME/.zshrc.back"
jvgrule='(^|\/)\.zsh_history$|(^|\/)\.z$|(^|\/)\.cache|\.emlx$|\.mbox$|\.tar*|(^|\/)\.glide|(^|\/)\.stack|(^|\/)\.anyenv|(^|\/)\.gradle|(^|\/)vendor|(^|\/)Application\ Support|(^|\/)\.cargo|(^|\/)\.config|(^|\/)com\.apple\.|(^|\/)\.idea|(^|\/)\.zplug|(^|\/)\.nimble|(^|\/)build|(^|\/)node_modules|(^|\/)\.git$|(^|\/)\.svn$|(^|\/)\.hg$|\.o$|\.obj$|\.a$|\.exe~?$|\.schema.json&|\.svg$|(^|\/)tags$'
greptext() {
if [ $# -eq 2 ]; then
if type rg >/dev/null 2>&1; then
rg $2 $1
elif type jvgrep >/dev/null 2>&1; then
jvgrep -I -R $2 $1 --exclude $jvgrule
else
find $1 -type d \( -name 'vendor' -o -name '.git' -o -name '.svn' -o -name 'build' -o -name '*.mbox' -o -name '.idea' -o -name '.cache' -o -name 'Application\ Support' \) \
-prune -o -type f \( -name '.zsh_history' -o -name '*.zip' -o -name '*.tar.gz' -o -name '*.tar.xz' -o -name '*.o' -o -name '*.so' -o -name '*.dll' -o -name '*.a' -o -name '*.out' -o -name '*.pdf' -o -name '*.swp' -o -name '*.bak' -o -name '*.back' -o -name '*.bac' -o -name '*.class' -o -name '*.bin' -o -name '.z' -o -name '*.dat' -o -name '*.plist' -o -name '*.db' -o -name '*.webhistory' \) \
-prune -o -type f -print0 | xargs -0 -P $CPUCORES grep -rnwe $2 /dev/null
fi
else
echo "Not enough arguments"
fi
}
alias gt=greptext
chperm() {
if [ $# -eq 3 ]; then
sudo chmod $1 $3
sudo chown $2 $3
elif [ $# -eq 4 ]; then
sudo chmod -R $2 $4
sudo chown -R $3 $4
fi
}
chword() {
if [ $# -eq 3 ]; then
if type rg >/dev/null 2>&1; then
rg --multiline -l $2 $1 | xargs -t -P $CPUCORES \sed -i -E "s/$2/$3/g"
elif type ug >/dev/null 2>&1; then
cd $1 && ug -l $2 | xargs -t -P $CPUCORES \sed -i -E "s/$2/$3/g" && cd -
elif type jvgrep >/dev/null 2>&1; then
jvgrep -I -R $2 $1 --exclude $jvgrule -l -r |
xargs -t -P $CPUCORES \sed -i -E "s/$2/$3/g"
else
find $1 -type d \( -name 'vendor' -o -name '.git' -o -name '.svn' -o -name 'build' -o -name '*.mbox' -o -name '.idea' -o -name '.cache' -o -name 'Application\ Support' \) \
-prune -o -type f \( -name '.zsh_history' -o -name '*.zip' -o -name '*.tar.gz' -o -name '*.tar.xz' -o -name '*.o' -o -name '*.so' -o -name '*.dll' -o -name '*.a' -o -name '*.out' -o -name '*.pdf' -o -name '*.swp' -o -name '*.bak' -o -name '*.back' -o -name '*.bac' -o -name '*.class' -o -name '*.bin' -o -name '.z' -o -name '*.dat' -o -name '*.plist' -o -name '*.db' -o -name '*.webhistory' -o -name '*.schema.json' \) \
-prune -o -type f -print0 | xargs -0 -P $CPUCORES grep -rnwe $2 | xargs -t -P $CPUCORES \sed -i -E "s/$2/$3/g"
fi
elif [ $# -eq 4 ]; then
if type rg >/dev/null 2>&1; then
rg --multiline -l $2 $1 | xargs -t -P $CPUCORES \sed -i -E "s$4$2$4$3$4g"
elif type ug >/dev/null 2>&1; then
cd $1 && ug -l $2 $1 | xargs -t -P $CPUCORES \sed -i -E "s$4$2$4$3$4g" && cd -
elif type jvgrep >/dev/null 2>&1; then
jvgrep -I -R $2 $1 --exclude $jvgrule -l -r |
xargs -t -P $CPUCORES \sed -i -E "s$4$2$4$3$4g"
else
find $1 -type d \( -name 'vendor' -o -name '.git' -o -name '.svn' -o -name 'build' -o -name '*.mbox' -o -name '.idea' -o -name '.cache' -o -name 'Application\ Support' \) \
-prune -o -type f \( -name '.zsh_history' -o -name '*.zip' -o -name '*.tar.gz' -o -name '*.tar.xz' -o -name '*.o' -o -name '*.so' -o -name '*.dll' -o -name '*.a' -o -name '*.out' -o -name '*.pdf' -o -name '*.swp' -o -name '*.bak' -o -name '*.back' -o -name '*.bac' -o -name '*.class' -o -name '*.bin' -o -name '.z' -o -name '*.dat' -o -name '*.plist' -o -name '*.db' -o -name '*.webhistory' -o -name '*.schema.json' \) \
-prune -o -type f -print0 | xargs -0 -P $CPUCORES grep -rnwe $2 | xargs -t -P $CPUCORES \sed -i -E "s$4$2$4$3$4g"
fi
else
echo "Not enough arguments"
fi
}
alias chword=chword
alias :q=exit
alias :wq=exit
alias 600='chmod -R 600'
alias 644='chmod -R 644'
alias 655='chmod -R 655'
alias 755='chmod -R 755'
alias 777='chmod -R 777'
if type nvim >/dev/null 2>&1; then
alias nvup="nvim --headless -c 'UpdateRemotePlugins' -c 'PackerSync' -c 'PackerCompile'"
nvim-init() {
rm -rf "$HOME/.config/gocode"
rm -rf "$HOME/.config/nvim/autoload"
rm -rf "$HOME/.config/nvim/ftplugin"
rm -rf "$HOME/.config/nvim/log"
rm -rf "$HOME/.config/nvim/pack"
nvup
rm "$HOME/.nvimlog"
rm "$HOME/.viminfo"
}
alias vedit="$EDITOR $HOME/.config/nvim/init.lua"
alias nvinit="nvim-init"
alias vback="cp $HOME/.config/nvim/init.lua $HOME/.config/nvim/init.lua.back"
alias vrestore="cp $HOME/.config/nvim/init.lua.back $HOME/.config/nvim/init.lua"
alias vake="$EDITOR Makefile"
alias vocker="$EDITOR Dockerfile"
else
alias vedit="$EDITOR $HOME/.vimrc"
fi
alias vi="$EDITOR"
alias vim="$EDITOR"
alias bim="$EDITOR"
alias cim="$EDITOR"
alias v="$EDITOR"
alias vspdchk="rm -rf /tmp/starup.log && $EDITOR --startuptime /tmp/startup.log +q && less /tmp/startup.log"
alias xedit="$EDITOR $HOME/.Xdefaults"
alias wedit="$EDITOR $HOME/.config/sway/config"
# if type thefuck >/dev/null 2>&1; then
# eval $(thefuck --alias --enable-experimental-instant-mode)
# fi
if type kubectl >/dev/null 2>&1; then
kubectl() {