forked from reorx/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zshrc_fzf
236 lines (207 loc) · 7.29 KB
/
zshrc_fzf
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
# fzf-extras
# Additional key bindings for fzf, primarily Bash.
#
# bash cmdline
# - f-cd cd into the directory of the selected file
# - f-gbr Checkout Git branch (including remote branches)
# - f-gco Checkout Git branch/tag
# - f-gcoc Checkout Git commit
# - f-gcs Get Git commit SHA hash
# - f-d cd into selected directory
# - f-dh cd into selected directory, including hidden directories
# - f-e [FUZZY PATTERN] Open the selected file with the default editor
# - f-h Select line from history, repeat without editing
# - f-he Select line from history, leave for editing
# - f-kill Select process to kill (alternatively, type kill˽Tab)
# - f-o Equivalent to f-e, but opens it with xdg-open if you press Ctrl+O
# - f-tmux [FUZZY PATTERN] Select tmux session
# - f-show Git commit browser
# - f-stash Git stash management (Enter to show contents of the stash, Ctrl+D
# to show a diff of the stash against your current HEAD, Ctrl+B to check the stash out as a branch, for easier merging)
# - f-tags Search ctags
# - f-tpane Switch pane
# - f-v Open files in ~/.viminfo
# - writecmd Utility function used to write the command in the shell
# zsh cmdline
# - Alt-i Paste the selected entry from locate output into the command line
# f-e [FUZZY PATTERN] - Open the selected file with the default editor
# - Bypass fuzzy finder if there's only one match (--select-1)
# - Exit if there's no match (--exit-0)
f-e() {
local file
file=$(fzf-tmux --query="$1" --select-1 --exit-0)
[ -n "$file" ] && ${EDITOR:-vim} "$file"
}
# Modified version of f-e() where you can press
# - CTRL-O to open with `xdg-open` command,
# - CTRL-E or Enter key to open with the $EDITOR
f-o() {
local out file key
out=$(fzf-tmux --query="$1" --exit-0 --expect=ctrl-o,ctrl-e)
key=$(head -1 <<< "$out")
file=$(head -2 <<< "$out" | tail -1)
if [ -n "$file" ]; then
[ "$key" = ctrl-o ] && xdg-open "$file" || ${EDITOR:-vim} "$file"
fi
}
# f-d - cd to selected directory
f-d() {
DIR=`find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf-tmux` \
&& cd "$DIR"
}
# f-dh - including hidden directories
f-dh() {
DIR=`find ${1:-.} -type d 2> /dev/null | fzf-tmux` && cd "$DIR"
}
# f-cd - cd into the directory of the selected file
f-cd() {
local file
local dir
file=$(fzf +m -q "$1") && dir=$(dirname "$file") && cd "$dir"
}
# utility function used to write the command in the shell
writecmd() {
perl -e '$TIOCSTI = 0x5412; $l = <STDIN>; $lc = $ARGV[0] eq "-run" ? "\n" : ""; $l =~ s/\s*$/$lc/; map { ioctl STDOUT, $TIOCSTI, $_; } split "", $l;' -- $1
}
# f-h - repeat history
f-h() {
([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed -re 's/^\s*[0-9]+\s*//' | writecmd -run
}
# f-he - repeat history edit
f-he() {
([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed -re 's/^\s*[0-9]+\s*//' | writecmd
}
# f-kill - kill process
f-kill() {
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]
then
kill -${1:-9} $pid
fi
}
# f-gbr - checkout git branch (including remote branches)
f-gbr() {
local branches branch
branches=$(git branch --all | grep -v HEAD) &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
# f-gco - checkout git branch/tag
f-gco() {
local tags branches target
tags=$(
git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
branches=$(
git branch --all | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
sort -u | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
target=$(
(echo "$tags"; echo "$branches") |
fzf-tmux -l30 -- --no-hscroll --ansi +m -d "\t" -n 2) || return
git checkout $(echo "$target" | awk '{print $2}')
}
# f-gcoc - checkout git commit
f-gcoc() {
local commits commit
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# f-show - git commit browser
f-show() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --toggle-sort=\` \
--bind "ctrl-m:execute:
echo '{}' | grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R'"
}
# f-gcs - get git commit sha
# example usage: git rebase -i `f-gcs`
f-gcs() {
local commits commit
commits=$(git log --color=always --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e --ansi --reverse) &&
echo -n $(echo "$commit" | sed "s/ .*//")
}
# f-stash - easier way to deal with stashes
# type f-stash to get a list of your 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
f-stash() {
local out q k sha
while out=$(
git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
fzf --ansi --no-sort --query="$q" --print-query \
--expect=ctrl-d,ctrl-b);
do
q=$(head -1 <<< "$out")
k=$(head -2 <<< "$out" | tail -1)
sha=$(tail -1 <<< "$out" | cut -d' ' -f1)
[ -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
}
# f-tags - search ctags
f-tags() {
local line
[ -e tags ] &&
line=$(
awk 'BEGIN { FS="\t" } !/^!/ {print toupper($4)"\t"$1"\t"$2"\t"$3}' tags |
cut -c1-80 | fzf --nth=1,2
) && $EDITOR $(cut -f3 <<< "$line") -c "set nocst" \
-c "silent tag $(cut -f2 <<< "$line")"
}
# f-tmux [FUZZY PATTERN] - Select selected tmux session
# - Bypass fuzzy finder if there's only one match (--select-1)
# - Exit if there's no match (--exit-0)
f-tmux() {
local session
session=$(tmux list-sessions -F "#{session_name}" | \
fzf-tmux --query="$1" --select-1 --exit-0) &&
tmux switch-client -t "$session"
}
# f-tpane - switch pane
f-tpane () {
local panes current_window target target_window target_pane
panes=$(tmux list-panes -s -F '#I:#P - #{pane_current_path} #{pane_current_command}')
current_window=$(tmux display-message -p '#I')
target=$(echo "$panes" | fzf) || return
target_window=$(echo $target | awk 'BEGIN{FS=":|-"} {print$1}')
target_pane=$(echo $target | awk 'BEGIN{FS=":|-"} {print$2}' | cut -c 1)
if [[ $current_window -eq $target_window ]]; then
tmux select-pane -t ${target_window}.${target_pane}
else
tmux select-pane -t ${target_window}.${target_pane} &&
tmux select-window -t $target_window
fi
}
# f-v - open files in ~/.viminfo
f-v() {
local files
files=$(grep '^>' ~/.viminfo | cut -c3- |
while read line; do
[ -f "${line/\~/$HOME}" ] && echo "$line"
done | fzf-tmux -d -m -q "$*" -1) && vim ${files//\~/$HOME}
}
# ALT-I - Paste the selected entry from locate output into the command line
if [[ $- =~ i ]]; then
fzf-locate-widget() {
local selected
if selected=$(locate / | fzf -q "$LBUFFER"); then
LBUFFER=$selected
fi
zle redisplay
}
zle -N fzf-locate-widget
bindkey '\ei' fzf-locate-widget
fi