-
Notifications
You must be signed in to change notification settings - Fork 0
/
fzf_cmd
242 lines (213 loc) · 9.16 KB
/
fzf_cmd
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
#!/bin/env sh
#check for fzf availability
type -f fzf >/dev/null 2>&1 || return
# Global Setting {{{1
# Use other trigger instead of default **
#export FZF_COMPLETION_TRIGGER="'"
#NOTE: this determines also search program for fzf.vim
#now fzf (w/o pipe) will use ag instead of find
if type ag >/dev/null 2>&1; then
export FZF_DEFAULT_COMMAND='ag -p ~/.gitignore -g ""'
fi
#refer rg over ag
if type rg >/dev/null 2>&1; then
export FZF_DEFAULT_COMMAND='rg --files --hidden'
fi
#refer fd over rg
if type rg >/dev/null 2>&1; then
export FZF_DEFAULT_COMMAND='fd --type file --hidden'
fi
#show matches in top-down order
export FZF_DEFAULT_OPTS='
--bind ctrl-h:half-page-down,ctrl-l:half-page-up
--color hl:81,hl+:45,pointer:150,marker:118
--reverse'
#working directory with tree preview
if type tree >/dev/null 2>&1; then
export FZF_ALT_C_OPTS="--preview 'tree -C {} | head -100'"
fi
#1}}}
###############################################################################################
#Files and Folders {{{1
#fuzzy history
#show bash_history w/o duplicates and order preserved
fh() {
local revshist # return history in reverse order
if echo "$OSTYPE" | grep -q 'darwin' >/dev/null 2>&1; then
revshist="tail -r $HISTFILE"
else
revshist="tac $HISTFILE"
fi
eval "$revshist" | awk '!x[$0]++' |
fzf --prompt='(history search): ' --query="$*" --cycle --exact --select-1 --no-sort --height 10% \
--bind="ctrl-o:execute-silent(eval {})+abort"
}
#jump to bookmark of autojump
fj() {
local dir=$(autojump --stat | sed '/___/,$d' | cut -f 2 | #use only second column
sed '/rvm\/copy/d' | #ignore rvm_copy
fzf --query="$1" --select-1 --tac --no-sort --height 10%)
[ -d "$dir" ] && cd "$dir" || return
}
#goto directory
fcd() {
command -v tree >/dev/null 2>&1 && local treeview="tree -L 3 -C {} | head -100"
local dir=$(find "${1:-.}" -path '*/\.*' -prune -o -type d -not -path "*/build*/*" -print 2> /dev/null |
fzf +m --preview-window=right --preview="$treeview") &&
cd "$dir" && ls -l
}
#fuzzy editing with vim
fe() {
local out key files pipe_highlight term
term=$([ "$(tput colors)" -ge 256 ] && echo 'xterm256' || echo 'ansi')
#highlight with ansi output format and line number
#also force output when filetype not recognized, mute error in case not a file
command -v highlight >/dev/null 2>&1 && pipe_highlight="| highlight -s pablo -O ${term} -l --force 2>/dev/null"
#output multiple lines each should be a single element
mapfile -t out < <(fzf --query="$1" --multi --select-1 --exit-0 \
--header='ctrl-b:buffer, ctrl-s: hsplit, ctrl-v: vsplit' \
--preview="head -300 {} ${pipe_highlight}" \
--expect=ctrl-b,ctrl-s,ctrl-v)
#first is expected key (empty if not pressed), from second are selections (-m enable)
key="${out[0]}"
files=( "${out[@]:1}" )
case "$key" in
ctrl-b) opt= ;;
ctrl-s) opt="-o" ;;
ctrl-v) opt="-O" ;;
*) opt="-p" ;; #default open files in tab pages
esac
[ -n "${files[*]}" ] && vim $opt "${files[@]}"
}
#1}}}
###############################################################################################
# git stuff {{{1
# branches browser
gitbranch() {
#check whether a git repo before executing command
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
local abbrevname branches cmdswitch cmdlog cmdpreview
#get human name of current in priority branch > tag > hash
abbrevname="$(git symbolic-ref -q --short HEAD \
|| git describe --tags --exact-match \
|| git rev-parse --short HEAD)"
#list all branches then tags in date order
branches=$(git tag --sort=taggerdate;
git branch --all --color=always --sort=committerdate "$@" | \
grep -v 'HEAD\s' | sed s/^..// | cut -d" " -f1)
# delete prefix i.e. remotes/origin/feature/abc -> feature/abc
cmdswitch="git checkout \"\$(echo {} | sed 's/^.*\/\(.*\/.*$\)/\1/')\""
cmddelete="git branch -D {}"
cmdlog="git log --color=always --name-status {} | less -R"
cmdpreview="git log --oneline --graph --date=short --color=always \
--pretty='format:%C(auto)%cd %h%d %s' {} | head -$LINES"
fzf --ansi --tac +s --preview-window right:70% \
--header="current($abbrevname), enter:log, ctrl-o:checkout, ctrl-x:delete" \
--bind="?:toggle-preview" \
--bind="enter:execute($cmdlog)" \
--bind="ctrl-o:execute($cmdswitch)+abort" \
--bind="ctrl-x:execute($cmddelete)" \
--preview="$cmdpreview" <<< "$branches"
}
# commits browser
gitlog() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
local pos
[ "$(tput cols)" -ge 200 ] && pos="right" || pos="down"
local fmt='%C(auto)%h %Cgreen%cd%C(auto)%d %s <%an>'
local cmdgrep="\"\$(grep -m1 -o '[a-f0-9]\{7,40\}' <<< {} | head -n1 )\""
local cmdshow="git show --color=always $cmdgrep"
local cmdinfo="git log --color=always --name-status -n10 $cmdgrep | less -R"
fzf --prompt='gitlog: ' --ansi -e --no-sort --tiebreak=index \
--header='enter:diff, ?:show, c-i:info, c-c:exit, move:c-v/b(page)-h/l(half)-j/k(line)' \
--bind="ctrl-v:page-down,ctrl-b:page-up,ctrl-i:execute($cmdinfo)" \
--bind="?:toggle-preview,enter:execute($cmdshow | less -R)" \
--preview-window="$pos":hidden:wrap --preview="$cmdshow" \
< <(git log --graph --color=always --date=short --format="$fmt" "$@")
return 0;
}
#TODO open in vim instead
#add --staged get diff between index and HEAD
#add HEAD get diff between working tree vs index vs HEAD
# diffs browser
gitdiff() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
local cmddiff="git diff --color=always $*"
difflist=$(git status --short)
[ -z "$difflist" ] && return
fzf --ansi --no-sort \
--header='j/k:down/up, enter:diff, ctrl-o:open, ctrl-x:checkout' \
--preview-window=down:80%:wrap --preview="eval $cmddiff {2..}" \
--bind="enter:execute(eval $cmddiff {2..} | less -R)" \
--bind="ctrl-o:execute(gvim --remote-silent {2..})" \
--bind="ctrl-x:execute(git checkout {2..})" \
--bind="j:down,k:up" \
<<< "$difflist"
return 0;
}
# interactive rebase from a chosen commit sha
gitrebase() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
local fmt='%C(auto)%h %Cgreen%ad%C(auto)%d %s <%an>'
local log=$(git log --color=always --date=short --pretty="$fmt" | head -n 99)
#declare and assign separately to avoid shallow exit code of 'last' command
local commit
#chose a commit exit in case user cancel
commit=$(fzf --ansi --prompt='gitrebase: ' <<< "$log") || return
git rebase -i "$(echo "$commit" | awk '{print $1"^"}')" "$@"
}
# stash list show
gitstash() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
local out opt key idx
stashlist=$(git stash list --color=always --pretty='%C(yellow)%gd %>(14)%Cgreen%ci%Creset %gs')
[ -z "$stashlist" ] && return
#map two lines into a two-elements array
mapfile -t out < <(fzf --ansi --no-sort \
--header='enter:show, ctrl-d:diff, ctrl-o:pop, ctrl-y:apply, ctrl-x:drop' \
--preview='git stash show --color=always -p $(cut -d" " -f1 <<< {}) | head -'$LINES \
--preview-window=down:80% --reverse \
--bind='enter:execute(git stash show --color=always -p $(cut -d" " -f1 <<< {}) | less -r > /dev/tty)' \
--bind='ctrl-d:execute(git diff --color=always $(cut -d" " -f1 <<< {}) | less -r > /dev/tty)' \
--expect=ctrl-o,ctrl-y,ctrl-x <<< "$stashlist")
#fzf returns key (first line) result (second) if '--expect' specified (without --print-query)
key="${out[0]}"
idx="${out[1]%% *}" #remove everything from the end till space (longest match)
case "$key" in
ctrl-o) opt="pop" ;;
ctrl-x) opt="drop" ;;
ctrl-y) opt="apply" ;;
esac
[ -n "$opt" ] && [ -n "$idx" ] && git stash "$opt" "$idx";
}
#1}}}
###############################################################################################
#other commands {{{1
#kill tmux session by selecting from list
tkill() {
type tmux >/dev/null 2>&1 || return
local sessions
#list-session with (only session_name) format
mapfile -t sessions < <(tmux ls -F '#S' | fzf -m)
for elem in "${sessions[@]}"; do
tmux kill-session -t "$elem" && echo "killed session: '$elem'"
done
}
#1}}}
###############################################################################################
#key bindings {{{1
if echo "$-" | grep -q 'i' >/dev/null 2>&1; then
if [ -n "$BASH" ]; then
#overwrite default keybind c-r
bind '"\er": redraw-current-line'
bind '"\C-r": "$(fh)\e\C-e\er"'
#git things
#use -x to execute a shell-comand by bash
bind -x '"\C-g\C-b": "gitbranch"'
bind -x '"\C-g\C-d": "gitdiff"'
bind -x '"\C-g\C-l": "gitlog"'
bind -x '"\C-g\C-s": "gitstash"'
fi
fi
#1}}}
# vim:foldmethod=marker:foldlevel=1:syntax=bash