.fzy.zsh 1.1 KiB raw
1
#
2
# fzy shell integration
3
#
4
#     Adapted from fzf (https://github.com/junegunn/fzf).
5
#
6
# Copyright (c) 2016 Junegunn Choi
7
# Copyright (c) 2016 Alexis Sellier
8
#
9
if [[ $- != *i* ]]; then
10
	return
11
fi
12
13
__fzy_fsel () {
14
	setopt localoptions pipefail 2> /dev/null
15
	command rg --files -u . * | fzy | while read -r item; do
16
		echo -n "${(q)item}"
17
	done
18
	local ret=$?
19
	echo
20
	return $ret
21
}
22
23
fzy-file-widget () {
24
	LBUFFER="${LBUFFER}$(__fzy_fsel)"
25
	local ret=$?
26
	zle redisplay
27
	if [[ $ret == 0 ]]; then
28
		zle accept-line "$LBUFFER"
29
		ret=$?
30
	fi
31
	typeset -f zle-line-init >/dev/null && zle zle-line-init
32
	return $ret
33
}
34
zle     -N   fzy-file-widget
35
bindkey '^P' fzy-file-widget
36
37
fzy-history-widget () {
38
	local selected num
39
	setopt localoptions noglobsubst pipefail 2> /dev/null
40
	selected=( $(fc -l -r 1 | fzy -q "${LBUFFER//$/\\$}") )
41
	local ret=$?
42
	if [[ -n ${selected} ]]; then
43
		num=${selected[1]}
44
		if [[ -n ${num} ]]; then
45
			zle vi-fetch-history -n ${num}
46
			zle accept-line "$LBUFFER"
47
		fi
48
	fi
49
	zle redisplay
50
	typeset -f zle-line-init >/dev/null && zle zle-line-init
51
	return $ret
52
}
53
zle     -N   fzy-history-widget
54
bindkey '^H' fzy-history-widget