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— evaluatesexprand pretty-prints the result (shorthand forprint(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 =exprpretty-prints tables and complex values usingvim.inspect()— far more readable thanprint()- Access the current buffer number:
:lua =vim.api.nvim_get_current_buf() - In the command-line history window (
q:),:luaentries 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
:luacommand