This article discusses improving the configuration of snippets in Neovim using Lua to enhance coding efficiency.
Abstract
The article titled "Neovim for Beginners — Snippets using Lua" focuses on refining the existing snippets configuration in Neovim using Lua. It covers the use of the Lua snippets loader, the basics of Luasnip, and creating code snippets to improve coding efficiency. The article provides examples and details on various nodes, such as choice nodes, function nodes, snippet nodes, and dynamic nodes, along with their usage in creating snippets for Lua and general coding. It also includes references and resources for further learning.
Bullet points
The article is part of the "Neovim for Beginners" series.
The article aims to improve the configuration of snippets in Neovim using Lua.
The Lua snippets loader is used to lazy load snippet files.
A command called "LuaSnipEdit" is configured to edit the snippet files.
Luasnip basics are covered, including nodes, such as text nodes, insert nodes, function nodes, choice nodes, restore nodes, and dynamic nodes.
Examples and details are provided for various nodes, such as choice nodes, function nodes, snippet nodes, and dynamic nodes.
Code snippets are created for Lua, such as requiring a module, protected call, creating a module, and creating a function.
General snippets are created, such as converting a URL to an HTML link and a markdown link.
Python snippets are created, such as a function definition with dynamic virtual text.
References and resources are provided for further learning.
Neovim for Beginners — Snippets using Lua
Better coding experience using snippets.
Neovim for Beginners — Snippets using Lua
In the previous article, we configured snippets plugins and wrote our snippets using Lua. We will build on the existing setup and improve the configuration to help us code more efficiently. We are going to
Refactor existing configuration to use Lua snippets loader.
Go through Luasnip basics.
Develop code snippets to help us code more efficiently.
The Neovim configuration files can be found in this repository.
Lua Snippets Loader
In the previous article, we separated our snippets by the languages under the lua/config/snip/snippets folder, and use an autocmd to load the snippet files. For Luasnip, there is a better way to do this now. We are going to refactor our configuration to use the Lua snippets loader.
We use the function node and the Lua library to return the current date and time.
Snippet Node
The snippet node directly inserts its contents into the surrounding snippet. This is useful for a choice node, which only accepts one child, or a dynamic node, where the node is created at runtime and inserted as a snippet node.
We define <Ctrl-u> to show the choices when the choice node is active.
Using the same snippet, we can now press <Ctrl-u> to display the choices.
Select Choice
Dynamic Node
Dynamic node is very similar to function node but returns a snippet node instead of just text, which makes it very powerful as parts of the snippet can be changed based on user input.
s("link_url", {
t '<a href="',
f(function(_, snip)-- TM_SELECTED_TEXT is a table to account for multiline-selections.-- In this case only the first line is inserted.return snip.env.TM_SELECTED_TEXT[1] or {}
end, {}),
t '">',
i(1),
t "</a>",
i(0),
}),
Now we can change an URL to an HTML link easily.
Convert URL to HTML Link
We select the URL and press <Ctrl-q>. The URL is removed automatically.
We type the trigger word link_url and the snippet is automatically populated with the URL