-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-bindings.sh
215 lines (192 loc) · 6.32 KB
/
zsh-bindings.sh
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
#!/usr/bin/env zsh
# Ctrl-P - select file to paste
fzf-file-select-and-insert() {
LBUFFER+="$(fzf-file-selector)"
zle redisplay
}
zle -N fzf-file-select-and-insert
bindkey '^p' fzf-file-select-and-insert
# Alt-P - select file based on ripgrep
fzf-ripgrep-select-and-insert() {
LBUFFER+="$(fzf-ripgrep-selector "$LBUFFER")"
zle redisplay
}
zle -N fzf-ripgrep-select-and-insert
bindkey '^[p' fzf-ripgrep-select-and-insert
# Ctrl-N - insert current directory's name
insert-current-directory-basename() {
LBUFFER+="$(basename $(pwd))"
zle redisplay
}
zle -N insert-current-directory-basename
bindkey '^n' insert-current-directory-basename
# Ctrl-H - find commit SHA(s)
fzf-gh-widget() {
local result="$(git fuzzy log | join_lines);"
LBUFFER+="$result"
zle redisplay
}
zle -N fzf-gh-widget
bindkey '^h' fzf-gh-widget
# Alt-O - open files differing from particular commit
fzf-git-files-from-commits() {
local commits="$(git fuzzy log)"
local num_commits="$(echo "$commits" | wc -l | awk '{ print $1 }')"
if [ "$num_commits" -eq 0 ]; then
local result="$(git fuzzy diff $(git merge-base HEAD $(git merge-base-absolute)) | join_lines);"
LBUFFER+="$result"
elif [ "$num_commits" -eq 1 ]; then
local result="$(git fuzzy diff "$commits"^ | join_lines);"
LBUFFER+="$result"
elif [ "$num_commits" -eq 2 ]; then
local range="$(echo "$commits" | tac | join_lines '..')"
local result="$(git fuzzy diff "$range" | join_lines);"
LBUFFER+="$result"
elif [ "$num_commits" -ge 2 ]; then
# unsupported
fi
zle redisplay
}
zle -N fzf-git-files-from-commits
bindkey '^[o' fzf-git-files-from-commits
# Ctrl-O - open files differing from merge-base
fzf-open-git-widget() {
local result="$( \
{ git fuzzy status || \
git fuzzy diff $(git merge-base HEAD $(git merge-base-absolute)) \
} | join_lines \
)"
if [[ "$LBUFFER" = *" " ]]; then
LBUFFER+="$result"
else
LBUFFER+=" $result"
fi
zle redisplay
}
zle -N fzf-open-git-widget
bindkey '^O' fzf-open-git-widget
# Ctrl-B/F - back / forward by word (instead of Alt)
# frees up Alt for other uses
bindkey '^b' backward-word
bindkey '^f' forward-word
# Alt-B inserts branch (OR if you use shortcuts, anything relating to branches)
if command_exists git-fuzzy; then
insert-git-branch-name() {
LBUFFER+="$(git-fuzzy branch)"
}
zle -N insert-git-branch-name
bindkey '^[b' insert-git-branch-name
fi
# Ctrl-V - edit the command line in vim
bindkey '^v' edit-command-line
fixup-command-for-cmd-substitution() {
if [[ "$1" == *'rg'* ]]; then
if [[ "$1" == *'$(rg'* ]] || [[ "$1" == *' -l'* ]] || [[ "$1" == *' --files-without-match'* ]]; then
# too complex or already containing flag needed
echo "$1"
else
echo "$1" | sed 's/rg/rg -l/'
fi
else
echo "$1"
fi
}
# Alt-V - `vim $(... -l)` around
insert-last-command-with-vim() {
BUFFER='vim $('
BUFFER+="$(fixup-command-for-cmd-substitution "$history[$((HISTCMD-1))]")"
BUFFER+=')'
CURSOR=$(echo "$#BUFFER - 1" | bc)
}
zle -N insert-last-command-with-vim
bindkey '^[v' insert-last-command-with-vim
# Alt-R - `rg $(... -l)` around
insert-last-command-with-rg() {
BUFFER='rg -- $('
BUFFER+="$(fixup-command-for-cmd-substitution "$history[$((HISTCMD-1))]")"
BUFFER+=')'
CURSOR=3
}
zle -N insert-last-command-with-rg
bindkey '^[r' insert-last-command-with-rg
# Shell-GPT integration ZSH v0.2
_sgpt_zsh() {
if [[ -n "$BUFFER" ]]; then
_sgpt_prev_cmd=$BUFFER
BUFFER+="⌛"
zle -I && zle redisplay
BUFFER=$(sgpt --shell <<< "$_sgpt_prev_cmd" --no-interaction)
zle end-of-line
fi
}
zle -N _sgpt_zsh
bindkey ^l _sgpt_zsh
# Shell-GPT integration ZSH v0.2
# Alt-S - insert SHA of the current commit
insert-sha() {
LBUFFER+="$(git rev-parse HEAD)"
}
zle -N insert-sha
bindkey '^[s' insert-sha
# Alt-S - insert SHA of the current commit
insert-sha() {
LBUFFER+="$(git rev-parse HEAD)"
}
zle -N insert-sha
bindkey '^[s' insert-sha
# Alt-E - make the command use `entr`
add-entr() {
if [[ "$#BUFFER" -gt 0 ]]; then
BUFFER="fd | entr -c $BUFFER"
CURSOR="$((CURSOR + 13))"
else
BUFFER="fd | entr -c $history[$((HISTCMD-1))]"
CURSOR=2
fi
}
zle -N add-entr
bindkey '^[e' add-entr
# Ctrl-R - append history to the command line
fzf-history-widget-append() {
local selected
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob nobash_rematch 2> /dev/null
# Ensure the module is loaded if not already, and the required features, such
# as the associative 'history' array, which maps event numbers to full history
# lines, are set. Also, make sure Perl is installed for multi-line output.
if zmodload -F zsh/parameter p:{commands,history} 2>/dev/null && (( ${+commands[perl]} )); then
selected="$(printf '%s\t%s\000' "${(kv)history[@]}" |
perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\t(.*)/s, $1)}++) { s/\n/\n\t/g; print; }' |
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --expect='enter,alt-enter' --bind=ctrl-r:toggle-sort --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m --read0") \
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
else
selected="$(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --expect='enter,alt-enter' --bind=ctrl-r:toggle-sort --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m") \
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
fi
local ret=$?
selected_content="$(echo "$selected" | tail -n +2)"
key_pressed="$(echo "$selected" | head -n 1)"
if [ -n "$selected_content" ]; then
if [[ $(awk '{print $1; exit}' <<< "$selected_content") =~ ^[1-9][0-9]* ]]; then
NEW_CONTENT="$(history $MATCH | head -n1 | sed -E 's/^[[:space:]]*[0-9]+[[:space:]]*//')"
else # selected is a custom query, not from history
NEW_CONTENT="$selected_content"
fi
if [ "$key_pressed" = 'alt-enter' ]; then
if ! [[ "$LBUFFER" =~ ^[[:space:]]*$ ]]; then
NEW_CONTENT=$'\n'"$NEW_CONTENT"
fi
if ! [[ "$RBUFFER" =~ ^[[:space:]]*$ ]]; then
NEW_CONTENT="$NEW_CONTENT"$'\n'
fi
LBUFFER+="$NEW_CONTENT"
else
LBUFFER="$NEW_CONTENT"
RBUFFER=""
fi
fi
zle reset-prompt
return $ret
}
zle -N fzf-history-widget-append
bindkey '^r' fzf-history-widget-append