home ~ projects ~ socials

Open A New Neovim Buffer In A Floating Window With Lua

This is what I'm doing to temporary pop-up windows in plugins.

local function open_floating_window()
  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)
  local floating_buffer = vim.api.nvim_create_buf(false, true)
  local floating_window = vim.api.nvim_open_win(floating_buffer, true, opts)
  return floating_buffer
end

local floating_window_id = open_floating_window()

Notes

  • The arguments to vim.api.nvim_create_buf() are:

    listed and scratch

    where listed determines if the buffer shows up in the buffer list: see buflisted

    and scratch creates a scratch-buffer that is used for temporary editing.

-- end of line --

References