How do I create a floating window in Neovim?
Answer
:lua vim.api.nvim_open_win(0, true, {relative='editor', width=80, height=20, row=5, col=10})
Explanation
Neovim's floating windows hover above the main layout, creating popup-like UI elements. They're used by plugins for menus, documentation previews, and notifications. You can create them programmatically using the Neovim API.
How it works
nvim_open_win(buf, enter, config)— creates a floating windowbuf— buffer to display (0 for current, or create one withnvim_create_buf)enter— whether to enter the window (true/false)- Config options:
relative,width,height,row,col,border
Example
:lua local buf = vim.api.nvim_create_buf(false, true)
:lua vim.api.nvim_open_win(buf, true, {
relative='editor', width=60, height=15,
row=3, col=10, border='rounded'
})
+---------------------------+
| Main editor |
| ╭──────────────────╮ |
| │ Floating window │ |
| │ with border │ |
| ╰──────────────────╯ |
+---------------------------+
Tips
borderoptions:'none','single','double','rounded','shadow'- Set
relative='cursor'to position relative to the cursor - Close with
:qornvim_win_close(win_id, true) - Use
nvim_create_buf(false, true)for a scratch buffer (unlisted, no swapfile)