vimtricks.wiki Concise Vim tricks, one at a time.

How do I create interactive selection menus and text input prompts in Neovim Lua scripts?

Answer

vim.ui.select() and vim.ui.input()

Explanation

Neovim provides vim.ui.select() and vim.ui.input() as standardized UI hooks for user interaction. They work out of the box as simple prompts but are overridable by plugins — tools like dressing.nvim or telescope.nvim can replace them with richer UIs, making your Lua config automatically benefit from any UI plugin the user installs.

How it works

vim.ui.select(items, opts, callback) — pick one item from a list:

vim.ui.select(
  { 'tabs', 'spaces', 'mixed' },
  { prompt = 'Choose indent style:' },
  function(choice)
    if choice then
      print('You chose: ' .. choice)
    end
  end
)

vim.ui.input(opts, callback) — prompt for free-form text:

vim.ui.input(
  { prompt = 'New variable name: ', default = 'myVar' },
  function(input)
    if input then
      vim.cmd('s/\\<oldName\\>/' .. input .. '/g')
    end
  end
)

Tips

  • Always check if the callback argument is non-nil before using it — it is nil when the user cancels
  • These functions are the standard API for interactive prompts in Neovim plugins; prefer them over raw vim.fn.input() so users can override them with a fancier UI
  • Install dressing.nvim or snacks.nvim to get telescope-style or floating-window UIs automatically for any code using these functions
  • vim.ui.select is what Neovim uses internally for vim.lsp.buf.code_action() — overriding it improves the built-in LSP experience

Next

What is the difference between the inner word (iw) and inner WORD (iW) text objects in Vim?