-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-fuzzy-wd.plugin.zsh
55 lines (45 loc) · 1.06 KB
/
zsh-fuzzy-wd.plugin.zsh
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
#!/usr/bin/env zsh
#
# Adds fuzzy search for directories "warped" with WD Zsh plugin.
# Activates with [Control+B].
#
WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
fuzzy_wd() {
# load warp points
typeset -A points
while read -r line
do
arr=(${(s,:,)line})
key=${arr[1]}
# join the rest, in case the path contains colons
val=${(j,:,)arr[2,-1]}
points[$key]=$val
done < ${WD_CONFIG}
# pipe warp points to fzf for selection
fzf_opts=${FZF_DEFAULT_OPTS:---height=40% --reverse}
selected=$(echo "${points[@]}" | tr ' ' '\n' | fzf $=fzf_opts)
if [[ -n "$selected" ]]; then
cd $(eval echo "${selected}")
fi
}
fuzzy_wd_widget() {
if [[ -e $WD_CONFIG ]]; then
fuzzy_wd
saved_buffer=$BUFFER
saved_cursor=$CURSOR
BUFFER=
zle redisplay
zle accept-line
fi
}
restore_buffer() {
BUFFER=$saved_buffer
CURSOR=$saved_cursor
saved_buffer=
saved_cursor=1
}
zle -N fuzzy_wd_widget
zle -N restore_buffer
autoload -Uz add-zle-hook-widget
add-zle-hook-widget line-init restore_buffer
bindkey ${FZF_WD_BINDKEY:-'^B'} fuzzy_wd_widget