Following up on the previous entry about lua in vim, in this one I document other ways in which I used lua to improve my setup.

Anyone using Neovim should know that it has support for floating windows: real windows that can float around. You can do all kinds of cool stuff with it. Here it is in action:

So this is how the story begins: one day my friend, and teammate, Miguel was showing me something on his computer, when I saw him doing something like this:

He was opening fzf inside a floating window! I immediately ran to my desk and copied the code that does this from his dotfiles. The relevant parts are here and here. He got it somewhere from the Github.

I was in love with this for a while!… Until I realized that it didn’t work great when I was using Vim in smaller tmux splits. I need something more “intelligent” that took into account the editor’s size. Since my VimScript skills are non-existent, I replaced his implementation with Lua.

Here’s what I want:

if the editor is small (when I’m in a split with 1/4 of the screen’s size) it uses a full window instead of a floating one.

the floating window’s width is 90% of the editor’s width, but if the editor’s width is small, it uses full width minus 4 columns from each side.

a floating window’s height is 3/4 of the editor’s height to a max of 30 lines.

The code, with comments, so it’s easier to understand:

function NavigationFloatingWin ( ) local width = vim . api . nvim_get_option ( "columns" ) local height = vim . api . nvim_get_option ( "lines" ) local buf = vim . api . nvim_create_buf ( false , true ) vim . api . nvim_buf_set_option ( buf , 'buftype' , 'nofile' ) if ( width > 150 or height > 35 ) then local win_height = math . min ( math . ceil ( height * 3 / 4 ) , 30 ) local win_width if ( width < 150 ) then win_width = math . ceil ( width - 8 ) else win_width = math . ceil ( width * 0.9 ) end local opts = { relative = "editor" , width = win_width , height = win_height , row = math . ceil ( ( height - win_height ) / 2 ) , col = math . ceil ( ( width - win_width ) / 2 ) } local win = vim . api . nvim_open_win ( buf , true , opts ) end end

To make it work, place the code in a file such as ~/.config/nvim/lua/navigation/init.lua , and in your vim configuration put something like this:

lua require ( "navigation" ) let g : fzf_layout = { 'window' : 'lua NavigationFloatingWin()' }

Once again, I know that you can do this with VimScript, but I can’t. And, because I know there are more like me out there, I hope this helps you, and my future self as well.