Debian Codegraph @

doc/fzf.txt

raw
1 fzf.txt fzf Last change: February 15 2024
2 FZF - TABLE OF CONTENTS *fzf* *fzf-toc*
3 ==============================================================================
4
5 FZF Vim integration |fzf-vim-integration|
6 Installation |fzf-installation|
7 Summary |fzf-summary|
8 :FZF[!] |:FZF|
9 Configuration |fzf-configuration|
10 Examples |fzf-examples|
11 Explanation of g:fzf_colors |fzf-explanation-of-gfzfcolors|
12 fzf#run |fzf#run|
13 fzf#wrap |fzf#wrap|
14 Global options supported by fzf#wrap |fzf-global-options-supported-by-fzf#wrap|
15 Tips |fzf-tips|
16 fzf inside terminal buffer |fzf-inside-terminal-buffer|
17 Starting fzf in a Vim popup window |fzf-starting-fzf-in-a-vim-popup-window|
18 Starting fzf in a tmux/Zellij popup window |fzf-starting-fzf-in-a-tmuxzellij-popup-window|
19 Hide statusline |fzf-hide-statusline|
20 License |fzf-license|
21
22 FZF VIM INTEGRATION *fzf-vim-integration*
23 ==============================================================================
24
25
26 INSTALLATION *fzf-installation*
27 ==============================================================================
28
29 Once you have fzf installed, you can enable it inside Vim simply by adding the
30 directory to 'runtimepath' in your Vim configuration file. The path may differ
31 depending on the package manager.
32 >
33 " If installed using Homebrew
34 set rtp+=/usr/local/opt/fzf
35
36 " If installed using Homebrew on Apple Silicon
37 set rtp+=/opt/homebrew/opt/fzf
38
39 " If you have cloned fzf on ~/.fzf directory
40 set rtp+=~/.fzf
41 <
42 If you use {vim-plug}{1}, the same can be written as:
43 >
44 " If installed using Homebrew
45 Plug '/usr/local/opt/fzf'
46
47 " If installed using Homebrew on Apple Silicon
48 Plug '/opt/homebrew/opt/fzf'
49
50 " If you have cloned fzf on ~/.fzf directory
51 Plug '~/.fzf'
52 <
53 But if you want the latest Vim plugin file from GitHub rather than the one
54 included in the package, write:
55 >
56 Plug 'junegunn/fzf'
57 <
58 The Vim plugin will pick up fzf binary available on the system. If fzf is not
59 found on `$PATH`, it will ask you if it should download the latest binary for
60 you.
61
62 To make sure that you have the latest version of the binary, set up
63 post-update hook like so:
64
65 *fzf#install*
66 >
67 Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
68 <
69 {1} https://github.com/junegunn/vim-plug
70
71
72 SUMMARY *fzf-summary*
73 ==============================================================================
74
75 The Vim plugin of fzf provides two core functions, and `:FZF` command which is
76 the basic file selector command built on top of them.
77
78 1. `fzf#run([spec dict])`
79 - Starts fzf inside Vim with the given spec
80 - `:call fzf#run({'source': 'ls'})`
81 2. `fzf#wrap([spec dict]) -> (dict)`
82 - Takes a spec for `fzf#run` and returns an extended version of it with
83 additional options for addressing global preferences (`g:fzf_xxx`)
84 - `:echo fzf#wrap({'source': 'ls'})`
85 - We usually wrap a spec with `fzf#wrap` before passing it to `fzf#run`
86 - `:call fzf#run(fzf#wrap({'source': 'ls'}))`
87 3. `:FZF [fzf_options string] [path string]`
88 - Basic fuzzy file selector
89 - A reference implementation for those who don't want to write VimScript to
90 implement custom commands
91 - If you're looking for more such commands, check out {fzf.vim}{2} project.
92
93 The most important of all is `fzf#run`, but it would be easier to understand
94 the whole if we start off with `:FZF` command.
95
96 {2} https://github.com/junegunn/fzf.vim
97
98
99 :FZF[!]
100 ==============================================================================
101
102 *:FZF*
103 >
104 " Look for files under current directory
105 :FZF
106
107 " Look for files under your home directory
108 :FZF ~
109
110 " With fzf command-line options
111 :FZF --reverse --info=inline /tmp
112
113 " Bang version starts fzf in fullscreen mode
114 :FZF!
115 <
116 Similarly to {ctrlp.vim}{3}, use Enter key, CTRL-T, CTRL-X or CTRL-V to open
117 selected files in the current window, in new tabs, in horizontal splits, or in
118 vertical splits respectively.
119
120 Note that the environment variables `FZF_DEFAULT_COMMAND` and
121 `FZF_DEFAULT_OPTS` also apply here.
122
123 {3} https://github.com/kien/ctrlp.vim
124
125
126 < Configuration >_____________________________________________________________~
127 *fzf-configuration*
128
129 *g:fzf_action* *g:fzf_layout* *g:fzf_colors* *g:fzf_history_dir*
130
131 - `g:fzf_action`
132 - Customizable extra key bindings for opening selected files in different
133 ways
134 - `g:fzf_layout`
135 - Determines the size and position of fzf window
136 - `g:fzf_colors`
137 - Customizes fzf colors to match the current color scheme
138 - `g:fzf_history_dir`
139 - Enables history feature
140
141
142 Examples~
143 *fzf-examples*
144 >
145 " This is the default extra key bindings
146 let g:fzf_action = {
147 \ 'ctrl-t': 'tab split',
148 \ 'ctrl-x': 'split',
149 \ 'ctrl-v': 'vsplit' }
150
151 " An action can be a reference to a function that processes selected lines
152 function! s:build_quickfix_list(lines)
153 call setqflist(map(copy(a:lines), '{ "filename": v:val, "lnum": 1 }'))
154 copen
155 cc
156 endfunction
157
158 let g:fzf_action = {
159 \ 'ctrl-q': function('s:build_quickfix_list'),
160 \ 'ctrl-t': 'tab split',
161 \ 'ctrl-x': 'split',
162 \ 'ctrl-v': 'vsplit' }
163
164 " Default fzf layout
165 if exists('$TMUX') || exists('$ZELLIJ')
166 " The Vim plugin will try to open fzf in a tmux or Zellij popup
167 " if possible (requires recent fzf and tmux/zellij) using --popup option,
168 " with the following argument:
169 let g:fzf_layout = { 'popup': '90%,60%' }
170 else
171 " If --popup option is not available, it will open in a popup window inside
172 " Vim (center of the screen)
173 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }
174 endif
175
176 " Here are some more layout examples:
177
178 " - Tmux or Zellij popup at the bottom 40%
179 let g:fzf_layout = { 'popup': 'bottom,40%' }
180
181 " - Tmux or Zellij popup at the top with a different size
182 let g:fzf_layout = { 'popup': 'top,90%,40%' }
183
184 " - Vim popup window: at the center of the current window (relative)
185 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true } }
186
187 " - Vim popup window: anchored to the bottom of the current window
188 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true, 'yoffset': 1.0 } }
189
190 " - Vim split window: down / up / left / right
191 let g:fzf_layout = { 'down': '40%' }
192
193 " - Vim window using a Vim command
194 let g:fzf_layout = { 'window': 'enew' }
195 let g:fzf_layout = { 'window': '-tabnew' }
196 let g:fzf_layout = { 'window': '10new' }
197
198 " Customize fzf colors to match your color scheme
199 " - fzf#wrap translates this to a set of `--color` options
200 let g:fzf_colors =
201 \ { 'fg': ['fg', 'Normal'],
202 \ 'bg': ['bg', 'Normal'],
203 \ 'hl': ['fg', 'Comment'],
204 \ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
205 \ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
206 \ 'hl+': ['fg', 'Statement'],
207 \ 'info': ['fg', 'PreProc'],
208 \ 'border': ['fg', 'Ignore'],
209 \ 'prompt': ['fg', 'Conditional'],
210 \ 'pointer': ['fg', 'Exception'],
211 \ 'marker': ['fg', 'Keyword'],
212 \ 'spinner': ['fg', 'Label'],
213 \ 'header': ['fg', 'Comment'] }
214
215 " Enable per-command history
216 " - History files will be stored in the specified directory
217 " - When set, CTRL-N and CTRL-P will be bound to 'next-history' and
218 " 'previous-history' instead of 'down' and 'up'.
219 let g:fzf_history_dir = '~/.local/share/fzf-history'
220 <
221
222 Explanation of g:fzf_colors~
223 *fzf-explanation-of-gfzfcolors*
224
225 `g:fzf_colors` is a dictionary mapping fzf elements to a color specification
226 list:
227 >
228 element: [ component, group1 [, group2, ...] ]
229 <
230 - `element` is an fzf element to apply a color to:
231
232 ----------------------------+------------------------------------------------------
233 Element | Description ~
234 ----------------------------+------------------------------------------------------
235 `fg` / `bg` / `hl` | Item (foreground / background / highlight)
236 `fg+` / `bg+` / `hl+` | Current item (foreground / background / highlight)
237 `preview-fg` / `preview-bg` | Preview window text and background
238 `gutter` | Background of the gutter on the left
239 `pointer` | Pointer to the current line ( `>` )
240 `marker` | Multi-select marker ( `>` )
241 `border` | Border around the window ( `--border` and `--preview` )
242 `header` | Header ( `--header` or `--header-lines` )
243 `info` | Info line (match counters)
244 `spinner` | Streaming input indicator
245 `query` | Query string
246 `disabled` | Query string when search is disabled
247 `prompt` | Prompt before query ( `> ` )
248 ----------------------------+------------------------------------------------------
249 - `component` specifies the component (`fg` / `bg`) from which to extract the
250 color when considering each of the following highlight groups
251 - `group1 [, group2, ...]` is a list of highlight groups that are searched (in
252 order) for a matching color definition
253
254 For example, consider the following specification:
255 >
256 'prompt': ['fg', 'Conditional', 'Comment'],
257 <
258 This means we color the prompt - using the `fg` attribute of the `Conditional`
259 if it exists, - otherwise use the `fg` attribute of the `Comment` highlight
260 group if it exists, - otherwise fall back to the default color settings for
261 the prompt.
262
263 You can examine the color option generated according to the setting by printing
264 the result of `fzf#wrap()` function like so:
265 >
266 :echo fzf#wrap()
267 <
268
269 FZF#RUN
270 ==============================================================================
271
272 *fzf#run*
273
274 `fzf#run()` function is the core of Vim integration. It takes a single
275 dictionary argument, a spec, and starts fzf process accordingly. At the very
276 least, specify `sink` option to tell what it should do with the selected
277 entry.
278 >
279 call fzf#run({'sink': 'e'})
280 <
281 We haven't specified the `source`, so this is equivalent to starting fzf on
282 command line without standard input pipe; fzf will traverse the file system
283 under the current directory to get the list of files. (If
284 `$FZF_DEFAULT_COMMAND` is set, fzf will use the output of the command
285 instead.) When you select one, it will open it with the sink, `:e` command. If
286 you want to open it in a new tab, you can pass `:tabedit` command instead as
287 the sink.
288 >
289 call fzf#run({'sink': 'tabedit'})
290 <
291 You can use any shell command as the source to generate the list. The
292 following example will list the files managed by git. It's equivalent to
293 running `git ls-files | fzf` on shell.
294 >
295 call fzf#run({'source': 'git ls-files', 'sink': 'e'})
296 <
297 fzf options can be specified as `options` entry in spec dictionary.
298 >
299 call fzf#run({'sink': 'tabedit', 'options': '--multi --reverse'})
300 <
301 You can also pass a layout option if you don't want fzf window to take up the
302 entire screen.
303 >
304 " up / down / left / right / window are allowed
305 call fzf#run({'source': 'git ls-files', 'sink': 'e', 'left': '40%'})
306 call fzf#run({'source': 'git ls-files', 'sink': 'e', 'window': '30vnew'})
307 <
308 `source` doesn't have to be an external shell command, you can pass a Vim
309 array as the source. In the next example, we pass the names of color schemes
310 as the source to implement a color scheme selector.
311 >
312 call fzf#run({'source': map(split(globpath(&rtp, 'colors/*.vim')),
313 \ 'fnamemodify(v:val, ":t:r")'),
314 \ 'sink': 'colo', 'left': '25%'})
315 <
316 The following table summarizes the available options.
317
318 ---------------------------+---------------+----------------------------------------------------------------------
319 Option name | Type | Description ~
320 ---------------------------+---------------+----------------------------------------------------------------------
321 `source` | string | External command to generate input to fzf (e.g. `find .` )
322 `source` | list | Vim list as input to fzf
323 `sink` | string | Vim command to handle the selected item (e.g. `e` , `tabe` )
324 `sink` | funcref | Function to be called with each selected item
325 `sinklist` (or `sink*` ) | funcref | Similar to `sink` , but takes the list of output lines at once
326 `exit` | funcref | Function to be called with the exit status of fzf (e.g. 0, 1, 2, 130)
327 `options` | string/list | Options to fzf
328 `dir` | string | Working directory
329 `up` / `down` / `left` / `right` | number/string | (Layout) Window position and size (e.g. `20` , `50%` )
330 `popup` / `tmux` | string | (Layout) `--popup` options (e.g. `90%,70%` )
331 `window` (Vim 8 / Neovim) | string | (Layout) Command to open fzf window (e.g. `vertical aboveleft 30new` )
332 `window` (Vim 8 / Neovim) | dict | (Layout) Popup window settings (e.g. `{'width': 0.9, 'height': 0.6}` )
333 ---------------------------+---------------+----------------------------------------------------------------------
334
335 `options` entry can be either a string or a list. For simple cases, string
336 should suffice, but prefer to use list type to avoid escaping issues.
337 >
338 call fzf#run({'options': '--reverse --prompt "C:\\Program Files\\"'})
339 call fzf#run({'options': ['--reverse', '--prompt', 'C:\Program Files\']})
340 <
341 When `window` entry is a dictionary, fzf will start in a popup window. The
342 following options are allowed:
343
344 - Required:
345 - `width` [float range [0 ~ 1]] or [integer range [8 ~ ]]
346 - `height` [float range [0 ~ 1]] or [integer range [4 ~ ]]
347 - Optional:
348 - `yoffset` [float default 0.5 range [0 ~ 1]]
349 - `xoffset` [float default 0.5 range [0 ~ 1]]
350 - `relative` [boolean default v:false]
351 - `border` [string default `rounded` (`sharp` on Windows)]: Border style
352 - `rounded` / `sharp` / `horizontal` / `vertical` / `top` / `bottom` / `left` / `right` / `no[ne]`
353
354
355 FZF#WRAP
356 ==============================================================================
357
358 *fzf#wrap*
359
360 We have seen that several aspects of `:FZF` command can be configured with a
361 set of global option variables; different ways to open files (`g:fzf_action`),
362 window position and size (`g:fzf_layout`), color palette (`g:fzf_colors`),
363 etc.
364
365 So how can we make our custom `fzf#run` calls also respect those variables?
366 Simply by "wrapping" the spec dictionary with `fzf#wrap` before passing it to
367 `fzf#run`.
368
369 - `fzf#wrap([name string], [spec dict], [fullscreen bool]) -> (dict)`
370 - All arguments are optional. Usually we only need to pass a spec
371 dictionary.
372 - `name` is for managing history files. It is ignored if `g:fzf_history_dir`
373 is not defined.
374 - `fullscreen` can be either `0` or `1` (default: 0).
375
376 `fzf#wrap` takes a spec and returns an extended version of it (also a
377 dictionary) with additional options for addressing global preferences. You can
378 examine the return value of it like so:
379 >
380 echo fzf#wrap({'source': 'ls'})
381 <
382 After we "wrap" our spec, we pass it to `fzf#run`.
383 >
384 call fzf#run(fzf#wrap({'source': 'ls'}))
385 <
386 Now it supports CTRL-T, CTRL-V, and CTRL-X key bindings (configurable via
387 `g:fzf_action`) and it opens fzf window according to `g:fzf_layout` setting.
388
389 To make it easier to use, let's define `LS` command.
390 >
391 command! LS call fzf#run(fzf#wrap({'source': 'ls'}))
392 <
393 Type `:LS` and see how it works.
394
395 We would like to make `:LS!` (bang version) open fzf in fullscreen, just like
396 `:FZF!`. Add `-bang` to command definition, and use <bang> value to set the
397 last `fullscreen` argument of `fzf#wrap` (see :help <bang>).
398 >
399 " On :LS!, <bang> evaluates to '!', and '!0' becomes 1
400 command! -bang LS call fzf#run(fzf#wrap({'source': 'ls'}, <bang>0))
401 <
402 Our `:LS` command will be much more useful if we can pass a directory argument
403 to it, so that something like `:LS /tmp` is possible.
404 >
405 command! -bang -complete=dir -nargs=? LS
406 \ call fzf#run(fzf#wrap({'source': 'ls', 'dir': <q-args>}, <bang>0))
407 <
408 Lastly, if you have enabled `g:fzf_history_dir`, you might want to assign a
409 unique name to our command and pass it as the first argument to `fzf#wrap`.
410 >
411 " The query history for this command will be stored as 'ls' inside g:fzf_history_dir.
412 " The name is ignored if g:fzf_history_dir is not defined.
413 command! -bang -complete=dir -nargs=? LS
414 \ call fzf#run(fzf#wrap('ls', {'source': 'ls', 'dir': <q-args>}, <bang>0))
415 <
416
417 < Global options supported by fzf#wrap >______________________________________~
418 *fzf-global-options-supported-by-fzf#wrap*
419
420 - `g:fzf_layout`
421 - `g:fzf_action`
422 - Works only when no custom `sink` (or `sinklist`) is provided
423 - Having custom sink usually means that each entry is not an ordinary
424 file path (e.g. name of color scheme), so we can't blindly apply the
425 same strategy (i.e. `tabedit some-color-scheme` doesn't make sense)
426 - `g:fzf_colors`
427 - `g:fzf_history_dir`
428
429
430 TIPS *fzf-tips*
431 ==============================================================================
432
433
434 < fzf inside terminal buffer >________________________________________________~
435 *fzf-inside-terminal-buffer*
436
437
438 When fzf is configured to start in a terminal buffer inside Vim or Neovim, you
439 may find the default ANSI colors to be different. In that case, configure the
440 colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x`
441 in Neovim.
442
443 >
444 " Terminal colors for seoul256 color scheme
445 if has('nvim')
446 let g:terminal_color_0 = '#4e4e4e'
447 let g:terminal_color_1 = '#d68787'
448 let g:terminal_color_2 = '#5f865f'
449 let g:terminal_color_3 = '#d8af5f'
450 let g:terminal_color_4 = '#85add4'
451 let g:terminal_color_5 = '#d7afaf'
452 let g:terminal_color_6 = '#87afaf'
453 let g:terminal_color_7 = '#d0d0d0'
454 let g:terminal_color_8 = '#626262'
455 let g:terminal_color_9 = '#d75f87'
456 let g:terminal_color_10 = '#87af87'
457 let g:terminal_color_11 = '#ffd787'
458 let g:terminal_color_12 = '#add4fb'
459 let g:terminal_color_13 = '#ffafaf'
460 let g:terminal_color_14 = '#87d7d7'
461 let g:terminal_color_15 = '#e4e4e4'
462 else
463 let g:terminal_ansi_colors = [
464 \ '#4e4e4e', '#d68787', '#5f865f', '#d8af5f',
465 \ '#85add4', '#d7afaf', '#87afaf', '#d0d0d0',
466 \ '#626262', '#d75f87', '#87af87', '#ffd787',
467 \ '#add4fb', '#ffafaf', '#87d7d7', '#e4e4e4'
468 \ ]
469 endif
470 <
471
472 < Starting fzf in a Vim popup window >________________________________________~
473 *fzf-starting-fzf-in-a-vim-popup-window*
474
475 You can configure fzf to start in a Vim popup window by setting the `window`
476 key in `g:fzf_layout`.
477 >
478 " Required:
479 " - width [float range [0 ~ 1]] or [integer range [8 ~ ]]
480 " - height [float range [0 ~ 1]] or [integer range [4 ~ ]]
481 "
482 " Optional:
483 " - xoffset [float default 0.5 range [0 ~ 1]]
484 " - yoffset [float default 0.5 range [0 ~ 1]]
485 " - relative [boolean default v:false]
486 " - border [string default 'rounded']: Border style
487 " - 'rounded' / 'sharp' / 'horizontal' / 'vertical' / 'top' / 'bottom' / 'left' / 'right'
488 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }
489 <
490
491 < Starting fzf in a tmux/Zellij popup window >________________________________~
492 *fzf-starting-fzf-in-a-tmuxzellij-popup-window*
493
494 fzf can also start in a popup of the multiplexer instead of a window inside
495 Vim, by putting a `--popup` option value in the `popup` key. `tmux` is
496 accepted as a synonym, just as `--tmux` is an alias of `--popup`.
497
498 The layout works on tmux 3.3 or above, or on Zellij 0.44 or above with fzf
499 0.71.0 or above. It is the default on tmux 3.7 or above with fzf 0.74.0 or
500 above, and on Zellij, where the pane is not modal: Vim keeps the window fzf
501 was started from visible, and you can switch to it while fzf is open. Below
502 those versions tmux gives a popup that cannot be left, so a window inside Vim
503 is the default there. On tmux, an explicit `--border` style also gives a modal
504 popup rather than a floating pane, because the native border of a tmux
505 floating pane cannot be removed. Drop `--border` to keep the floating pane and
506 its native border. Zellij keeps the floating pane either way, and hides its
507 native border when fzf draws one. Set `g:fzf_layout` yourself to choose either
508 one.
509 >
510 if exists('$TMUX') || exists('$ZELLIJ')
511 " See `--popup` option in `man fzf` for available options
512 " [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]
513 let g:fzf_layout = { 'popup': '90%,70%' }
514 else
515 " Configure the Vim popup window in case not on the multiplexer
516 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.7 } }
517 endif
518 <
519
520 < Hide statusline >___________________________________________________________~
521 *fzf-hide-statusline*
522
523 When fzf starts in a terminal buffer, the file type of the buffer is set to
524 `fzf`. So you can set up `FileType fzf` autocmd to customize the settings of
525 the window. This applies to the layouts that open inside Vim, not to the tmux
526 or Zellij pane the default uses, which is not a buffer.
527
528 For example, if you open fzf on the bottom on the screen (e.g. `{'down':
529 '40%'}`), you might want to temporarily disable the statusline for a cleaner
530 look.
531 >
532 let g:fzf_layout = { 'down': '30%' }
533 autocmd! FileType fzf
534 autocmd FileType fzf set laststatus=0 noshowmode noruler
535 \| autocmd BufLeave <buffer> set laststatus=2 showmode ruler
536 <
537
538 LICENSE *fzf-license*
539 ==============================================================================
540
541 The MIT License (MIT)
542
543 Copyright (c) 2013-2026 Junegunn Choi
544
545 ==============================================================================
546 vim:tw=78:sw=2:ts=2:ft=help:norl:nowrap: