Sublime Forum

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

#1

I have the following snippet:

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

it runs fine except when I invoke it sublime is looking for 4 tab fields instead of three, so when I’m done filling in the summary I have to hit tab again to fill the fourth field. Why is this happening and how do I change the behavior so there are only three tab fields? Thank you!
enter image description here

This might be a great question for @OdatNurd :slight_smile:

1 Like

#2

By default, snippets contain an extra hidden “field” at the end, so that you can press Tab to move the cursor to after the end of the snippet, regardless of where the last field is defined in the snippet.

Therefore, you probably want to change your snippet to remove the ${3:summary}, so that it will look like this:

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

and it will behave the way you desire.

1 Like

#3

(Sorry @OdatNurd for stealing your thunder! :smiley: actually it looks to be the same advice you gave previously anyway :wink: )

1 Like

#4

Thanks for getting back to me @kingkeith! I see what you mean about the hidden field being added; however, removing the summary section doesn’t really address the problem. It just means I have to press tab earlier to get rid of the hidden field. In an earlier workaround I used $0 to seemingly remove the hidden tab, but that only works for a single field. I made a video to show the behavior I’m talking about: https://goo.gl/3TEIBP

Do you know of a way to keep multiple fields but remove the hidden field so when I get to the last field I don’t have to press tab to close that last remaining hidden field? Thanks for your help :slight_smile:

0 Likes

#5

Based on the docs on snippets, I don’t think so, no. :frowning:

The rules for snippets are such that you need to press tab after every field to tell sublime that you’re done with it. The trick with $0 is just that it specifies where the cursor is placed after the last tab press (instead of just after the highest numbered tab spot).

You can press ESC at any point to cancel out, so the best workaround I can suggest would be to modify the last bit of your snippet to remind you or to get into the habit of always pressing Tab after you fill out a field.

#### ${2:sub title then press ESC}
0 Likes

#6

That seems like a weird bug that only happens if your snippet has a placeholder @ the very end of <content>.

( IE: whatever process is managing the tab-stops doesn’t have validation in place to see if lastPlaceholder == endOfSnippet ; so in this case it is redundantly moving from the last tab stop to the end of the snippet, which happens to be the same place )

 
Taking that into consideration, maybe add a blank line to the snippet so that the 4th tab-stop positions you after the actual snippet contents. ( which has the same effect as pressing enter; since as soon as you hit the 4th tab-stop, the snippet process is effectively exited )

3 Likes

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

There you go.

You can override the implicit $0 tab trigger that will normally be appended to let the user continue typing after the snippet. It also supports placeholders.

4 Likes

#8

Thank you so much! I’ve been wondering how to do this for weeks :slight_smile: you rock!

0 Likes