Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The window fzf was started from stays visible, and can be used while fzf runs, which a popup inside Vim cannot offer. Only where the pane is not modal, so tmux 3.7 or above, or Zellij 0.44 or above with fzf 0.71.0 or above. A tmux popup from 3.3 to 3.6 cannot be left, and below 3.3 fzf goes through the fzf-tmux script, whose options are spelled differently. Both keep the window inside Vim. An explicit popup layout is unaffected and still works from 3.3. fzf checks tmux before Zellij, so tmux wins when both are set. g:fzf_layout still wins, so anyone who set it sees no change.

authored by Junegunn Choi · committed by GitHub · parent 52f4319 · Browse files · 5 files+240 added−49 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • g: fzf_layout still wins, so anyone who set it sees no change.

Diff stats

5 files+240 added−49 removed

  • tests:+27 added−0 removed
  • other:+213 added−49 removed

5 files+240 added−49 removed

modified:doc/fzf.txt+56 added-18 removed
12 unmodified lines
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 popup window |fzf-starting-fzf-in-a-popup-window|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*
138 unmodified lines
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 inside164 " - Popup window (center of the screen)165 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }172 " Vim (center of the screen)173 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }174 endif175167 " - Popup window (center of the current window)176 " 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 } }186170 " - Popup window (anchored to the bottom of the current window)187 " - 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 } }189173 " - down / up / left / right190 " - Vim split window: down / up / left / right191 let g:fzf_layout = { 'down': '40%' }192176 " - Window using a Vim command193 " - 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' }197
236 unmodified lines
434< fzf inside terminal buffer >________________________________________________~435 *fzf-inside-terminal-buffer*436437421On the latest versions of Vim and Neovim, fzf will start in a terminal buffer.422If you find the default ANSI colors to be different, consider configuring the438When 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>
24 unmodified lines
468 \ ]469 endif470<471455< Starting fzf in a popup window >____________________________________________~456 *fzf-starting-fzf-in-a-popup-window*472< 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 ~ ]]
5 unmodified lines
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<470Alternatively, you can make fzf open in a popup window (requires tmux 3.3 or471above, or Zellij 0.44 or above) by putting `--popup` options in `popup` key.472`tmux` is accepted as a synonym, just as `--tmux` is an alias of `--popup`.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>474 " See `--popup` option in `man fzf` for available options475 " [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]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 multiplexer479 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }516 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 of488the window.525the 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.
16 unmodified lines
modified:plugin/fzf.vim+81 added-12 removed
138 unmodified lines
139 return has('nvim') ? has('nvim-0.4') : has('popupwin') && has('patch-8.2.191')140endfunction141142function! s:default_layout()143 " A floating pane leaves the window fzf was started from visible and144 " reachable while fzf is open. A popup covers it, inside Vim or not.145 " Without a job, s:execute_tmux() blocks and freezes Vim. fzf also wants a146 " tmux window of at least 3x3, and Vim's pane is never larger than the147 " window, so asking Vim is free and survives a resize148 if (has('nvim') || has('job')) && &columns >= 3 && &lines >= 3149 \ && s:tmux_enabled() && get(s:, 'tmux_floating', 0)150 return { 'tmux': '90%,60%' }151 endif152 return s:popup_support()153 \ ? { 'window' : { 'width': 0.9, 'height': 0.6 } }154 \ : { 'down': '~40%' }155endfunction
17 unmodified lines
173 call system(script)174 if v:shell_error175 throw 'Failed to download fzf: '.script176 endif177178 " A new binary invalidates the chosen executable and everything derived from179 " its version, including whether fzf opens a floating pane. fzf#install() is180 " also the vim-plug 'do' hook, so this can run long after the first fzf call181 let [s:versions, s:checked] = [{}, {}]182 unlet! s:exec s:tmux s:tmux_floating183endfunction184185let s:versions = {}186function s:get_version(bin)
84 unmodified lines
271 return s:exec272endfunction273274" Path to the fzf-tmux script, or an empty string if it is not available. Only260" the legacy options still need it. --tmux is handled by fzf itself.275" the legacy options still need it. --popup is handled by fzf itself.276function! s:fzf_tmux_script()277 if !executable(s:fzf_tmux)278 if !executable('fzf-tmux')279 return ''
7 unmodified lines
287 if has('gui_running')288 return 0289 endif290276 " --tmux covers Zellij as well, where the fzf-tmux script and the tmux277 " version are irrelevant, but the binary only learned it in 0.71.0278 if exists('$ZELLIJ')279 return exists('s:exec')280 \ && s:compare_versions(s:get_version(s:exec), '0.71.0') >= 0281 endif282283 if !exists('$TMUX')291 if empty($TMUX) && empty($ZELLIJ)292 return 0293 endif294295 if exists('s:tmux')296 return s:tmux297 endif298299 let [s:tmux, s:tmux_floating] = [0, 0]300301 " --popup covers Zellij as well, where the fzf-tmux script and the tmux302 " version are irrelevant. fzf learned it in 0.71.0, and the floating pane303 " options it passes need Zellij 0.44 or above. fzf checks tmux first, so304 " this branch is Zellij without tmux. Both non-empty means tmux wins.305 " empty(), not exists(), to match how fzf reads the two variables306 if empty($TMUX)291 let s:tmux = 0307 let s:tmux =308 \ s:compare_versions(s:get_version(s:fzf_binary()), '0.71.0') >= 0309 \ && s:compare_versions(s:zellij_version(), '0.44') >= 0310 let s:tmux_floating = s:tmux311 return s:tmux312 endif313314 let output = system('tmux -V')315 if v:shell_error316 return s:tmux317 endif318 " e.g. 'tmux 3.7b', 'tmux next-3.8'319 let ver = matchstr(output, '\d\+\.\d\+')320299 " --tmux requires tmux 3.3 or above, and needs no fzf-tmux script321 " --popup requires tmux 3.3 or above, and needs no fzf-tmux script. The322 " default layout wants a floating pane, which also needs fzf 0.74.0 or323 " above. fzf opens a modal popup otherwise. The version here only skips the324 " probe for servers too old to answer it325 if s:compare_versions(ver, '3.3') >= 0326 let s:tmux = 1327 let s:tmux_floating = s:compare_versions(ver, '3.7') >= 0328 \ && s:tmux_floating_pane_support()329 \ && s:compare_versions(s:get_version(s:fzf_binary()), '0.74.0') >= 0330 return s:tmux331 endif332333 " Older versions still go through the script
144 unmodified lines
478 elseif !s:has_any(opts, s:layout_keys)479 if !exists('g:fzf_layout') && exists('g:fzf_height')480 let opts.down = g:fzf_height481 else454 let opts = extend(opts, s:validate_layout(get(g:, 'fzf_layout', s:default_layout())))482 " Not get(), which would evaluate s:default_layout() and run its version483 " checks even when g:fzf_layout makes the answer irrelevant484 let opts = extend(opts, s:validate_layout(485 \ exists('g:fzf_layout') ? g:fzf_layout : s:default_layout()))486 endif487 endif488489 " Colors: g:fzf_colors
167 unmodified lines
657 endfor658 return 0659endfunction660661" The binary fzf#exec() would choose, without its prompting or installing.662" Layout selection runs before fzf#exec() has resolved one663function! s:fzf_binary()664 if exists('s:exec')665 return s:exec666 endif667 let bins = filter(['fzf', s:fzf_go], 'executable(v:val)')668 if empty(bins)669 return ''670 endif671 return len(bins) > 1 ? sort(bins, 's:compare_binary_versions')[-1] : bins[0]672endfunction673674function! s:zellij_version()675 if !exists('s:zellij_ver')676 let output = systemlist('zellij --version')677 let s:zellij_ver = v:shell_error || empty(output)678 \ ? '' : matchstr(output[0], '[0-9.]\+')679 endif680 return s:zellij_ver681endfunction682683" Whether the running server can put fzf in a floating pane. fzf decides on684" the server, not on the version the tmux client reports, so ask it the same685" question rather than predicting the answer. See tmuxFloatingPaneInfo in686" src/tmux.go, which also requires the tmux window to be at least 3x3; that687" one changes with a resize, so s:default_layout() reads it from Vim instead688" of caching it here689function! s:tmux_floating_pane_support()690 " fzf does not use a floating pane when it was not started from a pane691 if empty($TMUX_PANE)692 return 0693 endif694 let out = system('tmux list-commands new-pane')695 " A server that does not know the command exits normally with no output696 return !v:shell_error && out =~# 'new-pane'697endfunction698699function! s:fzf_tmux(dict)700 let size = get(a:dict, 'tmux', '')701 if empty(size)702 for o in ['up', 'down', 'left', 'right']
619 unmodified lines
modified:test/vim/fzf.vader+27 added
100 unmodified lines
101 call g:FzfRun({'source': ['/foobar'], 'sink': 'e', 'dir': '/', 'options': '-1'})102 " Working directory changed due to &acd103 AssertEqual '/', getcwd()104105Execute (Default layout):106 unlet! g:fzf_layout g:fzf_height107 let layout_keys = ['window', 'popup', 'tmux', 'up', 'down', 'left', 'right']108109 let opts = fzf#wrap('foobar')110 Log opts111 let found = filter(copy(layout_keys), 'has_key(opts, v:val)')112 AssertEqual 1, len(found)113 if found[0] ==# 'tmux'114 " Only where fzf opens a floating pane, which can be left while fzf runs115 Assert !empty($TMUX) || !empty($ZELLIJ)116 AssertEqual '90%,60%', opts.tmux117 elseif found[0] ==# 'window'118 AssertEqual 0.9, opts.window.width119 else120 " No popup support in this build121 AssertEqual '~40%', opts.down122 endif123124 " Fullscreen strips it, whichever it was125 let opts = fzf#wrap('foobar', {}, 1)126 Log opts127 AssertEqual [], filter(copy(layout_keys), 'has_key(opts, v:val)')128129Execute (fzf#wrap):130 AssertThrows fzf#wrap({'foo': 'bar'})131132 " Pin the layout so the assertions do not depend on the environment133 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }134135 let opts = fzf#wrap('foobar')136 Log opts137 AssertEqual 0.9, opts.window.width138 Assert opts.options =~ '--expect='
117 unmodified lines
modified:CHANGELOG.md+3 added
12 unmodified lines
13 - Added `popup` as a synonym of the `tmux` layout key14 ```vim15 let g:fzf_layout = { 'popup': '90%,70%' }16 ```17 - fzf now opens in a tmux or Zellij floating pane by default, so the window it was started from stays visible and can be used while fzf is running18 - Requires tmux 3.7+ or Zellij 0.44+19 - Set `g:fzf_layout` to pick a different layout20210.74.322------23- Performance optimizations for non-ASCII input
3421 unmodified lines
modified:README-VIM.md+73 added-19 removed
1FZF Vim integration2===================34<!-- vim-markdown-toc GFM -->56* [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)2122<!-- vim-markdown-toc -->2324Installation25------------2627Once you have fzf installed, you can enable it inside Vim simply by adding the
124 unmodified lines
152 \ 'ctrl-x': 'split',153 \ 'ctrl-v': 'vsplit' }154155" Default fzf layout156if exists('$TMUX') || exists('$ZELLIJ')157 " The Vim plugin will try to open fzf in a tmux or Zellij popup158 " if possible (requires recent fzf and tmux/zellij) using --popup option,159 " with the following argument:160 let g:fzf_layout = { 'popup': '90%,60%' }161else162 " If --popup option is not available, it will open in a popup window inside136" - Popup window (center of the screen)137let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }163 " Vim (center of the screen)164 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }165endif166139" - Popup window (center of the current window)167" Here are some more layout examples:168169" - Tmux or Zellij popup at the bottom 40%170let g:fzf_layout = { 'popup': 'bottom,40%' }171172" - Tmux or Zellij popup at the top with a different size173let g:fzf_layout = { 'popup': 'top,90%,40%' }174175" - Vim popup window: at the center of the current window (relative)176let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true } }177142" - Popup window (anchored to the bottom of the current window)178" - Vim popup window: anchored to the bottom of the current window179let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true, 'yoffset': 1.0 } }180145" - down / up / left / right181" - Vim split window: down / up / left / right182let g:fzf_layout = { 'down': '40%' }183148" - Window using a Vim command184" - Vim window using a Vim command185let g:fzf_layout = { 'window': 'enew' }186let g:fzf_layout = { 'window': '-tabnew' }187let g:fzf_layout = { 'window': '10new' }188
254 unmodified lines
443----444445### fzf inside terminal buffer446411On the latest versions of Vim and Neovim, fzf will start in a terminal buffer.412If you find the default ANSI colors to be different, consider configuring the413colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x`414in Neovim.447When fzf is configured to start in a terminal buffer inside Vim or Neovim, you448may find the default ANSI colors to be different. In that case, configure the449colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x` in450Neovim.451452```vim453" Terminal colors for seoul256 color scheme454if has('nvim')
22 unmodified lines
477 \ ]478endif479```480445### Starting fzf in a popup window481### Starting fzf in a Vim popup window482483You can configure fzf to start in a Vim popup window by setting the `window` key484in `g:fzf_layout`.485486```vim487" Required:488" - width [float range [0 ~ 1]] or [integer range [8 ~ ]]489" - height [float range [0 ~ 1]] or [integer range [4 ~ ]]
6 unmodified lines
496" - 'rounded' / 'sharp' / 'horizontal' / 'vertical' / 'top' / 'bottom' / 'left' / 'right'497let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }498```499461Alternatively, you can make fzf open in a popup window (requires tmux 3.3 or462above, or Zellij 0.44 or above) by putting `--popup` option value in `popup`463key. `tmux` is accepted as a synonym, just as `--tmux` is an alias of464`--popup`.500### Starting fzf in a tmux/Zellij popup window501502fzf can also start in a popup of the multiplexer instead of a window inside503Vim, by putting a `--popup` option value in the `popup` key. `tmux` is504accepted as a synonym, just as `--tmux` is an alias of `--popup`.505506The layout works on tmux 3.3 or above, or on Zellij 0.44 or above with fzf5070.71.0 or above. It is the default on tmux 3.7 or above with fzf 0.74.0 or508above, and on Zellij, where the pane is not modal: Vim keeps the window fzf509was started from visible, and you can switch to it while fzf is open. Below510those versions tmux gives a popup that cannot be left, so a window inside Vim511is the default there. On tmux, an explicit `--border` style also gives a modal512popup rather than a floating pane, because the native border of a tmux513floating pane cannot be removed. Drop `--border` to keep the floating pane and514its native border. Zellij keeps the floating pane either way, and hides its515native border when fzf draws one. Set `g:fzf_layout` yourself to choose either516one.517518```vim467" See `--popup` option in `man fzf` for available options468" [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]519if exists('$TMUX') || exists('$ZELLIJ')520 " See `--popup` option in `man fzf` for available options521 " [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]522 let g:fzf_layout = { 'popup': '90%,70%' }523else524 " Configure the Vim popup window in case not on the multiplexer472 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }525 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.7 } }526endif527```528529### Hide statusline530531When fzf starts in a terminal buffer, the file type of the buffer is set to532`fzf`. So you can set up `FileType fzf` autocmd to customize the settings of480the window.533the window. This applies to the layouts that open inside Vim, not to the tmux534or Zellij pane the default uses, which is not a buffer.535536For 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 cleaner538look.
14 unmodified lines