Sublime Forum

Snippet: Multiple selections, different content

#1

Hey everyone!
I’d like to be able to optionally delete content of a snippet that belongs together, but at different places, there’s different content like so:

${1:int x (int i) {
    }return i+1;${1:
\}}

Right now, the second $ mirrors the first $'s content, but it’s supposed to only include that of the the second $. Is that possible?

Best wishes,
Shu

0 Likes

#2

I’m not quite sure what you’re asking here, but all snippet fields with the same number (in this case $1) always share the same content and only the first one is editable.

If you want them to be distinct, you need to use different numbers for them, like $1 and $2 instead.

The cursor visits the snippet fields in order.

0 Likes

#3

Thanks for the info, I guess that just doesn’t work then, since either I’ll have to choose a later selection or mirror exactly the content.
With the snippet, I’d like something like this:

Having the snippet invoked, then pressing tab, it should be

int x (int i) {
    return i+1;
}

, but having the snippet invoked, then pressing backspace, it should be

return i+1;

Something like optional lines which I can get rid of in one go, since they belong together.
I essentially want to be able to edit multiple selections at the same time (so their number would need to be the same), but with different original content (can’t be the case, since their numbers are the same then).

0 Likes

#4

Indeed, for something like that (except in very constrained cases) I think your solution is to have multiple snippets, one for each of the different blocks that you might want to use, or possibly some interaction with a plugin.

The “very contstrained case” mentioned above is that the mirrored version of a field can be modified via a regular expression, but that is not going to help you here. An example of this would be allowing you to type a word and have it uppercased, or turned from CamelCase to camel_case or similar; something that transforms the text in a known way and isn’t freeform.

0 Likes

#5

well, presumably the mirrored field could replace the whole contents of the first field with } if not empty?
indeed, this seems to work:

<snippet>
    <content><![CDATA[
${1:int x (int i) {
    }return i+1;${1/^.+$/\n\}/}
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <!-- <tabTrigger>hello</tabTrigger> -->
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>
0 Likes

#6

@kingkeith
Thanks for the reply! Quite interesting! That does indeed work!

0 Likes