Skip to content

Commit 21d1246

Browse files
committed
Fork of fzf plugin from Oh My Zsh
Add ctrl+f and ctrl+g commands
1 parent 5549e36 commit 21d1246

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed

zsh_custom/plugins/fzf/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
> [!NOTICE]
2+
> This was forked from the Oh My zsh plugin and modified
3+
4+
# fzf
5+
6+
This plugin tries to find [junegunn's fzf](https://github.com/junegunn/fzf) based on where
7+
it's been installed, and enables its fuzzy auto-completion and key bindings.
8+
9+
To use it, add `fzf` to the plugins array in your zshrc file:
10+
11+
```zsh
12+
plugins=(... fzf)
13+
```
14+
15+
## Settings
16+
17+
All these settings should go in your zshrc file, before Oh My Zsh is sourced.
18+
19+
### `FZF_BASE`
20+
21+
Set to fzf installation directory path:
22+
23+
```zsh
24+
export FZF_BASE=/path/to/fzf/install/dir
25+
```
26+
27+
### `FZF_DEFAULT_COMMAND`
28+
29+
Set default command to use when input is tty:
30+
31+
```zsh
32+
export FZF_DEFAULT_COMMAND='<your fzf default commmand>'
33+
```
34+
35+
If not set, the plugin will try to set it to these, in the order in which they're found:
36+
37+
- [`rg`](https://github.com/BurntSushi/ripgrep)
38+
- [`fd`](https://github.com/sharkdp/fd)
39+
- [`ag`](https://github.com/ggreer/the_silver_searcher)
40+
41+
### `DISABLE_FZF_AUTO_COMPLETION`
42+
43+
Set whether to load fzf auto-completion:
44+
45+
```zsh
46+
DISABLE_FZF_AUTO_COMPLETION="true"
47+
```
48+
49+
### `DISABLE_FZF_KEY_BINDINGS`
50+
51+
Set whether to disable key bindings (CTRL-T, CTRL-R, ALT-C):
52+
53+
```zsh
54+
DISABLE_FZF_KEY_BINDINGS="true"
55+
```
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
function setup_using_base_dir() {
2+
local fzf_base fzf_shell fzfdirs dir
3+
4+
test -d "${FZF_BASE}" && fzf_base="${FZF_BASE}"
5+
6+
if [[ -z "${fzf_base}" ]]; then
7+
fzfdirs=(
8+
"${HOME}/.fzf"
9+
"${HOME}/.nix-profile/share/fzf"
10+
"${XDG_DATA_HOME:-$HOME/.local/share}/fzf"
11+
"/usr/local/opt/fzf"
12+
"/usr/share/fzf"
13+
"/usr/local/share/examples/fzf"
14+
)
15+
for dir in ${fzfdirs}; do
16+
if [[ -d "${dir}" ]]; then
17+
fzf_base="${dir}"
18+
break
19+
fi
20+
done
21+
22+
if [[ -z "${fzf_base}" ]]; then
23+
if (( ${+commands[brew]} )) && dir="$(brew --prefix fzf 2>/dev/null)"; then
24+
if [[ -d "${dir}" ]]; then
25+
fzf_base="${dir}"
26+
fi
27+
fi
28+
fi
29+
fi
30+
31+
if [[ ! -d "${fzf_base}" ]]; then
32+
return 1
33+
fi
34+
35+
# Fix fzf shell directory for Arch Linux, NixOS or Void Linux packages
36+
if [[ ! -d "${fzf_base}/shell" ]]; then
37+
fzf_shell="${fzf_base}"
38+
else
39+
fzf_shell="${fzf_base}/shell"
40+
fi
41+
42+
# Setup fzf binary path
43+
if (( ! ${+commands[fzf]} )) && [[ "$PATH" != *$fzf_base/bin* ]]; then
44+
export PATH="$PATH:$fzf_base/bin"
45+
fi
46+
47+
# Auto-completion
48+
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
49+
source "${fzf_shell}/completion.zsh" 2> /dev/null
50+
fi
51+
52+
# Key bindings
53+
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
54+
source "${fzf_shell}/key-bindings.zsh"
55+
fi
56+
}
57+
58+
59+
function setup_using_debian_package() {
60+
if (( ! $+commands[dpkg] )) || ! dpkg -s fzf &>/dev/null; then
61+
# Either not a debian based distro, or no fzf installed
62+
return 1
63+
fi
64+
65+
# NOTE: There is no need to configure PATH for debian package, all binaries
66+
# are installed to /usr/bin by default
67+
68+
local completions key_bindings
69+
70+
case $PREFIX in
71+
*com.termux*)
72+
# Support Termux package
73+
completions="${PREFIX}/share/fzf/completion.zsh"
74+
key_bindings="${PREFIX}/share/fzf/key-bindings.zsh"
75+
;;
76+
*)
77+
# Determine completion file path: first bullseye/sid, then buster/stretch
78+
completions="/usr/share/doc/fzf/examples/completion.zsh"
79+
[[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
80+
key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
81+
;;
82+
esac
83+
84+
# Auto-completion
85+
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
86+
source $completions 2> /dev/null
87+
fi
88+
89+
# Key bindings
90+
if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
91+
source $key_bindings
92+
fi
93+
94+
return 0
95+
}
96+
97+
function setup_using_opensuse_package() {
98+
# OpenSUSE installs fzf in /usr/bin/fzf
99+
# If the command is not found, the package isn't installed
100+
(( $+commands[fzf] )) || return 1
101+
102+
# The fzf-zsh-completion package installs the auto-completion in
103+
local completions="/usr/share/zsh/site-functions/_fzf"
104+
# The fzf-zsh-completion package installs the key-bindings file in
105+
local key_bindings="/etc/zsh_completion.d/fzf-key-bindings"
106+
107+
# If these are not found: (1) maybe we're not on OpenSUSE, or
108+
# (2) maybe the fzf-zsh-completion package isn't installed.
109+
if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
110+
return 1
111+
fi
112+
113+
# Auto-completion
114+
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
115+
source "$completions" 2>/dev/null
116+
fi
117+
118+
# Key bindings
119+
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
120+
source "$key_bindings" 2>/dev/null
121+
fi
122+
123+
return 0
124+
}
125+
126+
function setup_using_openbsd_package() {
127+
# openBSD installs fzf in /usr/local/bin/fzf
128+
if [[ "$OSTYPE" != openbsd* ]] || (( ! $+commands[fzf] )); then
129+
return 1
130+
fi
131+
132+
# The fzf package installs the auto-completion in
133+
local completions="/usr/local/share/zsh/site-functions/_fzf_completion"
134+
# The fzf package installs the key-bindings file in
135+
local key_bindings="/usr/local/share/zsh/site-functions/_fzf_key_bindings"
136+
137+
# Auto-completion
138+
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
139+
source "$completions" 2>/dev/null
140+
fi
141+
142+
# Key bindings
143+
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
144+
source "$key_bindings" 2>/dev/null
145+
fi
146+
147+
return 0
148+
}
149+
150+
function indicate_error() {
151+
cat >&2 <<EOF
152+
[oh-my-zsh] fzf plugin: Cannot find fzf installation directory.
153+
Please add \`export FZF_BASE=/path/to/fzf/install/dir\` to your .zshrc
154+
EOF
155+
}
156+
157+
# Indicate to user that fzf installation not found if nothing worked
158+
setup_using_openbsd_package \
159+
|| setup_using_debian_package \
160+
|| setup_using_opensuse_package \
161+
|| setup_using_base_dir \
162+
|| indicate_error
163+
164+
unset -f setup_using_opensuse_package setup_using_debian_package setup_using_base_dir indicate_error
165+
166+
if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
167+
if (( $+commands[rg] )); then
168+
export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"'
169+
elif (( $+commands[fd] )); then
170+
export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
171+
elif (( $+commands[ag] )); then
172+
export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git'
173+
fi
174+
fi
175+
176+
# Helper: use fzf-tmux popup if in tmux, otherwise regular fzf
177+
_fzf_cmd() {
178+
if [[ -n "$TMUX" ]]; then
179+
fzf-tmux -p 80%,80% "$@"
180+
else
181+
fzf "$@"
182+
fi
183+
}
184+
185+
# Ctrl+F: fuzzy find files and insert into command line
186+
fzf-file-widget() {
187+
local cmd selected
188+
if (( $+commands[fd] )); then
189+
cmd='fd --type f --hidden --exclude .git'
190+
elif (( $+commands[rg] )); then
191+
cmd='rg --files --hidden --glob "!.git/*"'
192+
else
193+
cmd='find . -type f -not -path "*/\.git/*"'
194+
fi
195+
selected=$(eval "$cmd" | _fzf_cmd --reverse)
196+
if [[ -n "$selected" ]]; then
197+
LBUFFER+="${selected}"
198+
fi
199+
zle redisplay
200+
}
201+
zle -N fzf-file-widget
202+
bindkey '^F' fzf-file-widget
203+
204+
# Ctrl+G: fuzzy grep file contents and insert filename into command line
205+
fzf-grep-widget() {
206+
local selected file
207+
if (( $+commands[rg] )); then
208+
selected=$(rg --color=always --line-number --hidden --glob "!.git/*" "" 2>/dev/null \
209+
| _fzf_cmd --ansi --reverse \
210+
--delimiter : \
211+
--preview 'bat --style=numbers --color=always --highlight-line {2} {1} 2>/dev/null || head -100 {1}' \
212+
--preview-window '+{2}-5')
213+
else
214+
selected=$(grep -rn --color=always "" . 2>/dev/null \
215+
| _fzf_cmd --ansi --reverse \
216+
--delimiter : \
217+
--preview 'head -100 {1}')
218+
fi
219+
if [[ -n "$selected" ]]; then
220+
file=$(echo "$selected" | cut -d: -f1)
221+
LBUFFER+="${file}"
222+
fi
223+
zle redisplay
224+
}
225+
zle -N fzf-grep-widget
226+
bindkey '^G' fzf-grep-widget

0 commit comments

Comments
 (0)