This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc_bak
1011 lines (872 loc) · 32.2 KB
/
.zshrc_bak
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
# creates_tty=0
# has_compsys=0
# has_syntax_highlighting=0
# has_autosuggestions=0
# has_git_prompt=0
# first_prompt_lag_ms=83.642
# first_command_lag_ms=83.978
# command_lag_ms=0.301
# input_lag_ms=0.498
# exit_time_ms=90.680
# creates_tty=0
# has_compsys=1
# has_syntax_highlighting=1
# has_autosuggestions=1
# has_git_prompt=1
# first_prompt_lag_ms=92.677
# first_command_lag_ms=3848.722
# command_lag_ms=110.157
# input_lag_ms=37.546
# exit_time_ms=3150.649
# if [ -z "$TMUX" ]; then
# ~/bin/ta
# fi
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# exit 1
# 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
# make with the nice completion
# autoload -U compinit; compinit
# autoload -Uz compinit
# if [ $(date +'%j') != $(stat -f '%Sm' -t '%j' ~/.zcompdump) ]; then
# compinit
# else
# compinit -C
# fi
# use homebrew sqlite install
export PATH="/usr/local/opt/sqlite/bin:$PATH"
fpath=(
$fpath
/usr/local/share/zsh/site-functions
)
# color term
export CLICOLOR=1
export LSCOLORS=Dxfxcxdxbxegedabadacad
export ZLS_COLORS=$LSCOLORS
export LC_CTYPE=en_US.UTF-8
# https://github.com/kovidgoyal/kitty/issues/2047#issuecomment-934058006 ?
# export LANG=en_US.UTF-8
# export LANGUAGE=en_US.UTF-8
# export LC_ALL=en_US.UTF-8
# set for git delta, iirc
export LESS=FRX
# telescope and less? https://github.com/nvim-telescope/telescope.nvim/issues/253#issuecomment-730071741
# export LESS=iMRS
# make with the pretty colors
autoload colors; colors
# TODO: replace with https://github.com/jeffreytse/zsh-vi-mode ??
#vim binding
bindkey -v
bindkey -M vicmd '^r' history-incremental-search-backward
bindkey -M viins '^r' history-incremental-search-backward
bindkey "^[[H" beginning-of-line # Home key
bindkey "^[[F" end-of-line # End key
# https://github.com/zsh-users/zsh-autosuggestions#key-bindings
bindkey '^ ' autosuggest-accept # ctrl + space to accept the current suggestion. right arrow or EOL, also works
bindkey '^x' autosuggest-execute # ctrl + x to execute the current suggestion
export KEYTIMEOUT=5 # remove normal/insert mode switch delay
# vi-mode yank
function vi-yank-pbcopy {
zle vi-yank
echo "$CUTBUFFER" | pbcopy
}
zle -N vi-yank-pbcopy
bindkey -M vicmd 'y' vi-yank-pbcopy
# ci", ci', ci`, di", etc
autoload -U select-quoted
zle -N select-quoted
for m in visual viopp; do
for c in {a,i}{\',\",\`}; do
bindkey -M $m $c select-quoted
done
done
# ci{, ci(, ci<, di{, etc
autoload -U select-bracketed
zle -N select-bracketed
for m in visual viopp; do
for c in {a,i}${(s..)^:-'()[]{}<>bB'}; do
bindkey -M $m $c select-bracketed
done
done
# ic camelCase motions
autoload -U select-word-match
zle -N select-in-camel select-word-match
zle -N select-a-camel select-word-match
zstyle ':zle:*-camel' word-style normal-subword
bindkey -M viopp ic select-in-camel
# i/ and a/ path component motions
zstyle ':zle:select-*-directory' word-style unspecified
zstyle ':zle:select-*-directory' word-chars $'/<>=;&| \t\n'
# Change cursor shape for different vi modes.
function zle-keymap-select {
if [[ ${KEYMAP} == vicmd ]] ||
[[ $2 = 'block' ]]; then
echo -ne '\e[1 q'
elif [[ ${KEYMAP} == main ]] ||
[[ ${KEYMAP} == viins ]] ||
[[ ${KEYMAP} = '' ]] ||
[[ $1 = 'beam' ]]; then
echo -ne '\e[5 q'
fi
}
zle -N zle-keymap-select
zle-line-init() {
zle -K viins # initiate `vi insert` as keymap (can be removed if `bindkey -V` has been set elsewhere)
echo -ne "\e[5 q"
}
zle -N zle-line-init
echo -ne '\e[5 q' # Use beam shape cursor on startup.
preexec() { echo -ne '\e[5 q' ;} # Use beam shape cursor for each new prompt.
# history
HISTFILE=~/.zsh_history
HISTSIZE=10000000
SAVEHIST=10000000
# options https://gist.github.com/mattmc3/c490d01751d6eb80aa541711ab1d54b1#file-1-setopts-zsh-L50
setopt inc_append_history # sessions will append their history list to the history file
setopt always_to_end
# setopt appendhistory # sessions will append their history list to the history file
setopt extendedglob # Treat the ‘#’, ‘~’ and ‘^’ characters as part of filename
setopt auto_cd # if a command isn't valid, but is a directory, cd to that dir
setopt nonomatch # tell zsh to pass the unevaluated argument like bash
setopt interactivecomments # Allow comments even in interactive shells.
setopt hist_ignore_all_dups # Delete old recorded entry if new entry is a duplicate
# setopt share_history # Share history between all sessions.
setopt prompt_subst # parameter expansion, command substitution and arithmetic expansion are performed in prompts
setopt auto_resume # attempt to resume existing job before creating a new process
# don't append failed command to ~/.zsh_history
zshaddhistory() { whence ${${(z)1}[1]} >| /dev/null || return 1 }
# Bindings
# external editor support
autoload edit-command-line
zle -N edit-command-line
bindkey '^o' edit-command-line
# bindkey '^x^e' edit-command-line
# Partial word history completion
bindkey '\ep' up-line-or-search
bindkey '\en' down-line-or-search
bindkey '\ew' kill-region
# alt-c
bindkey "ç" fzf-cd-widget
# disable in tmux
if [ -z "$TMUX" ]; then
fg-widget() {
stty icanon echo pendin -inlcr < /dev/tty
stty discard '^O' dsusp '^Y' lnext '^V' quit '^\' susp '^Z' < /dev/tty
zle reset-prompt
if jobs %- >/dev/null 2>&1; then
fg %-
else
fg
fi
}
# Use C-z to foreground in zsh
zle -N fg-widget
bindkey -M emacs "^Z" fg-widget
bindkey -M vicmd "^Z" fg-widget
bindkey -M viins "^Z" fg-widget
fi
# fg () {
# _CMD=$(jobs -l | grep nvim | awk '{print $3}');
# echo _CMD
# echo $1
# if [[ "nvim" == "$1" ]]; then
# _CMD=$(ps -o cmd --no-headers $(jobs -l | awk '/\[[0-9]\]\+/{print $2}'));
# else
# _CMD=$(ps -o cmd --no-headers $(jobs -l | awk '/\['$1'\]/{print $2}'));
# fi;
# title $(basename $(echo $_CMD | awk '{print $1}')) "$_CMD";
# unset _CMD;
# builtin fg $*;
# }
# fkill: npm install --global fkill-cli
fkill_full() {
local pid
if [ "$UID" != "0" ]; then
pid=$(ps -f -u $UID | sed 1d | fzf-tmux -p 90%,90% --ansi --multi --preview-window=:hidden | awk '{print $2}')
else
pid=$(ps -ef | sed 1d | fzf-tmux -p 90%,90% --ansi --multi --preview-window=:hidden | awk '{print $2}')
fi
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
fi
}
# find-in-file - usage: fif <SEARCH_TERM>
fif() {
if [ ! "$#" -gt 0 ]; then
echo "Need a string to search for!";
return 1;
fi
rg --files-with-matches --no-messages "$1" | fzf-tmux -p 90%,90% --preview "rg --ignore-case --pretty --context 10 '$1' {}"
}
gcr() {
git checkout -b $1 origin/$1
}
# do instead? https://github.com/tpope/vim-rhubarb/commit/964d48f
git() {
if command -v hub >/dev/null; then
command hub "$@"
else
command git "$@"
fi
}
function tree-git-ignore {
# tree respecting gitignore
local ignored=$(git ls-files -ci --others --directory --exclude-standard)
local ignored_filter=$(echo "$ignored" \
| egrep -v "^#.*$|^[[:space:]]*$" \
| sed 's~^/~~' \
| sed 's~/$~~' \
| tr "\\n" "|")
tree --prune -I ".git|${ignored_filter: : -1}" "$@"
}
# default apps
# I have no idea why I have to unset this but otherwise I don't get LESS paging
unset DELTA_PAGER
export PAGER='less'
export EDITOR='nvim'
export MANPAGER='nvim +Man!'
# export MANPAGER="sh -c 'col -bx | bat -l man -p'"
export MANWIDTH=999
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"
# FZF SETTINGS
# tmux
export FZF_TMUX=1 # open in pop-up
export FZF_TMUX_OPTS="-p -w 90% -h 60%"
export FZF_TMUX_POP_UP_OPTS="-p 90%,90%"
# default fzf
export FZF_DEFAULT_COMMAND='rg --files'
# files
export FZF_CTRL_T_COMMAND="fd --hidden --follow --exclude '.git' --exclude 'node_modules'"
export FZF_CTRL_T_OPTS="$FZF_DEFAULT_OPTS $FZF_SHOW_PREVIEW --select-1"
# directories
export FZF_ALT_C_COMMAND="$FZF_CTRL_T_COMMAND --type d"
export FZF_ALT_C_OPTS="$FZF_DEFAULT_OPTS $FZF_SHOW_PREVIEW --select-1"
# history
# modify keybindings.zsh or add plugin: https://github.com/junegunn/fzf/issues/477#issuecomment-230338992
# ctrl-x to execute, enter to put in cmd line
export FZF_CTRL_R_OPTS="$FZF_HIDE_PREVIEW"
# presentation
export BAT_THEME="Enki-Tokyo-Night"
export FZF_HEADER_DEFAULT="ALT-k/j page up/down, CTRL-u/d preview half-page up/down, \ to toggle"
export FZF_HEADER_FILES="CTRL-s/ALT-d to select/unselect-all, CTRL-v open in nvim"
export FZF_HEADER_PASTE="CTRL-y to copy into clipboard"
export FZF_YANK_COMMAND="--bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort'"
# preview cycle window rotate: https://github.com/junegunn/fzf/commit/43f0d0c
export FZF_HIDE_PREVIEW="--preview-window=:hidden"
export FZF_SHOW_PREVIEW="
--preview '([[ -f {} ]] && (bat --style=full --color=always {} || cat {})) || ([[ -d {} ]] && (tree -C {} | less)) || echo {} 2> /dev/null | head -200'
--preview-window cycle
--bind '\:toggle-preview'
--bind 'ctrl-u:preview-half-page-up'
--bind 'ctrl-d:preview-half-page-down'
"
export FZF_DEFAULT_OPTS="
--history=$HOME/.fzf_history
--layout=reverse
--info=inline
--height=80%
--multi
--border
--color=fg:#c0caf5,bg:#1a1b26,hl:#bb9af7
--color=fg+:#c0caf5,bg+:#292e42,hl+:#7dcfff
--color=info:#7aa2f7,prompt:#7dcfff,pointer:#7dcfff
--color=marker:#9ece6a,spinner:#9ece6a,header:#9ece6a
--color=gutter:#1a1b26
--prompt='∼ ' --pointer='▶' --marker='✓'
--header '$FZF_HEADER_DEFAULT'
--bind 'ctrl-s:select-all'
--bind 'alt-d:deselect-all'
--preview-window cycle
--bind '\:toggle-preview'
--bind 'ctrl-u:preview-half-page-up'
--bind 'ctrl-d:preview-half-page-down'
--bind 'alt-k:page-up'
--bind 'alt-j:page-down'
--bind 'ctrl-v:execute(nvim {} < /dev/tty > /dev/tty 2>&1)+accept'
"
# --bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort'
# https://github.com/junegunn/fzf/commit/f84b3de24b63e2e26cbfa2a24e61a4173824fffd
# tweak above nvim open?
# ls | fzf --bind "enter:execute(vim {})"
# override builtin fzf functions for `app **<tab>`
_fzf_compgen_path() {
fd --hidden --follow --exclude ".git" . "$1"
}
# override builtin fzf functions for `cd **<tab>`
# Use fd to generate the list for directory completion
_fzf_compgen_dir() {
fd --type d --hidden --follow --exclude ".git" . "$1"
}
# directly executing the command (CTRL-X CTRL-R)
fzf-history-widget-accept() {
fzf-history-widget
zle accept-line
}
zle -N fzf-history-widget-accept
# bindkey '^X^R' fzf-history-widget-accept
in_tmux () {
if [ -n "$TMUX" ]; then
return 0
else
return 1
fi
}
# tm - create new tmux session, or switch to existing one. Works from within tmux too
# `tm` will allow you to select your tmux session via fzf.
# `tm irc` will attach to the irc session (if it exists), else it will create it.
tm() {
[[ -n "$TMUX" ]] && change="switch-client" || change="attach-session"
if [ $1 ]; then
tmux $change -t "$1" 2>/dev/null || (tmux new-session -d -s $1 && tmux $change -t "$1"); return
fi
session=$(tmux list-sessions -F "#{session_name}" 2>/dev/null | fzf-tmux -p 90%,90% -preview-window=:hidden --exit-0) && tmux $change -t "$session" || echo "No sessions found."
}
tmt() {
if [[ -z "$TMUX" ]]; then
tmux attach -t "$session_name"
else
tmux switch-client -t "$session_name"
fi
}
# fs() {
# local -r fmt='#{session_id}:|#S|(#{session_attached} attached)'
# { tmux display-message -p -F "$fmt" && tmux list-sessions -F "$fmt"; } \
# | awk '!seen[$1]++' \
# | column -t -s'|' \
# | fzf -q '$' --reverse --prompt 'switch session: ' -1 \
# | cut -d':' -f1 \
# | xargs tmux switch-client -t
# }
# would need to do a workaround like: https://newbedev.com/errors-from-whatis-command-unable-to-rebuild-database-with-makewhatis
# fman() {
# man -k . | fzf-tmux -p 90%,90% -preview-window=:hidden --prompt='Man> ' | awk '{print $1}' | xargs -r man
# }
# fman() {
# man -k . | fzf -q "$1" --prompt='man> ' --preview $'echo {} | tr -d \'()\' | awk \'{printf "%s ", $2} {print $1}\' | xargs -r man | col -bx | bat -l man -p --color always' | tr -d '()' | awk '{printf "%s ", $2} {print $1}' | xargs -r man
# }
# Get the colors in the opened man page itself
# export MANPATH="/usr/share/man"
# export MANPAGER="sh -c 'col -bx | bat -l man -p --paging always'"
# fstash - easier way to deal with stashes
# enter shows you the contents of the stash
# ctrl-d shows a diff of the stash against your current HEAD
# ctrl-b checks the stash out as a branch, for easier merging
fstash() {
local out q k sha
while out=$(
git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
fzf-tmux -p 90%,90% --ansi --no-sort --query="$q" --print-query \
--expect=ctrl-d,ctrl-b);
do
mapfile -t out <<< "$out"
q="${out[0]}"
k="${out[1]}"
sha="${out[-1]}"
sha="${sha%% *}"
[[ -z "$sha" ]] && continue
if [[ "$k" == 'ctrl-d' ]]; then
git diff $sha
elif [[ "$k" == 'ctrl-b' ]]; then
git stash branch "stash-$sha" $sha
break;
else
git stash show -p $sha
fi
done
}
# fgst - pick files from `git status -s`
is_in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
}
fgst() {
is_in_git_repo || return
local cmd="${FZF_CTRL_T_COMMAND:-"command git status -s"}"
eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" fzf -m "$@" | while read -r item; do
echo "$item" | awk '{print $2}'
done
echo
}
# quick rebase with origin
function greb() {
REMOTE=${1:-main}
git fetch origin && git rebase origin/${REMOTE}
}
# USAGE:
# git fixup
# git rebase -i master --autosquash
# https://fle.github.io/git-tip-keep-your-branch-clean-with-fixup-and-autosquash.html
function git-fixup () {
git log --oneline -n 20 | fzf-tmux -p 90%,90% | cut -f 1 | xargs git commit --no-verify --fixup
}
# Install (one or multiple) selected application(s)
# using "brew search" as source input
# mnemonic [B]rew [I]nstall [P]lugin
bip() {
local inst=$(brew search | fzf-tmux -p 90%,90% -m)
if [[ $inst ]]; then
for prog in $(echo $inst);
do; brew install $prog; done;
fi
}
# Update (one or multiple) selected application(s)
# mnemonic [B]rew [U]pdate [P]lugin
bup() {
local upd=$(brew leaves | fzf-tmux -p 90%,90% -m)
if [[ $upd ]]; then
for prog in $(echo $upd);
do; brew upgrade $prog; done;
fi
}
# Delete (one or multiple) selected application(s)
# mnemonic [B]rew [C]lean [P]lugin (e.g. uninstall)
bcp() {
local uninst=$(brew leaves | fzf-tmux -p 90%,90% -m)
if [[ $uninst ]]; then
for prog in $(echo $uninst);
do; brew uninstall $prog; done;
fi
}
# fbr - checkout git branch (including remote branches), sorted by most recent commit, limit 30 last branches
fbr() {
local branches branch
branches=$(git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format="%(refname:short)") &&
branch=$(echo "$branches" |
fzf-tmux -p 90%,90% -preview-window=:hidden) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
alias glNoGraph='git log --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr% C(auto)%an" "$@"'
_gitLogLineToHash="echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
_viewGitLogLine="$_gitLogLineToHash | xargs -I % sh -c 'git show --color=always % | diff-so-fancy'"
# fcoc_preview - checkout git commit with previews
fcoc_preview() {
local commit
commit=$( glNoGraph |
fzf-tmux -p 90%,90% --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview="$_viewGitLogLine" ) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# fshow_preview - git commit browser with previews
fshow_preview() {
glNoGraph |
fzf-tmux -p 90%,90% --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview="$_viewGitLogLine" \
--header "enter to view, alt-y to copy hash" \
--bind "enter:execute:$_viewGitLogLine | less -R" \
--bind "alt-y:execute:$_gitLogLineToHash | pbcopy"
}
# fco_preview - checkout git branch/tag, with a preview showing the commits between the tag/branch and HEAD
fco_preview() {
local tags branches target
branches=$(
git --no-pager branch --all \
--format="%(if)%(HEAD)%(then)%(else)%(if:equals=HEAD)%(refname:strip=3)%(then)%(else)%1B[0;34;1mbranch%09%1B[m%(refname:short)%(end)%(end)" \
| sed '/^$/d') || return
tags=$(
git --no-pager tag | awk '{print "\x1b[35;1mtag\x1b[m\t" $1}') || return
target=$(
(echo "$branches"; echo "$tags") |
fzf-tmux -p 90%,90% --no-hscroll --no-multi -n 2 \
--ansi --preview="git --no-pager log -150 --pretty=format:%s '..{2}'") || return
git checkout $(awk '{print $2}' <<<"$target" )
}
# change all these with these
git_reset_hard_remote() {
local commit
commit=$( git branch --show-current ) &&
git reset --hard origin/$(echo "$commit")
}
git_reset_hard_local() {
local commit
commit=$( git branch --show-current ) &&
git reset --hard $(echo "$commit")
}
# checkout existing branch otherwise create new branch
gcob() {
BRANCH=$1
ARGS=$2
if [ "$BRANCH" = "" ] ||
[ "$BRANCH" = "-" ]; then
git checkout $BRANCH
elif [ "$BRANCH $ARGS" = "-- ." ]; then
git checkout -- .
else
git checkout $(git show-ref --verify --quiet refs/heads/$BRANCH || echo '-b') $BRANCH
fi
}
gpull() {
if [ $# -eq 0 ]
then
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
else
BRANCH=${1}
fi
git pull origin "${BRANCH}"
}
gpush() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
git push -u origin "${BRANCH}"
}
function kube-toggle() {
if (( ${+POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND} )); then
unset POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND
else
POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND='kubectl|helm|kubens|kubectx|oc|istioctl|kogito|k9s|helmfile|flux|fluxctl|stern'
fi
p10k reload
if zle; then
zle push-input
zle accept-line
fi
}
# zle -N kube-toggle
# bindkey '^]' kube-toggle # ctrl-] to toggle kubecontext in powerlevel10k prompt
# aliases
alias ls='lsd'
alias ll='ls -l'
alias la='ls -a'
alias lla='ls -la'
alias lt='ls --tree'
alias lart='ls -lart'
alias md='mkdir -p'
alias rd='rmdir'
alias tree='tree-git-ignore'
alias cl='clear'
#vim
alias vi='nvim'
alias vim='nvim'
alias n='nvim'
alias wl='watchman watch-list'
alias wda='watchman watch-del-all'
# tmux
alias tmn='tmux new -s'
alias tma='tmux attach -t'
alias tms='tmux switch-client -t'
alias tmls='tmux ls'
alias tag='ta ~/code/invitae'
alias tkill="for s in \$(tmux list-sessions | awk '{print \$1}' | rg ':' -r '' | fzf); do tmux kill-session -t \$s; done;"
# alias tkill=tmux display-popup -E "for s in \$(tmux list-sessions | awk '{print \$1}' | rg ':' -r '' | fzf); do tmux kill-session -t \$s; done;"
# docker
alias dc='docker-compose'
alias dcu='docker-compose up'
alias dcd='docker-compose down'
alias dcps='docker-compose ps'
alias kt='kubetail'
alias ks='stern -l'
alias kctx='kubectx'
alias kc="kubecolor"
alias kns='kubens'
# python
alias pactivate='source $(poetry env info --path)/bin/activate'
# safe
alias safeauth="safe auth ldap"
alias safedev="safe target blah dev"
alias safetst="safe target blah tst"
alias safestg="safe target blah stg"
alias safeprd="safe target blah prd"
# https://qmacro.org/autodidactics/2021/08/06/tmux-output-formatting/
# 1. Open a popup
# 2. Show you all the docker images on your system in an FZF menu
# 3. Select your choice
# 4. A split pane (from target pane) will run docker run --rm -it <chosen_image>
alias dselect='tmux display-popup -E "docker image ls --format '{{.Repository}}' | fzf | xargs tmux split-window -h docker run --rm -it"'
# another option: https://github.com/natkuhn/Chrome-debug
alias chrome-debug='/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222&'
# used in 'gnap' alias
gref() {
command git --no-pager diff --cached --stat | command grep "|\s*0$" | awk '{system("command git reset " $1)}'
}
# only run lint on changes
# "git diff --name-only | grep -E '.(js|ts|tsx)$' | xargs eslint --fix"
ch() {
local cols sep
cols=$(( COLUMNS / 3 ))
sep='{::}'
# Chrome search
# cp -f ~/Library/Application\ Support/Google/Chrome/Default/History /tmp/h
# Chromium search https://github.com/Eloston/ungoogled-chromium
cp -f ~/Library/ApplicationSupport/Chromium/Default/History /tmp/h
sqlite3 -separator $sep /tmp/h \
"select substr(title, 1, $cols), url
from urls order by last_visit_time desc" |
awk -F $sep '{printf "%-'$cols's \x1b[36m%s\x1b[m\n", $1, $2}' |
fzf-tmux -p 90%,90% --ansi --multi --preview-window=:hidden | sed 's#.*\(https*://\)#\1#' | xargs open -a "Chromium"
}
# Default `fold` to screen width and break at spaces
function fold {
if [ $# -eq 0 ]; then
/usr/bin/fold -w $COLUMNS -s
else
/usr/bin/fold $*
fi
}
# Use `fzf` against system dictionary
function spell {
cat /usr/share/dict/words | fzf-tmux -p 90%,90% --preview 'wn {} -over | fold' --preview-window=up:60%
}
# Lookup definition of word using `wn $1 -over`.
# If $1 is not provided, we'll use the `spell` command to pick a word.
#
# Requires:
# brew install wordnet
# brew install fzf
function dic {
if [ $# -eq 0 ]; then
wn `spell` -over | fold
else
wn $1 -over | fold
fi
}
:e() {
if [[ -z $VIM_TERMINAL ]]; then
# maybe something like this instead? nvim **'\t'
nvim "$@"
else
for f; do
echo -e "\033]51;[\"drop\", \"$f\"]\007"
done
fi
}
# Select a docker container to start and attach to
function da() {
local cid
cid=$(docker ps -a | sed 1d | fzf -1 -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker start "$cid" && docker attach "$cid"
}
# Select a running docker container to stop
function ds() {
local cid
cid=$(docker ps | sed 1d | fzf -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker stop "$cid"
}
# Select a docker container to remove
function drm() {
local cid
cid=$(docker ps -a | sed 1d | fzf -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker rm "$cid"
}
# Select a docker image or images to remove
function drmi() {
docker images | sed 1d | fzf -q "$1" --no-sort -m --tac | awk '{ print $3 }' | xargs -r docker rmi
}
# git aliases
alias g='git'
alias ga='git add'
alias gap='git add -p'
alias gnap='git add -N --ignore-removal . && gap && gref'
# alias gnap='git add $(git ls-files -N -add -o --exclude-standard) || git add -N --ignore-removal . && gap && gref'
alias gb="git branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate"
alias gc='git commit -v'
alias gca='git commit -a -v'
alias gcf='git commit --fixup'
alias gcl='git clean -f -d'
alias gd='git diff'
alias gds='git diff --staged'
alias gdc='git diff --cached'
alias glod='git log --oneline --decorate'
alias glola='git log --graph --decorate --pretty=oneline --abbrev-commit --all'
alias gp='git push'
alias gpf='git pushf'
alias gpu='git pull'
alias gpr='git pull --rebase'
alias pulls='git browse -- pulls'
alias branches='git browse -- branches'
# alias open-PLACEHOLDER='git browse PLACEHOLDER'
alias gst='git status -sb'
alias gstl='git status'
alias ghst='gh pr status'
alias ghpr='gh pr list | fzf-tmux -p 90%,90% --preview "gh pr diff --color=always {+1}" | { read first rest ; echo $first ; } | xargs gh pr checkout'
alias ghd='gh pr list | fzf-tmux -p 90%,90% --preview "gh pr diff --color=always {+1}" | { read first rest ; echo $first ; } | xargs gh pr diff'
alias gs='fbr'
alias gr='git rebase'
alias grc='git rebase --continue'
alias gra='git rebase --abort'
alias grs='git rebase --skip'
alias gmc='git merge --continue'
alias gma='git merge --abort'
alias gms='git merge --skip'
alias gcp='git cherry-pick'
alias gcpa='git cherry-pick --abort'
alias gcps='git cherry-pick --skip'
alias gcpc='git cherry-pick --continue'
alias gco='git checkout'
# alias gcob='git checkout -b'
alias gcop='git checkout -p' # interactive hunk revert
alias gres='git restore --staged .'
alias gappend='git add . && git commit --amend -C HEAD'
alias gamend='git commit --amend -C HEAD'
alias gappendyolo='git add . && HUSKY_SKIP_HOOKS=1 git commit --amend -C HEAD'
alias gclean 'git clean -fd'
alias unstage='git restore --staged .'
alias grestore="git restore --staged . && git restore ."
alias reset_authors='git commit --amend --reset-author -C HEAD'
alias grhr="git_reset_hard_remote"
alias grhl="git_reset_hard_local"
alias stash="git add . && git add stash"
alias wip="git add . && HUSKY_SKIP_HOOKS=1 gc -m 'wip [ci skip]'"
alias undo="git reset HEAD~1 --mixed"
alias unwip="undo"
# alias unwip="git reset --soft 'HEAD^' && git restore --staged ."
alias nuke="unwip && grestore"
alias pokey="gco main && gpr && gco - && gr -"
alias gup="git up"
alias hokey="pokey"
alias sha="git rev-parse HEAD"
alias cannonball="git add . && git commit --amend -C HEAD && git push --force-with-lease"
alias cannonballyolo="git add . && HUSKY_SKIP_HOOKS=1 git commit --amend -C HEAD && git push --force-with-lease"
alias fix='nvim +/HEAD `git diff --name-only | uniq`'
# tmux alias
alias tmux_plugins_install="~/.tmux/plugins/tpm/bin/install_plugins"
alias tmux_plugins_update="~/.tmux/plugins/tpm/bin/update_plugins all"
alias tmux_plugins_clean="~/.tmux/plugins/tpm/bin/clean_plugins"
# random alias
alias be="bundle exec"
alias nvm="fnm"
alias strat="start"
alias ns="npm start"
alias barf="rm -rf node_modules && npm i"
alias rimraf="rm -rf node_modules"
# brew tap jason0x43/homebrew-neovim-nightly
# brew cask install neovim-nightly
alias update-neovim-nightly="brew reinstall neovim-nightly"
alias brew-tmux-head="brew reinstall tmux"
alias brew-install="brew bundle install --global"
alias brew-outdated="brew update && echo 'OUTDATED:' && brew outdated"
alias brewup="brew update; brew upgrade; brew cleanup"
alias crate-update="cargo install-update -a"
alias reload='source ~/.zshrc; echo -e "\n\u2699 \e[33mZSH config reloaded\e[0m \u2699"'
# remove duplicates in $PATH
typeset -aU path
# export PATH="$HOME/Library/Python/2.7/bin:$PATH"
export PATH="$(yarn global bin):$PATH"
export PATH="$HOME/bin:$PATH"
export GOPATH=$HOME/go
export GOROOT=/usr/local/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH
export PATH=$PATH:$GOROOT/bin
export PYTHONPATH="."
# eval "$(zsh)"
# eval "$(rbenv init -)"
# ruby-build installs a non-Homebrew OpenSSL for each Ruby version installed and these are never upgraded.
# To link Rubies to Homebrew's OpenSSL 1.1 (which is upgraded) add the following to your ~/.zshrc:
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix [email protected])"
[ -f $HOME/.cargo/env ] && source $HOME/.cargo/env
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# export FZF_COMPLETION_TRIGGER=''
# bindkey '^T' fzf-completion #context (dir) aware completion
# bindkey '^I' $fzf_default_completion
# Clone zcomet if necessary
if [[ ! -f ${ZDOTDIR:-${HOME}}/.zcomet/bin/zcomet.zsh ]]; then
command git clone https://github.com/agkozak/zcomet.git ${ZDOTDIR:-${HOME}}/.zcomet/bin
fi
source ${ZDOTDIR:-${HOME}}/.zcomet/bin/zcomet.zsh
zcomet load romkatv/powerlevel10k
zcomet load aloxaf/fzf-tab
zcomet load sudosubin/zsh-poetry
zcomet load zsh-vi-more/vi-motions
zcomet load greymd/docker-zsh-completion
zcomet load zsh-users/zsh-autosuggestions
zcomet load zdharma-continuum/fast-syntax-highlighting
zcomet load zsh-users/zsh-completions
# Completion for kill-like commands
# zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
# zstyle ':completion:*:*:*:*:processes' command "ps -u `whoami` -o pid,user,comm -w -w"
zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm -w -w"
zstyle ':completion:*:ssh:*' tag-order hosts users
zstyle ':completion:*:ssh:*' group-order hosts-domain hosts-host users hosts-ipaddr
zstyle ':completion:*:*:docker:*' option-stacking yes
zstyle ':completion:*:*:docker-*:*' option-stacking yes
# ignore completion functions (until the _ignored completer)
zstyle ':completion:*:functions' ignored-patterns '_*'
zstyle ':completion:*' accept-exact '*(N)'
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zshcache
zstyle ':completion:*' special-dirs true
# disable sort when completing options of any command
# zstyle ":completion:*:git-checkout:*" sort false
zstyle ':completion:complete:*:options' sort false
# TODO fix all this: https://superuser.com/questions/265547/zsh-cdpath-and-autocompletion
# set cd autocompletion to commonly visited directories
cdpath=($HOME/code/invitae $HOME/code/timtyrrell)
zstyle ':completion:*:complete:(cd|pushd):*' tag-order 'local-directories named-directories'
# set list-colors to enable filename colorizing
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# set descriptions format to enable group support
zstyle ':completion:*:descriptions' format '[%d]'
zstyle ':fzf-tab:*' show-group brief
zstyle ':fzf-tab:*' fzf-command ftb-tmux-popup
zstyle ':fzf-tab:*' popup-pad 200 200
# switch group using `,` and `.`
zstyle ':fzf-tab:*' switch-group ',' '.'
# use input as query string when completing zlua
zstyle ':fzf-tab:complete:_zlua:*' query-string input
zstyle ':fzf-tab:complete:(kill|ps):argument-rest' fzf-preview \
'[[ $group == "[process ID]" ]] && ps --pid=$word -o cmd --no-headers -w -w'
zstyle ':fzf-tab:complete:(kill|ps):argument-rest' fzf-flags --preview-window=down:3:wrap
zstyle ':fzf-tab:*' fzf-bindings 'ctrl-u:preview-half-page-up' 'ctrl-d:preview-half-page-down' 'space:accept' 'alt-k:page-up' 'alt-j:page-down'
zstyle ':fzf-tab:*' continuous-trigger '/'
zstyle ':fzf-tab:*' accept-line ctrl-x
local preview_command='
if [[ -d $realpath ]]; then
lsd --color always --icon always --depth 2 --tree $realpath
elif [[ -f $realpath ]]; then
bat --pager=never --color=always --line-range :80 $realpath
else
# "potato"
# lesspipe.sh $word | bat --color=always
exit 1
fi
'
zstyle ':fzf-tab:complete:*' fzf-preview $preview_command
# zstyle ':fzf-tab:complete:*.*' fzf-preview $preview_command
# zstyle ':fzf-tab:complete:*:*' fzf-preview 'less ${(Q)realpath}'
# export LESSOPEN="|/usr/local/bin/lesspipe.sh %s" LESS_ADVANCED_PREPROCESSOR=1
# zstyle ':fzf-tab:complete:(\\|)run-help:*' fzf-preview 'run-help $word'
# zstyle ':fzf-tab:complete:(\\|*/|)man:*' fzf-preview 'man $word'
# zstyle ':fzf-tab:complete:(-command-|-parameter-|-brace-parameter-|export|unset|expand):*' \
# fzf-preview 'echo ${(P)word}'
# zstyle ':fzf-tab:complete:cd:*' fzf-preview 'lsd --color always --icon always --depth 2 --tree $realpath'
# zstyle ':fzf-tab:complete:git-checkout:argument-rest' fzf-preview '
# [[ $group == "[recent branches]" || $group == "[local head]" ]] && git log --max-count=3 -p $word | delta
# '
# zstyle ':fzf-tab:complete:*' fzf-preview 'less $realpath'
# zstyle ':fzf-tab:complete:git-(add|diff|restore):*' fzf-preview 'git diff $word'
# zstyle -s ':fzf-tab:complete:git-add:*' fzf-preview str
zstyle ':fzf-tab:complete:git-(add|diff|restore):*' fzf-preview \
'git diff $word'
zstyle ':fzf-tab:complete:git-log:*' fzf-preview \
'git log --color=always $word'
zstyle ':fzf-tab:complete:git-help:*' fzf-preview \
'git help $word | bat -plman --color=always'
zstyle ':fzf-tab:complete:git-show:*' fzf-preview \
'case "$group" in
"commit tag") git show --color=always $word ;;
*) git show --color=always $word | delta ;;
esac'
zstyle ':fzf-tab:complete:git-checkout:*' fzf-preview \
'case "$group" in
"modified file") git diff $word | delta ;;
"recent commit object name") git show --color=always $word | delta ;;
*) git log --color=always $word ;;
esac'
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
[ -f ~/.kubectl_aliases ] && source ~/.kubectl_aliases
# eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib=$HOME/perl5)"
eval "$(pyenv init -)"
source <(stern --completion=zsh)
# setup kubecolor completion
compdef kubecolor=kubectl
# fnm