Language-specific code snippets - Boost productivity with smart templates and expansions
This directory contains LuaSnip snippet definitions for various programming languages, providing quick code templates and boilerplate reduction.
snippets/
├── c.lua # C language snippets (4.6KB)
├── cpp.lua # C++ snippets (7.8KB)
├── html.lua # HTML/Web snippets (4.4KB)
├── java.lua # Java snippets (6.1KB)
├── javascript.lua # JavaScript/TypeScript (6.6KB)
├── markdown.lua # Markdown snippets (3.7KB)
├── python.lua # Python snippets (7.1KB)
├── sh.lua # Shell/Bash snippets (4.1KB)
├── tex.lua # LaTeX snippets (5.0KB)
└── todo.lua # TODO/Comment snippets (1.4KB)
c.lua & cpp.lua
main - Main function templateinc - Include statementsfor - For loopwhile - While loopstruct - Structure definitionclass - Class template (C++)printf - Print statementmalloc - Memory allocationpython.lua
main - Main guardclass - Class definitiondef - Function definitiondefs - Method with selftry - Try/except blockwith - Context managerlist - List comprehensiondict - Dictionary comprehensionimport - Import statementsjavascript.lua
func - Function declarationarrow - Arrow functionclass - ES6 classpromise - Promise templateasync - Async functionfetch - Fetch API calluseState - React hookcomponent - React componentsh.lua
bash - Bash shebangif - If statementfor - For loopwhile - While loopcase - Case statementfunc - Function definitionarray - Array operationsgetopts - Option parsinghtml.lua
html5 - HTML5 boilerplatediv - Div with classlink - Link tagscript - Script tagform - Form templateinput - Input fieldbutton - Button elementmarkdown.lua
code - Code blocklink - Markdown linkimg - Imagetable - Table templatetask - Task list itemdetails - Collapsible sectionbadge - Shield.io badgetex.lua
doc - Document templatesec - Sectioneq - Equationfig - Figuretable - Table environmentcite - Citationref - Referenceenumerate - Listtodo.lua
TODO - TODO commentFIXME - Fix me commentNOTE - Note commentHACK - Hack commentWARNING - Warning commentlocal ls = require("luasnip")
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
return {
s("trigger", {
t("Static text "),
i(1, "placeholder"),
t(" more text"),
i(0), -- Final cursor position
}),
}
-- Choice node example
s("for", {
t("for "),
c(1, {
t("i"),
t("index"),
i(nil, "var"),
}),
t(" in range("),
i(2, "10"),
t("):"),
t({"", "\t"}),
i(0),
}),
Tab to expandTab/Shift+Tab to navigate placeholdersCtrl+k to jump to next placeholder:LuaSnipListAvailable " List available snippets
:LuaSnipUnlinkCurrent " Unlink from current snippet
:LuaSnipJump 1 " Jump forward
:LuaSnipJump -1 " Jump backward
Tab:source %Example:
-- In python.lua
s("fastapi", {
t({"from fastapi import FastAPI", "", ""}),
t("app = FastAPI()", "", ""),
t("@app.get(\"/\")"),
t({"", "async def root():"}),
t({"", "\treturn {\"message\": \""}),
i(1, "Hello World"),
t("\"}"),
i(0),
}),
Create ~/.config/nvim/snippets/custom.lua:
return {
all = { -- Snippets for all file types
s("date", {
f(function() return os.date("%Y-%m-%d") end),
}),
},
lua = { -- Lua-specific snippets
s("req", {
t("local "),
i(1, "module"),
t(" = require(\""),
rep(1),
t("\")"),
}),
},
}
Snippets integrate with blink.cmp:
| Key | Mode | Action |
|---|---|---|
Tab |
Insert | Expand/Jump forward |
Shift+Tab |
Insert | Jump backward |
Ctrl+k |
Insert | Jump to next placeholder |
Ctrl+j |
Insert | Jump to previous placeholder |
Ctrl+l |
Insert | Choose next option |
Ctrl+h |
Insert | Choose previous option |
-- Date/time insertion
f(function() return os.date("%Y-%m-%d %H:%M") end)
-- Filename without extension
f(function() return vim.fn.expand("%:t:r") end)
-- Capitalize placeholder
f(function(args) return string.upper(args[1][1]) end, {1})
:checkhealth luasnip
:LuaSnipListAvailable
:lua require("luasnip").cleanup()
:lua require("luasnip.loaders.from_lua").load({paths = "~/.config/nvim/snippets"})
-- In config
require("luasnip").config.set_config({
history = true,
updateevents = "TextChanged,TextChangedI",
enable_autosnippets = true,
})