Debian Codegraph @

README-VIM.md

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