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

How do I execute Lua code directly from the Neovim command line without writing a script file?

Answer

:lua {code}

Explanation

Neovim's :lua command lets you run arbitrary Lua code inline from the command line. This is invaluable for quick debugging, inspecting runtime state, toggling options, or calling plugin APIs — all without leaving your editing session or writing a temporary script.

How it works

  • :lua {statement} — executes a Lua statement
  • :lua =expr — evaluates expr and pretty-prints the result (shorthand for print(vim.inspect(expr)); requires Neovim 0.7+)
  • :luafile {path} — executes a Lua script file
  • :lua <<EOF ... EOF — runs a multi-line Lua heredoc

Example

" Print Neovim version
:lua print(vim.version())

" Enable line numbers on the fly
:lua vim.opt.number = true

" Inspect all loaded buffers
:lua =vim.api.nvim_list_bufs()

" Open Telescope (if installed)
:lua require('telescope.builtin').find_files()

" Check LSP clients attached to the current buffer
:lua =vim.lsp.get_clients({ bufnr = 0 })

Tips

  • :lua =expr pretty-prints tables and complex values using vim.inspect() — far more readable than print()
  • Access the current buffer number: :lua =vim.api.nvim_get_current_buf()
  • In the command-line history window (q:), :lua entries are preserved and reusable
  • Use :luado {body} to run a Lua function over every line in a range: :%luado return line:upper() uppercases the whole file
  • Neovim-only — Vim does not have a :lua command

Next

How do I open the directory containing the current file in netrw from within Vim?