Sublime Forum

Sometimes snippet doesn't work

#1

I have a relativley simple custom snippet:

<snippet>
    <content><![CDATA[
#### ${1:subtitle}
$2
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>st</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

It’s working about 25% of the time. Sometimes the snippet will work, other times the cursor will randomly jump up about 5 lines, other times it will jump down a couple lines and other times still it will just do a normal tab. Is there any way to fix this so it just works all the time?

0 Likes

Why are there four fields in my sublime snippet when I only have three fields defined?
#3

I think the basis of your issue is that your snippet is defined to have a second field ($2) on a second line, but in your video you’re not trying to enter any text there for that part of the snippet. When the snippet initially expands, it wants to let you type the text for $1, and then pressing tab again jumps it to the text for $2 so you can enter that.

In effect, the text (for example), “works after 3 tabs<enter>st” are all part of the text for $1, so that when you type <tab> again, now it thinks you’re entering the text for $2, while you think you’re expanding the snippet again, and then you have to hit tab some more times to get it to recognize again.

You can see this better if you change your snippet to be:

<snippet>
    <content><![CDATA[
#### ${1:subtitle}
${2:next line}
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>st</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

Based on your usage, you probably don’t want that $2 part there at all, or you should use a placeholder for it to remind you what you’re supposed to put there. :smiley:

3 Likes

Why are there four fields in my sublime snippet when I only have three fields defined?
#4

thanks @OdatNurd! Everything is working tip top :slight_smile:

Edit: just for anyone who is reading this the problem is that sublime adds a hidden field. If you want only one field then you can use the $0 tab space as shown below:

<snippet>
    <content><![CDATA[
#### ${0:subtitle}
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>st</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>
0 Likes