mhmood-sf / snip Goto Github PK
View Code? Open in Web Editor NEWArchived: plugin for managing vim abbreviations.
License: MIT License
Archived: plugin for managing vim abbreviations.
License: MIT License
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).
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>
}
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 ๐).
#
breaks highlighting inside snip block (thinks it's a comment, but they're not allowed inside the block).That's all I can think of for now, might add more stuff later?
:h abbreviations
shows a method for eating the space character, which can be added to the plugin.
As the title says, add snips to the completion list, based on which ones are activated in the current buffer. See :h completion-functions
.
I'll add more later, maybe.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.