Home

Awesome

LuaSnip

LuaSnip

https://user-images.githubusercontent.com/41961280/122515860-5179fa00-d00e-11eb-91f7-331893f61fbf.mp4

Features

Drawbacks

Requirements

Neovim >= 0.7 (extmarks) jsregexp for lsp-snippet-transformations (see here for some tips on installing it).

Setup

Install

Keymaps

In vimscript, with <Tab> for jumping forward/expanding a snippet, <Shift-Tab> for jumping backward, and <Ctrl-E> for changing the current choice when in a choiceNode...

" press <Tab> to expand or jump in a snippet. These can also be mapped separately
" via <Plug>luasnip-expand-snippet and <Plug>luasnip-jump-next.
imap <silent><expr> <Tab> luasnip#expand_or_jumpable() ? '<Plug>luasnip-expand-or-jump' : '<Tab>' 
" -1 for jumping backwards.
inoremap <silent> <S-Tab> <cmd>lua require'luasnip'.jump(-1)<Cr>

snoremap <silent> <Tab> <cmd>lua require('luasnip').jump(1)<Cr>
snoremap <silent> <S-Tab> <cmd>lua require('luasnip').jump(-1)<Cr>

" For changing choices in choiceNodes (not strictly necessary for a basic setup).
imap <silent><expr> <C-E> luasnip#choice_active() ? '<Plug>luasnip-next-choice' : '<C-E>'
smap <silent><expr> <C-E> luasnip#choice_active() ? '<Plug>luasnip-next-choice' : '<C-E>'

... or in lua, with a different set of keys: <Ctrl-K> for expanding, <Ctrl-L> for jumping forward, <Ctrl-J> for jumping backward, and <Ctrl-E> for changing the active choice.

local ls = require("luasnip")

vim.keymap.set({"i"}, "<C-K>", function() ls.expand() end, {silent = true})
vim.keymap.set({"i", "s"}, "<C-L>", function() ls.jump( 1) end, {silent = true})
vim.keymap.set({"i", "s"}, "<C-J>", function() ls.jump(-1) end, {silent = true})

vim.keymap.set({"i", "s"}, "<C-E>", function()
	if ls.choice_active() then
		ls.change_choice(1)
	end
end, {silent = true})

nvim-cmp's wiki also contains an example for setting up a super-tab-like mapping.

Add Snippets

Check out the doc for a general explanation of the loaders and their benefits. The following list serves only as a short overview.

There's also a repository collecting snippets for various languages, molleweide/LuaSnip-snippets.nvim

Documentation

Getting started

You have two main choices: use SnipMate/VS Code snippets (easier) or write snippets in Lua (more complex but also more feature-rich). Here are some suggestions for getting started in either case:

Official docs and examples

Note: instead of immediately reading the official documentation, you may want to check out the Resources for new users section below since the docs are written more as a reference manual than as a tutorial for new users.

【中文版】DOC in Chinese is here.

Resources for new users

Here are some LuaSnip videos and tutorials on the Web:

Inspired by vsnip.vim