Neovim for Beginners — Snippets using Lua

Better coding experience using snippets.

alpha2phi

--

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.

This article is part of the Neovim for Beginners series.

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.

Lua Snippets

We already used the VSCode snippets loader and Snipmate snippets loader in the lua/config/snip/init.lua file. To load the Lua snippets, add the following lines of code.

  • We use the Lua snippets loader to lazy load snippet files under the snippets folder.
  • We configure a command called LuaSnipEdit to edit the snippet files.
Lua Snippets

Once loaded, the snippet files will be automatically reloaded on save (BufWritePost).

The Basics

Before we deep dive into the details, we need to understand the basics of Luasnip. In LuaSnip, snippets are made up of nodes. These can contain either

--

--

alpha2phi

Software engineer, Data Science and ML practitioner.