[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.
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
Provenance
- Commit
a52ac84- Tree
1c58722- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
52f4319
5 files changed+240 added−49 removed
modified:doc/fzf.txt+56 added-18 removed
12 unmodified lines13 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|21 22 FZF VIM INTEGRATION *fzf-vim-integration*138 unmodified lines161 \ 'ctrl-x': 'split',162 \ 'ctrl-v': 'vsplit' }163 164 " 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+ endif175 167- " - Popup window (center of the current window)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 size182+ 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 170- " - 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 } }189 173- " - down / up / left / right190+ " - Vim split window: down / up / left / right191 let g:fzf_layout = { 'down': '40%' }192 176- " - 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 lines434 < fzf inside terminal buffer >________________________________________________~435 *fzf-inside-terminal-buffer*436 437 421-On the latest versions of Vim and Neovim, fzf will start in a terminal buffer.422-If you find the default ANSI colors to be different, consider configuring the438+When fzf is configured to start in a terminal buffer inside Vim or Neovim, you439+may find the default ANSI colors to be different. In that case, configure the440 colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x`441 in Neovim.442 443 >24 unmodified lines468 \ ]469 endif470 <471 455-< 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*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 ~ ]]5 unmodified lines486 " - 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 <470-Alternatively, you can make fzf open in a popup window (requires tmux 3.3 or471-above, 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`.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 inside495+Vim, by putting a `--popup` option value in the `popup` key. `tmux` is496+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 fzf499+0.71.0 or above. It is the default on tmux 3.7 or above with fzf 0.74.0 or500+above, and on Zellij, where the pane is not modal: Vim keeps the window fzf501+was started from visible, and you can switch to it while fzf is open. Below502+those versions tmux gives a popup that cannot be left, so a window inside Vim503+is the default there. On tmux, an explicit `--border` style also gives a modal504+popup rather than a floating pane, because the native border of a tmux505+floating pane cannot be removed. Drop `--border` to keep the floating pane and506+its native border. Zellij keeps the floating pane either way, and hides its507+native border when fzf draws one. Set `g:fzf_layout` yourself to choose either508+one.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 <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 to524 `fzf`. So you can set up `FileType fzf` autocmd to customize the settings of488-the window.525+the window. This applies to the layouts that open inside Vim, not to the tmux526+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 cleaner530 look.16 unmodified linesmodified:plugin/fzf.vim+81 added-12 removed
138 unmodified lines139 return has('nvim') ? has('nvim-0.4') : has('popupwin') && has('patch-8.2.191')140 endfunction141 142 function! 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%' }155 endfunction17 unmodified lines173 call system(script)174 if v:shell_error175 throw 'Failed to download fzf: '.script176 endif177+178+ " 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_floating183 endfunction184 185 let s:versions = {}186 function s:get_version(bin)84 unmodified lines271 return s:exec272 endfunction273 274 " 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.276 function! s:fzf_tmux_script()277 if !executable(s:fzf_tmux)278 if !executable('fzf-tmux')279 return ''7 unmodified lines287 if has('gui_running')288 return 0289 endif290 276- " --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- endif282-283- if !exists('$TMUX')291+ if empty($TMUX) && empty($ZELLIJ)292 return 0293 endif294 295 if exists('s:tmux')296 return s:tmux297 endif298 299+ let [s:tmux, s:tmux_floating] = [0, 0]300+301+ " --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+ endif313+314 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\+')320 299- " --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 endif332 333 " Older versions still go through the script144 unmodified lines478 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 endif488 489 " Colors: g:fzf_colors167 unmodified lines657 endfor658 return 0659 endfunction660 661+" The binary fzf#exec() would choose, without its prompting or installing.662+" Layout selection runs before fzf#exec() has resolved one663+function! 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]672+endfunction673+674+function! 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_ver681+endfunction682+683+" 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 here689+function! 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'697+endfunction698+699 function! 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 linesmodified:test/vim/fzf.vader+27 added
100 unmodified lines101 call g:FzfRun({'source': ['/foobar'], 'sink': 'e', 'dir': '/', 'options': '-1'})102 " Working directory changed due to &acd103 AssertEqual '/', getcwd()104 105+Execute (Default layout):106+ unlet! g:fzf_layout g:fzf_height107+ let layout_keys = ['window', 'popup', 'tmux', 'up', 'down', 'left', 'right']108+109+ 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+ endif123+124+ " 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)')128+129 Execute (fzf#wrap):130 AssertThrows fzf#wrap({'foo': 'bar'})131 132+ " Pin the layout so the assertions do not depend on the environment133+ let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }134+135 let opts = fzf#wrap('foobar')136 Log opts137 AssertEqual 0.9, opts.window.width138 Assert opts.options =~ '--expect='117 unmodified linesmodified:CHANGELOG.md+3 added
12 unmodified lines13 - 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 layout20 21 0.74.322 ------23 - Performance optimizations for non-ASCII input3421 unmodified linesmodified:README-VIM.md+73 added-19 removed
1 FZF Vim integration2 ===================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 Installation25 ------------26 27 Once you have fzf installed, you can enable it inside Vim simply by adding the124 unmodified lines152 \ 'ctrl-x': 'split',153 \ 'ctrl-v': 'vsplit' }154 155 " Default fzf layout156+if 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%' }161+else162+ " If --popup option is not available, it will open in a popup window inside136-" - Popup window (center of the screen)137-let 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 } }165+endif166 139-" - Popup window (center of the current window)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 size173+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 142-" - Popup window (anchored to the bottom of the current window)178+" - Vim popup window: anchored to the bottom of the current window179 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true, 'yoffset': 1.0 } }180 145-" - down / up / left / right181+" - Vim split window: down / up / left / right182 let g:fzf_layout = { 'down': '40%' }183 148-" - Window using a Vim command184+" - Vim window using a Vim command185 let g:fzf_layout = { 'window': 'enew' }186 let g:fzf_layout = { 'window': '-tabnew' }187 let g:fzf_layout = { 'window': '10new' }188 254 unmodified lines443 ----444 445 ### fzf inside terminal buffer446 411-On the latest versions of Vim and Neovim, fzf will start in a terminal buffer.412-If you find the default ANSI colors to be different, consider configuring the413-colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x`414-in Neovim.447+When fzf is configured to start in a terminal buffer inside Vim or Neovim, you448+may find the default ANSI colors to be different. In that case, configure the449+colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x` in450+Neovim.451 452 ```vim453 " Terminal colors for seoul256 color scheme454 if has('nvim')22 unmodified lines477 \ ]478 endif479 ```480 445-### Starting fzf in a popup window481+### Starting fzf in a Vim popup window482 483+You can configure fzf to start in a Vim popup window by setting the `window` key484+in `g:fzf_layout`.485+486 ```vim487 " Required:488 " - width [float range [0 ~ 1]] or [integer range [8 ~ ]]489 " - height [float range [0 ~ 1]] or [integer range [4 ~ ]]6 unmodified lines496 " - 'rounded' / 'sharp' / 'horizontal' / 'vertical' / 'top' / 'bottom' / 'left' / 'right'497 let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }498 ```499 461-Alternatively, you can make fzf open in a popup window (requires tmux 3.3 or462-above, or Zellij 0.44 or above) by putting `--popup` option value in `popup`463-key. `tmux` is accepted as a synonym, just as `--tmux` is an alias of464-`--popup`.500+### Starting fzf in a tmux/Zellij popup window501 502+fzf can also start in a popup of the multiplexer instead of a window inside503+Vim, by putting a `--popup` option value in the `popup` key. `tmux` is504+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 fzf507+0.71.0 or above. It is the default on tmux 3.7 or above with fzf 0.74.0 or508+above, and on Zellij, where the pane is not modal: Vim keeps the window fzf509+was started from visible, and you can switch to it while fzf is open. Below510+those versions tmux gives a popup that cannot be left, so a window inside Vim511+is the default there. On tmux, an explicit `--border` style also gives a modal512+popup rather than a floating pane, because the native border of a tmux513+floating pane cannot be removed. Drop `--border` to keep the floating pane and514+its native border. Zellij keeps the floating pane either way, and hides its515+native border when fzf draws one. Set `g:fzf_layout` yourself to choose either516+one.517+518 ```vim467-" See `--popup` option in `man fzf` for available options468-" [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]519 if 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%' }523 else524+ " 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 } }526 endif527 ```528 529 ### Hide statusline530 531 When 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 of480-the window.533+the window. This applies to the layouts that open inside Vim, not to the tmux534+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 cleaner538 look.14 unmodified lines