Skip to content

Instantly share code, notes, and snippets.

@eduardoarandah
Created June 6, 2023 18:12
Show Gist options
  • Save eduardoarandah/1dd12fe345861952d48d65a0f72dc874 to your computer and use it in GitHub Desktop.
Save eduardoarandah/1dd12fe345861952d48d65a0f72dc874 to your computer and use it in GitHub Desktop.
Show a floating popup in neovim with lua
-- show a floating popup
local function show_popup(size_percentage, filetype, title, lines)
-- build buffer
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
vim.api.nvim_buf_set_option(buf, "filetype", filetype)
vim.api.nvim_buf_set_keymap(buf, "n", "<esc>", ":bd<cr>", { nowait = true, noremap = true, silent = true })
vim.api.nvim_open_win(buf, true, {
style = "minimal",
relative = "editor",
width = math.ceil(vim.o.columns * size_percentage),
height = math.ceil(vim.o.lines * size_percentage),
row = math.ceil(vim.o.lines * (1 - size_percentage) / 2),
col = math.ceil(vim.o.columns * (1 - size_percentage) / 2),
border = "double",
title = title,
})
-- content
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
end
-- example of use:
vim.api.nvim_create_user_command("ShowPopup", function()
show_popup(0.5, "markdown", "Hello world", { "hello", "world" })
end, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment