Skip to content
Myr

fzf.txt

This file

Path

doc/fzf.txt

546 lines · 23.0 KiB

Last changed by

[vim] Open fzf in a tmux or Zellij floating pane by default (#4905)Junegunn Choibefore adoptiona52ac84

History of this file

Take it

1fzf.txt	fzf	Last change: February 15 20242FZF - TABLE OF CONTENTS                                            *fzf* *fzf-toc*3==============================================================================45  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|2122FZF VIM INTEGRATION                                        *fzf-vim-integration*23==============================================================================242526INSTALLATION                                                  *fzf-installation*27==============================================================================2829Once you have fzf installed, you can enable it inside Vim simply by adding the30directory to 'runtimepath' in your Vim configuration file. The path may differ31depending on the package manager.32>33    " If installed using Homebrew34    set rtp+=/usr/local/opt/fzf3536    " If installed using Homebrew on Apple Silicon37    set rtp+=/opt/homebrew/opt/fzf3839    " If you have cloned fzf on ~/.fzf directory40    set rtp+=~/.fzf41<42If you use {vim-plug}{1}, the same can be written as:43>44    " If installed using Homebrew45    Plug '/usr/local/opt/fzf'4647    " If installed using Homebrew on Apple Silicon48    Plug '/opt/homebrew/opt/fzf'4950    " If you have cloned fzf on ~/.fzf directory51    Plug '~/.fzf'52<53But if you want the latest Vim plugin file from GitHub rather than the one54included in the package, write:55>56    Plug 'junegunn/fzf'57<58The Vim plugin will pick up fzf binary available on the system. If fzf is not59found on `$PATH`, it will ask you if it should download the latest binary for60you.6162To make sure that you have the latest version of the binary, set up63post-update hook like so:6465                                                                   *fzf#install*66>67    Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }68<69                                      {1} https://github.com/junegunn/vim-plug707172SUMMARY                                                            *fzf-summary*73==============================================================================7475The Vim plugin of fzf provides two core functions, and `:FZF` command which is76the basic file selector command built on top of them.7778 1. `fzf#run([spec dict])`79   - Starts fzf inside Vim with the given spec80   - `: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 with83     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 selector89   - A reference implementation for those who don't want to write VimScript to90     implement custom commands91   - If you're looking for more such commands, check out {fzf.vim}{2} project.9293The most important of all is `fzf#run`, but it would be easier to understand94the whole if we start off with `:FZF` command.9596                                       {2} https://github.com/junegunn/fzf.vim979899:FZF[!]100==============================================================================101102                                                                          *:FZF*103>104    " Look for files under current directory105    :FZF106107    " Look for files under your home directory108    :FZF ~109110    " With fzf command-line options111    :FZF --reverse --info=inline /tmp112113    " Bang version starts fzf in fullscreen mode114    :FZF!115<116Similarly to {ctrlp.vim}{3}, use Enter key, CTRL-T, CTRL-X or CTRL-V to open117selected files in the current window, in new tabs, in horizontal splits, or in118vertical splits respectively.119120Note that the environment variables `FZF_DEFAULT_COMMAND` and121`FZF_DEFAULT_OPTS` also apply here.122123                                         {3} https://github.com/kien/ctrlp.vim124125126< Configuration >_____________________________________________________________~127                                                             *fzf-configuration*128129                      *g:fzf_action* *g:fzf_layout* *g:fzf_colors* *g:fzf_history_dir*130131 - `g:fzf_action`132   - Customizable extra key bindings for opening selected files in different133     ways134 - `g:fzf_layout`135   - Determines the size and position of fzf window136 - `g:fzf_colors`137   - Customizes fzf colors to match the current color scheme138 - `g:fzf_history_dir`139   - Enables history feature140141142Examples~143                                                                  *fzf-examples*144>145    " This is the default extra key bindings146    let g:fzf_action = {147      \ 'ctrl-t': 'tab split',148      \ 'ctrl-x': 'split',149      \ 'ctrl-v': 'vsplit' }150151    " An action can be a reference to a function that processes selected lines152    function! s:build_quickfix_list(lines)153      call setqflist(map(copy(a:lines), '{ "filename": v:val, "lnum": 1 }'))154      copen155      cc156    endfunction157158    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' }163164    " Default fzf layout165    if exists('$TMUX') || exists('$ZELLIJ')166      " The Vim plugin will try to open fzf in a tmux or Zellij popup167      " 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    else171      " If --popup option is not available, it will open in a popup window inside172      " Vim (center of the screen)173      let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }174    endif175176    " Here are some more layout examples:177178    " - Tmux or Zellij popup at the bottom 40%179    let g:fzf_layout = { 'popup': 'bottom,40%' }180181    " - Tmux or Zellij popup at the top with a different size182    let g:fzf_layout = { 'popup': 'top,90%,40%' }183184    " - 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 } }186187    " - Vim popup window: anchored to the bottom of the current window188    let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true, 'yoffset': 1.0 } }189190    " - Vim split window: down / up / left / right191    let g:fzf_layout = { 'down': '40%' }192193    " - Vim window using a Vim command194    let g:fzf_layout = { 'window': 'enew' }195    let g:fzf_layout = { 'window': '-tabnew' }196    let g:fzf_layout = { 'window': '10new' }197198    " Customize fzf colors to match your color scheme199    " - fzf#wrap translates this to a set of `--color` options200    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'] }214215    " Enable per-command history216    " - History files will be stored in the specified directory217    " - When set, CTRL-N and CTRL-P will be bound to 'next-history' and218    "   'previous-history' instead of 'down' and 'up'.219    let g:fzf_history_dir = '~/.local/share/fzf-history'220<221222Explanation of g:fzf_colors~223                                                 *fzf-explanation-of-gfzfcolors*224225`g:fzf_colors` is a dictionary mapping fzf elements to a color specification226list:227>228    element: [ component, group1 [, group2, ...] ]229<230 - `element` is an fzf element to apply a color to:231232 ----------------------------+------------------------------------------------------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 background238  `gutter`                     | Background of the gutter on the left239  `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 indicator245  `query`                      | Query string246  `disabled`                   | Query string when search is disabled247  `prompt`                     | Prompt before query ( `> ` )248 ----------------------------+------------------------------------------------------249 - `component` specifies the component (`fg` / `bg`) from which to extract the250   color when considering each of the following highlight groups251 - `group1 [, group2, ...]` is a list of highlight groups that are searched (in252   order) for a matching color definition253254For example, consider the following specification:255>256      'prompt':  ['fg', 'Conditional', 'Comment'],257<258This means we color the prompt - using the `fg` attribute of the `Conditional`259if it exists, - otherwise use the `fg` attribute of the `Comment` highlight260group if it exists, - otherwise fall back to the default color settings for261the prompt.262263You can examine the color option generated according to the setting by printing264the result of `fzf#wrap()` function like so:265>266    :echo fzf#wrap()267<268269FZF#RUN270==============================================================================271272                                                                       *fzf#run*273274`fzf#run()` function is the core of Vim integration. It takes a single275dictionary argument, a spec, and starts fzf process accordingly. At the very276least, specify `sink` option to tell what it should do with the selected277entry.278>279    call fzf#run({'sink': 'e'})280<281We haven't specified the `source`, so this is equivalent to starting fzf on282command line without standard input pipe; fzf will traverse the file system283under the current directory to get the list of files. (If284`$FZF_DEFAULT_COMMAND` is set, fzf will use the output of the command285instead.) When you select one, it will open it with the sink, `:e` command. If286you want to open it in a new tab, you can pass `:tabedit` command instead as287the sink.288>289    call fzf#run({'sink': 'tabedit'})290<291You can use any shell command as the source to generate the list. The292following example will list the files managed by git. It's equivalent to293running `git ls-files | fzf` on shell.294>295    call fzf#run({'source': 'git ls-files', 'sink': 'e'})296<297fzf options can be specified as `options` entry in spec dictionary.298>299    call fzf#run({'sink': 'tabedit', 'options': '--multi --reverse'})300<301You can also pass a layout option if you don't want fzf window to take up the302entire screen.303>304    " up / down / left / right / window are allowed305    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 Vim309array as the source. In the next example, we pass the names of color schemes310as 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<316The following table summarizes the available options.317318 ---------------------------+---------------+----------------------------------------------------------------------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 fzf323  `sink`                      | string        | Vim command to handle the selected item (e.g.  `e` ,  `tabe` )324  `sink`                      | funcref       | Function to be called with each selected item325  `sinklist`  (or  `sink*` )    | funcref       | Similar to  `sink` , but takes the list of output lines at once326  `exit`                      | funcref       | Function to be called with the exit status of fzf (e.g. 0, 1, 2, 130)327  `options`                   | string/list   | Options to fzf328  `dir`                       | string        | Working directory329  `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 ---------------------------+---------------+----------------------------------------------------------------------334335`options` entry can be either a string or a list. For simple cases, string336should 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<341When `window` entry is a dictionary, fzf will start in a popup window. The342following options are allowed:343344 - 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 style352     - `rounded` / `sharp` / `horizontal` / `vertical` / `top` / `bottom` / `left` / `right` / `no[ne]`353354355FZF#WRAP356==============================================================================357358                                                                      *fzf#wrap*359360We have seen that several aspects of `:FZF` command can be configured with a361set of global option variables; different ways to open files (`g:fzf_action`),362window position and size (`g:fzf_layout`), color palette (`g:fzf_colors`),363etc.364365So how can we make our custom `fzf#run` calls also respect those variables?366Simply by "wrapping" the spec dictionary with `fzf#wrap` before passing it to367`fzf#run`.368369 - `fzf#wrap([name string], [spec dict], [fullscreen bool]) -> (dict)`370   - All arguments are optional. Usually we only need to pass a spec371     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).375376`fzf#wrap` takes a spec and returns an extended version of it (also a377dictionary) with additional options for addressing global preferences. You can378examine the return value of it like so:379>380    echo fzf#wrap({'source': 'ls'})381<382After we "wrap" our spec, we pass it to `fzf#run`.383>384    call fzf#run(fzf#wrap({'source': 'ls'}))385<386Now it supports CTRL-T, CTRL-V, and CTRL-X key bindings (configurable via387`g:fzf_action`) and it opens fzf window according to `g:fzf_layout` setting.388389To make it easier to use, let's define `LS` command.390>391    command! LS call fzf#run(fzf#wrap({'source': 'ls'}))392<393Type `:LS` and see how it works.394395We would like to make `:LS!` (bang version) open fzf in fullscreen, just like396`:FZF!`. Add `-bang` to command definition, and use <bang> value to set the397last `fullscreen` argument of `fzf#wrap` (see :help <bang>).398>399    " On :LS!, <bang> evaluates to '!', and '!0' becomes 1400    command! -bang LS call fzf#run(fzf#wrap({'source': 'ls'}, <bang>0))401<402Our `:LS` command will be much more useful if we can pass a directory argument403to it, so that something like `:LS /tmp` is possible.404>405    command! -bang -complete=dir -nargs=? LS406        \ call fzf#run(fzf#wrap({'source': 'ls', 'dir': <q-args>}, <bang>0))407<408Lastly, if you have enabled `g:fzf_history_dir`, you might want to assign a409unique 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=? LS414        \ call fzf#run(fzf#wrap('ls', {'source': 'ls', 'dir': <q-args>}, <bang>0))415<416417< Global options supported by fzf#wrap >______________________________________~418                                      *fzf-global-options-supported-by-fzf#wrap*419420 - `g:fzf_layout`421 - `g:fzf_action`422   - Works only when no custom `sink` (or `sinklist`) is provided423     - Having custom sink usually means that each entry is not an ordinary424       file path (e.g. name of color scheme), so we can't blindly apply the425       same strategy (i.e. `tabedit some-color-scheme` doesn't make sense)426 - `g:fzf_colors`427 - `g:fzf_history_dir`428429430TIPS                                                                  *fzf-tips*431==============================================================================432433434< fzf inside terminal buffer >________________________________________________~435                                                    *fzf-inside-terminal-buffer*436437438When fzf is configured to start in a terminal buffer inside Vim or Neovim, you439may find the default ANSI colors to be different. In that case, configure the440colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x`441in Neovim.442443>444    " Terminal colors for seoul256 color scheme445    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    else463      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    endif470<471472< Starting fzf in a Vim popup window >________________________________________~473                                        *fzf-starting-fzf-in-a-vim-popup-window*474475You can configure fzf to start in a Vim popup window by setting the `window`476key 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 style487    "   - 'rounded' / 'sharp' / 'horizontal' / 'vertical' / 'top' / 'bottom' / 'left' / 'right'488    let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }489<490491< Starting fzf in a tmux/Zellij popup window >________________________________~492                                 *fzf-starting-fzf-in-a-tmuxzellij-popup-window*493494fzf can also start in a popup of the multiplexer instead of a window inside495Vim, by putting a `--popup` option value in the `popup` key. `tmux` is496accepted as a synonym, just as `--tmux` is an alias of `--popup`.497498The layout works on tmux 3.3 or above, or on Zellij 0.44 or above with fzf4990.71.0 or above. It is the default on tmux 3.7 or above with fzf 0.74.0 or500above, and on Zellij, where the pane is not modal: Vim keeps the window fzf501was started from visible, and you can switch to it while fzf is open. Below502those versions tmux gives a popup that cannot be left, so a window inside Vim503is the default there. On tmux, an explicit `--border` style also gives a modal504popup rather than a floating pane, because the native border of a tmux505floating pane cannot be removed. Drop `--border` to keep the floating pane and506its native border. Zellij keeps the floating pane either way, and hides its507native border when fzf draws one. Set `g:fzf_layout` yourself to choose either508one.509>510    if exists('$TMUX') || exists('$ZELLIJ')511      " See `--popup` option in `man fzf` for available options512      " [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]513      let g:fzf_layout = { 'popup': '90%,70%' }514    else515      " Configure the Vim popup window in case not on the multiplexer516      let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.7 } }517    endif518<519520< Hide statusline >___________________________________________________________~521                                                           *fzf-hide-statusline*522523When fzf starts in a terminal buffer, the file type of the buffer is set to524`fzf`. So you can set up `FileType fzf` autocmd to customize the settings of525the window. This applies to the layouts that open inside Vim, not to the tmux526or Zellij pane the default uses, which is not a buffer.527528For 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 cleaner530look.531>532    let g:fzf_layout = { 'down': '30%' }533    autocmd! FileType fzf534    autocmd  FileType fzf set laststatus=0 noshowmode noruler535      \| autocmd BufLeave <buffer> set laststatus=2 showmode ruler536<537538LICENSE                                                            *fzf-license*539==============================================================================540541The MIT License (MIT)542543Copyright (c) 2013-2026 Junegunn Choi544545==============================================================================546vim:tw=78:sw=2:ts=2:ft=help:norl:nowrap: