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

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 to print()
  • Output appears in Neovim's messages area (:messages to review)
  • Also accessible in Ex command mode via :lua = expression shorthand

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 = expr in the command line as an even shorter REPL-style shortcut: :lua = vim.api.nvim_get_current_win()
  • vim.print() is also available as the global vim.print inside vim.cmd.lua blocks
  • For notifications (not just debug output), prefer vim.notify() which supports log levels and integrates with notification plugins

Next

How do I enable matchit so % jumps between if/else/end style pairs?