-
Notifications
You must be signed in to change notification settings - Fork 13
Home
Here's a gallery of snippets that illustrate the awesome power of this fully operational snippet plugin.
The following snippet inserts boilerplate for a Vim plugin.
Note that $1
is specified twice—the value you enter for the first occurence of $1
is reused for all other $1
.
$0
is the last position.
pluginboilerplate = [[
" Author: Foo Bar <foo.bar@example.org>
" Description: $2
if exists('g:loaded_$1')
return
endif
let g:loaded_$1 = 1
let s:save_cpo = &cpoptions
set cpoptions&vim
$0
let &cpoptions = s:save_cpo
unlet s:save_cpo]],
The following snippet can be used to insert the current date:
["ymd"] = "${=os.date('%Y-%m-%d')},
To have a multi-line snippet automatically match the surrounding indentation, you can use, e.g.,
["for"] = require'snippets'.u.match_indentation "for ${1:i} = 0, $2 do\n $0\nend",
If you want to apply the indentation at expansion time (e.g., since you want to overload expansion to apply indentation to all snippets by default), you have to do
require'snippets'.expand_at_cursor((require'snippets'.u.match_indentation(S)))
(where S
is the snippet string), since match_indentation
actually returns multiple values.
This gives a random color in hex format:
randcolor = function()
return format("#%06X", math.floor(math.random() * 0xFFFFFF))
end;
This uses the force_comment()
utility function to turn this line into a filetype-specific comment:
copyright = require'snippets.utils'.force_comment [[Copyright (C) Foo Bar ${=os.date("%Y")}]];
This takes the text entered for $1
, strips everything before the final .
(if there is one), and uses the result as the default text for $2
:
req = [[local ${2:${1|S.v:match"([^.()]+)[()]*$"}} = require '$1']];
Note the use of S.v
for the text entered for $1
.
Here's an example that uses a lua function to autogenerate a numbered list up to a specified number (e.g., for Markdown):
function snippet_make_list(n)
n = tonumber(n) or 0
if n == 0 then
return ""
end
local r = {}
for i = 1, n do
r[i] = format("%d. ", i)
end
return concat(r, '\n')
end
list = [[${1|snippet_make_list(S.v)}]];
A more practical example (courtesy of @sunjon_) is the following, which generates a Markdown table head from a comma-delimited list of headings (entered as a single placeholder text):
function make_table(csv)
local str = "|"
local headers_complete = false
local headers = {}
for cell in csv:gmatch('([^,]+)') do
-- match and remove EOL char
if cell:sub(-1) == '.' then
cell = cell:sub(1, -2)
headers_complete = true
end
-- capitalize each word
cell = string.gsub(" "..cell, "%W%l", string.upper):sub(2)
str = str .. string.format(" %s |", cell)
headers[#headers+1] = cell:len()
-- build the header separator
if headers_complete then
str = str .. '\n|'
for i=1, #headers do
str = str .. string.format(" %s |", string.rep('-', headers[i]))
end
str = str .. '\n|'
end
end
return str
end
tbl = [[${1|make_table(S.v)}]];
The following snippet (courtesy @smolck) inserts a list of strings:
function create_list_of_strings(input)
local str = ''
local function wrap_quotes(x) return "'" .. x .. "'" end
for item, _ in input:gmatch('([%s%w%.%_]+),') do
local str_end = (' '):rep(vim.bo.shiftwidth) .. wrap_quotes(item)
if str == '' then
str = str .. str_end
else
str = str .. ',\n' .. str_end
end
end
return str
end
slist = [[
local $1 = {
${2|create_list_of_strings(S.v)}
}
]]
Similarly, for generating enums or lists of arguments:
function snippets_ngen(pattern, count, sep)
local start, finish = count:match("(%d)%s*,%s*(%d+)")
if start then
start = tonumber(start)
finish = tonumber(finish)
else
start = 1
finish = tonumber(count) or 1
end
local R = {}
for i = start, finish do
insert(R, pattern:format(i))
end
return concat(R, sep)
end
```lua
ngen = [[${1|""}${2|""}${3:,|""}${|snippets_ngen(S[1], S[2], S[3])}]];
You can of course call neovim API functions. The following snippet inserts the current line number at the cursor:
line = [[${=tostring(vim.api.nvim_win_get_cursor(0)[1])}]];
And this one expands a value using the unamed register's (where a yank goes) contents:
cljv = [[JSON.stringify(${=vim.fn.getreg('"')}, null, 2);]];