Neovim for Beginners — Lua APIs
In this article, let’s learn a few tricks using the Neovim Lua APIs.
This article is part of the Neovim for Beginners series.
The Neovim configuration files are available in this repository.
Pretty Print
As we learn to configure Neovim, we may need to inspect the content of a Lua variable or object. Neovim provides these 2 APIs to help.
vim.inspect
(:h vim.inspect()
) — Return a human-readable representation of the given object.vim.print
(:h vim.print()
) — Print the given arguments in a human-readable format.
E.g., if we want to display the loaded packages (:h package.loaded
), we can use either
:lua print(vim.inspect(package.loaded))
:lua vim.print(package.loaded)
:lua =package.loaded
The output is in the command buffer which makes it difficult to read. We can use the execute
command (:h execute
) to display the output in a new buffer.
:enew|put=execute('lua =package.loaded')
Alternatively, we can use the redir
(:h redir
) command.
:redir @a
:lua =package.loaded
:redir END
We redirect the command output to the register a
. From the buffer, we can print out the register (“ap
).
vim.loop
vim.loop
(:h vim.loop
) exposes all features of the Neovim event-loop. It is a low-level API that provides networking, filesystem, and process management functionality.
Internally, vim.loop
wraps the luv
Lua bindings (:h luv-intro
) for the LibUV library.
:lua =vim.loop