nui.popup - MunifTanjim/nui.nvim GitHub Wiki

Focus a Popup

The window id for a popup is stored in .winid property once the popup is mounted. You can focus the popup window using that:

vim.api.nvim_set_current_win(popup.winid)

Strategies for closing the Popup

Depending on what your use case is, you probably want to setup some way for the popup window to close itself. Below are some common strategies. All of the examples below assume you have created a Popup window already, such as in the example at lua/nui/popup.

NOTE: If the popup is not cleaned up properly with popup:unmount() when the window is closed, you may get undesired behavior such as an orphaned window border.

Close on Lost Focus

This example will close the popup when it loses focus for any reason, including an explicit close or the user switching to another window:

  popup:mount()

  popup:on("BufLeave", function()
    popup:unmount()
  end, { once = true })