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

[!NOTE] On Windows, you need to use a shell that can run Unix commands (MinGW,MSYS2,etc). Luckily, Git offers a sh.exe, so you don't need to install a heavy MSYS2 environment. Other than Git, you also need a C compiler and make to install jsregexp. You may also need to change the build command: make install_jsregexp CC=gcc.exe SHELL=C:/path/to/sh.exe .SHELLFLAGS=-c:

SHELL=C:/path/to/Git/usr/bin/sh.exe # if Git/MinGW/MSYS2 `sh.exe` is not in PATH
.SHELLFLAGS=-c # if Git/MinGW/MSYS2 `sh.exe` is not in PATH
CC=gcc.exe # if CC's default value cc is not set (when `which cc` fails to find the compiler command)
NEOVIM_BIN_PATH=C:/path/to/Neovim/bin # if the Makefile fails to automatically detect the Neovim/bin path

Keymaps

In Vim script, 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