snippet layer#
The snippet layer allows you to define code snippets via the
LuaSnip snippet engine. These may be
specified using VS Code syntax, SnipMate syntax, or directly in Lua.
Bindings#
<Tab>in insert mode: after typing snippet trigger, expands the snippet;<Tab>in insert/selection mode: jumps to the next snippet stop;<S-Tab>(shift + tab) in insert/selection mode: jumps to the previous snippet stop;<C-E>(ctrl + e) in insert/selection mode: cycles through options for multiple choice snippet stops.
Bindings can be customized via the binds layer.
Configuration#
setup: setup settings for LuaSnip (as documented here);loaders: a table whose keys are snippet types (lua,snipmate, orvscode) for Lua to load from the filesystem, and values are the respective configurations (as described here).
Examples#
-- path/of/your/vim/config/init.lua
require("visimp")({
snippet = {
setup = { -- LuaSnip setup configuration
update_events = { "TextChanged", "TextChangedI" },
},
loaders = {
-- load SnipMate-like snippets from
-- path/of/your/vim/config/snippets/. See below for an example.
snipmate = {}
-- load VS-Code-like snippets from any directory in your Neovim's
-- `runtimepath` containing at least one VS-Code-like package.json
-- contributing snippets. See
-- https://github.com/rafamadriz/friendly-snippets as an example.
vscode = {},
-- load snippets defined in Lua from
-- path/of/your/vim/config/luasnippets/. See below for an example.
lua = {},
},
},
})# path/of/your/nvim/config/snippets/haskell.snippets
# "main" will be expanded as Haskell's main function. The editor will move to
# the end of the snippet (where the main function implementation should be),
# selecting the word "undefined", which acts as default implementation.
snippet main
main :: IO ()
main = ${0:undefined}-- path/of/your/nvim/config/luasnippets/all.lua
local ls = require("luasnip") -- snippets are defined as trees
local s = ls.snippet -- snippets constructor from trigger keyword and tree
local t = ls.text_node -- plaintext node
return { -- this .lua file returns a list with just one snippet
-- "lorem" will be expanded as plaintext, non-interactive snippet "Lorem
-- ipsum dolor..."
s("lorem", t("Lorem ipsum dolor sit amet, consectetur adipiscing elit."))
}See the documentation section to get familiar with LuaSnip.
Layer-specific API#
Other layers may interact with snippet via the following method.
add_snippets(ldr:string, opts:table or nil)#
A wrapper for LuaSnip’s lazy loading. The first parameter is either “vscode”, “snipmate”, or “lua”. The second parameter is described in detail at the previous link.
local snippet = require('visimp.loader').get('snippet')
snippet.add_snippets('vscode', { paths = '~/.config/nvim/my_snippets' })Documentation#
LuaSnip’s README lists all kinds of (un)official resources for beginners and experienced users, including:
- LuaSnip’s documentation (also available as
:help luasnip.txt); - LuaSnip’s Lua snippets examples;
- LuaSnip’s wiki.