forked from AnthonyAstige/.dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.fzf-key-bindings.bash
196 lines (165 loc) · 7.33 KB
/
.fzf-key-bindings.bash
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
# Anthony forked from ~/.fzf/shell/key-bindings.bash Dec 2017
# Key bindings
# ------------
__fzf_select__() {
local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type f -print \
-o -type d -print \
-o -type l -print 2> /dev/null | cut -b3-"}"
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
printf '%q ' "$item"
done
echo
}
if [[ $- =~ i ]]; then
__fzf_use_tmux__() {
[ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]
}
__fzfcmd() {
__fzf_use_tmux__ &&
echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"
}
__fzf_select_tmux__() {
local height
height=${FZF_TMUX_HEIGHT:-40%}
if [[ $height =~ %$ ]]; then
height="-p ${height%\%}"
else
height="-l $height"
fi
tmux split-window $height "cd $(printf %q "$PWD"); FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS") PATH=$(printf %q "$PATH") FZF_CTRL_T_COMMAND=$(printf %q "$FZF_CTRL_T_COMMAND") FZF_CTRL_T_OPTS=$(printf %q "$FZF_CTRL_T_OPTS") bash -c 'source \"${BASH_SOURCE[0]}\"; RESULT=\"\$(__fzf_select__ --no-height)\"; tmux setb -b fzf \"\$RESULT\" \\; pasteb -b fzf -t $TMUX_PANE \\; deleteb -b fzf || tmux send-keys -t $TMUX_PANE \"\$RESULT\"'"
}
fzf-file-widget() {
if __fzf_use_tmux__; then
__fzf_select_tmux__
else
local selected="$(__fzf_select__)"
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
fi
}
__fzf_cd__() {
local cmd dir
cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type d -print 2> /dev/null | cut -b3-"}"
dir=$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m) && printf 'cd %q' "$dir"
}
__fzf_history__() (
local line
shopt -u nocaseglob nocasematch
line=$(
HISTTIMEFORMAT= history |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tac -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m" $(__fzfcmd) |
command grep '^ *[0-9]') &&
if [[ $- =~ H ]]; then
sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line"
else
sed 's/^ *\([0-9]*\)\** *//' <<< "$line"
fi
)
# GIT heart FZF (1 of 2)
# Start & inspiration: https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
# -------------
is_in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
}
__fzf_down__() {
fzf --height 50% "$@" --border
}
__fzf_gf__() {
is_in_git_repo || return
git -c color.status=always status --short |
__fzf_down__ -m --ansi --nth 2..,.. --height 90% --preview-window right:70% \
--preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500' |
cut -c4- | sed 's/.* -> //'
}
__fzf_gb__() {
is_in_git_repo || return
git branch -a --color=always | grep -v '/HEAD\s' | sort |
__fzf_down__ --ansi --multi --tac --preview-window right:70% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##'
}
__fzf_gt__() {
is_in_git_repo || return
git tag --sort -version:refname |
__fzf_down__ --multi --preview-window right:70% \
--preview 'git show --color=always {} | head -'$LINES
}
__fzf_gh__() {
is_in_git_repo || return
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always |
__fzf_down__ --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \
--header 'Press CTRL-S to toggle sort' \
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES |
grep -o "[a-f0-9]\{7,\}"
}
__fzf_gr__() {
is_in_git_repo || return
git remote -v | awk '{print $1 "\t" $2}' | uniq |
__fzf_down__ --tac \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {1} | head -200' |
cut -d$'\t' -f1
}
if [[ ! -o vi ]]; then
# Required to refresh the prompt after fzf
bind '"\er": redraw-current-line'
bind '"\e^": history-expand-line'
# CTRL-P - Paste the selected file path into the command line
if [ $BASH_VERSINFO -gt 3 ]; then
bind -x '"\C-p": "fzf-file-widget"'
elif __fzf_use_tmux__; then
bind '"\C-p": " \C-u \C-a\C-k`__fzf_select_tmux__`\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"'
else
bind '"\C-p": " \C-u \C-a\C-k`__fzf_select__`\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"'
fi
# CTRL-R - Paste the selected command from history into the command line
bind '"\C-r": " \C-e\C-u`__fzf_history__`\e\C-e\e^\er"'
# ALT-C - cd into the selected directory
# TODO: Put this back on soemthing that doesn't overwrite MacOS copy keyboard shortcut
# bind '"\ec": " \C-e\C-u`__fzf_cd__`\e\C-e\er\C-m"'
else
# We'd usually use "\e" to enter vi-movement-mode so we can do our magic,
# but this incurs a very noticeable delay of a half second or so,
# because many other commands start with "\e".
# Instead, we bind an unused key, "\C-x\C-a",
# to also enter vi-movement-mode,
# and then use that thereafter.
# (We imagine that "\C-x\C-a" is relatively unlikely to be in use.)
bind '"\C-x\C-a": vi-movement-mode'
bind '"\C-x\C-e": shell-expand-line'
bind '"\C-x\C-r": redraw-current-line'
bind '"\C-x^": history-expand-line'
# CTRL-P - Paste the selected file path into the command line
# - FIXME: Selected items are attached to the end regardless of cursor position
if [ $BASH_VERSINFO -gt 3 ]; then
bind -x '"\C-p": "fzf-file-widget"'
elif __fzf_use_tmux__; then
bind '"\C-p": "\C-x\C-a$a \C-x\C-addi`__fzf_select_tmux__`\C-x\C-e\C-x\C-a0P$xa"'
else
bind '"\C-p": "\C-x\C-a$a \C-x\C-addi`__fzf_select__`\C-x\C-e\C-x\C-a0Px$a \C-x\C-r\C-x\C-axa "'
fi
bind -m vi-command '"\C-p": "i\C-p"'
# CTRL-R - Paste the selected command from history into the command line
bind '"\C-r": "\C-x\C-addi`__fzf_history__`\C-x\C-e\C-x^\C-x\C-a$a\C-x\C-r"'
bind -m vi-command '"\C-r": "i\C-r"'
# ALT-C - cd into the selected directory
# TODO: Put this back on soemthing that doesn't overwrite MacOS copy keyboard shortcut
# bind '"\ec": "\C-x\C-addi`__fzf_cd__`\C-x\C-e\C-x\C-r\C-m"'
# bind -m vi-command '"\ec": "ddi`__fzf_cd__`\C-x\C-e\C-x\C-r\C-m"'
# GIT heart FZF (2 of 2)
# Start & inspiration: https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
bind '"\er": redraw-current-line'
bind '"\C-g\C-f": "\C-x\C-a$a \C-x\C-addi`__fzf_gf__`\C-x\C-e\C-x\C-a0Px$a \C-x\C-r\C-x\C-axa "'
bind -m vi-command '"\C-g\C-f": "i\C-g\C-f"'
bind '"\C-g\C-b": "\C-x\C-a$a \C-x\C-addi`__fzf_gb__`\C-x\C-e\C-x\C-a0Px$a \C-x\C-r\C-x\C-axa "'
bind -m vi-command '"\C-g\C-b": "i\C-g\C-b"'
bind '"\C-g\C-t": "\C-x\C-a$a \C-x\C-addi`__fzf_gt__`\C-x\C-e\C-x\C-a0Px$a \C-x\C-r\C-x\C-axa "'
bind -m vi-command '"\C-g\C-t": "i\C-g\C-t"'
bind '"\C-g\C-h": "\C-x\C-a$a \C-x\C-addi`__fzf_gh__`\C-x\C-e\C-x\C-a0Px$a \C-x\C-r\C-x\C-axa "'
bind -m vi-command '"\C-g\C-h": "i\C-g\C-h"'
bind '"\C-g\C-r": "\C-x\C-a$a \C-x\C-addi`__fzf_gr__`\C-x\C-e\C-x\C-a0Px$a \C-x\C-r\C-x\C-axa "'
bind -m vi-command '"\C-g\C-r": "i\C-g\C-r"'
fi
fi