Is it possible to use snippets with surround? #108
|
Is it possible to add tabstops and similar snippet features to surround? If it isn't possible to achieve as part of a custom surround binding, is it possible to build a snippet that surrounds some word or selected text, and add tabstops that way? |
Replies: 1 comment
|
Short answer: You can't add tabstops directly to surround definitions, but you can build snippets that wrap selected text — effectively achieving "surround with tabstops" — using the How it worksThe snippet system supports Example: JSON snippet fileCreate a {
"Wrap in link": {
"prefix": "wraplink",
"body": "[${1:$TM_SELECTED_TEXT}](${2:url})$0",
"description": "Surround with markdown link, tabstop on URL"
},
"Wrap in callout": {
"prefix": "wrapcallout",
"body": [
"> [!${1:info}] ${2:Title}",
"> $TM_SELECTED_TEXT",
"$0"
],
"description": "Wrap selection in a callout with configurable type"
}
}Usage
Alternatively, Lua DSL alternativeIn local s = vim.snippet.s
local t = vim.snippet.t
local i = vim.snippet.i
local fmt = vim.snippet.fmt
vim.snippet.add("wraplink", s("Wrap in link", fmt(
"[{}]({}){}", { i(1, "$VISUAL"), i(2, "url"), i(0) }
)))Same workflow — select text, What about Tab-triggered expansion?Tab expansion requires an empty selection (you're typing a prefix and hitting Tab), so Why surround definitions can't have tabstopsSurround operates at the vim motion/operator level (it's implemented in the codemirror-vim fork), while tabstops are a CodeMirror snippet-expansion concept. They're separate systems with no bridge between them. Custom surround pairs ( The snippet-based approach above is the intended way to get "surround + tabstops" behavior. |
Short answer: You can't add tabstops directly to surround definitions, but you can build snippets that wrap selected text — effectively achieving "surround with tabstops" — using the
$TM_SELECTED_TEXT/$VISUALvariable. This is supported in version0.100.0and later.How it works
The snippet system supports
$TM_SELECTED_TEXT(and the vim alias$VISUAL), which resolves to whatever text is selected when the snippet is expanded. Combined with the:snippetex command (which works in visual mode), this gives you surround-with-tabstops behavior.Example: JSON snippet file
Create a
.jsonfile in your snippet directory (configured in Settings → Vim Motions → Snippets):{ "Wrap in link": { …