Open A Floating Window In Neovim With Lua
This is how I'm openeing floating windows in Neovim plugins. It returns the buffer number of the floating window and sets a keymap to close the window with ESC in normal mode.
local open_floating_window = function()
local active_buffer = vim.api.nvim_get_current_buf()
local opts = {
style="minimal",
relative='editor',
border='single'
}
opts.width = vim.api.nvim_win_get_width(0) - 18
opts.height = vim.api.nvim_win_get_height(0) - 12
opts.col = (vim.api.nvim_win_get_width(0) / 2) - (opts.width / 2)
opts.row = (vim.api.nvim_win_get_height(0) / 2) - (opts.height / 2)
floating_buffer = vim.api.nvim_create_buf(false, true)
floating_window = vim.api.nvim_open_win(floating_buffer, true, opts)
vim.keymap.set("n", "<ESC>", function()
vim.api.nvim_win_close(floating_window, true)
end,
{ buffer = floating_buffer }
)
return floating_window
end
-- end of line --