How do I pretty-print Lua tables and values for debugging in Neovim using vim.print?
Answer
vim.print()
Explanation
vim.print() is a Neovim built-in that prints any Lua value in a human-readable format by automatically applying vim.inspect() internally. It saves you from writing print(vim.inspect(value)) every time you want to inspect a table or complex value during development.
How it works
- Accepts any number of arguments
- Runs each argument through
vim.inspect()before passing toprint() - Output appears in Neovim's messages area (
:messagesto review) - Also accessible in Ex command mode via
:lua = expressionshorthand
Example
-- Without vim.print:
print(vim.inspect({ 'a', 'b', nestedKey = { x = 1 } }))
-- Output: { "a", "b", nestedKey = { x = 1 } }
-- With vim.print:
vim.print({ 'a', 'b', nestedKey = { x = 1 } })
-- Same output, less typing
vim.print(vim.api.nvim_get_current_buf())
-- Output: 1 (current buffer handle)
Tips
- Use
:lua = exprin the command line as an even shorter REPL-style shortcut::lua = vim.api.nvim_get_current_win() vim.print()is also available as the globalvim.printinsidevim.cmd.luablocks- For notifications (not just debug output), prefer
vim.notify()which supports log levels and integrates with notification plugins