Sublime Forum

Help with Sublime Snippet Duplication

#1

I’ve recently started wring tests which require them to look something like this.

function test_a_user_can_view_their_profile() { }

I have this so far which will replace spaces with underscores, my problem is that the snippet also keeps the method name with the spaces as I type, how do I only keep the one with the underscores?

<snippet>
<content><![CDATA[
function ${1:} ${1/\s/_/g}()
{
${2:}
}
]]></content>
<tabTrigger>t</tabTrigger>
</snippet>
0 Likes

Sublime Regex Snippet help
#2

The unofficial documentation on snippet subtitutions says:

Use substitutions to dynamically generate text based on a mirrored tab stop. Of course, the tab stop you want to use as variable has to be mirrored somewhere else in the snippet.

So I don’t think it’s possible to do something like this without having a mirrored field somewhere else in the snippet. A workaround for this would be to use the mirrored copy as the name of the function and put the other part in a comment where it won’t get in the way, for example:

<snippet>
	<content><![CDATA[
// Test: ${1}
function ${1/\s/_/g}()
{
${0}
}
]]></content>
	<tabTrigger>t</tabTrigger>
</snippet>
3 Likes

#3

I was starting to think there wasn’t a way either, but your solution is the next best thing, thank you very much :slight_smile:

0 Likes