Rationale
So, snippets with multi-cursors or similar functionality seems to be tricky to do without adding a lot of complex code (at least from what i can tell?), so instead a simpler way is to trigger the abbreviation, leave input placeholders, and then prompt the user for what to replace the placeholders with. This sounds (and kind of is) a bit hacky, but seems to work smoothly enough, and can be refined further (suggestions at the end).
Example implementation
First, a SnipInput()
function:
function! SnipInput()
let s:placeholder = localtime() . localtime()
return s:placeholder
endfunction
The placeholder needs to be a large and random enough string such that when it is replaced at the end, there's a near-zero chance of colliding with anything else in the buffer. An example (as shown above) is the current time in ms (repeated twice just in case).
Next, the prompt function:
function! SnipPrompt(prompt)
let str = input(a:prompt)
execute ":%s?" . s:placeholder . "?" . str . "?g"
return ""
endfunction
Here we make use of :h input()
and then execute a global substitute, replacing the placeholder with the input string and returning an empty string so nothing else is added to the abbreviation.
The abbreviation itself would look like:
inoreabbrev test
\ \begin{<C-R>=SnipInput()<CR>}<CR>
\ <CR>
\ \end{<C-R>=SnipInput()<CR>}<CR>
\ <C-R>=SnipPrompt("Environment: ")<CR>
Or in snip syntax:
snip test {
\begin{<C-R>=SnipInput()<CR>}<CR>
<CR>
\end{<C-R>=SnipInput()<CR>}<CR>
<C-R>=SnipPrompt("Environment: ")<CR>
}
UX improvements
Obviously, the input/prompt parts are quite unreadable. Since the input part is always constant, it would be easy to add a special keyword (for example, <INPUT>
) which can be used in the snip file, and then later when the snippets are being registered it can be replaced with the actual expression. Something similar could be done with SnipPrompt, but it needs to be decided how to handle the prompt string. Maybe something like <PROMPT:Environment>
? Spaces and nested colons might be tricky to handle depending on how this is parsed. Regardless, with these changes the syntax would look like:
snip test {
\begin{<INPUT>}<CR>
<CR>
\end{<INPUT>}<CR>
<PROMPT:Environment>
}
This is obviously much cleaner and readable (even more so with syntax highlighting, which isn't available here on GH ๐).