Sublime Forum

Tab complete failing after snippet insertion

#1

I think there’s something wrong with my snippet, so this is the code:

<snippet> <content><![CDATA[ @include MQ($1) { $2 } ]]></content> <tabTrigger>mmq</tabTrigger> <scope>source.scss</scope> <description>mixin media query</description> </snippet>

  1. Typing ‘mmq’ followed by tab inserts the snippet as intended.
  2. The insertion point goes where I want.
  3. When I tab again the cursor moves to where the css should start. So far so good.
  4. If I then type an Emmet abbreviation (e.g. ‘fz’ for font-size) and hit tab, nothing happens and the ‘fz’ stays highlighted; if I click somewhere else on the page, then back to after the ‘fz’ and then hit tab it works (font-size replaces ‘fz’). Font-size is just an example, any abbreviation has the same effect, likewise another snippet, so its not just Emmet.

Is there something wrong with the snippet, or a setting in Sublime (Emmet is just using default settings, nothing completion-related in my Sublime user settings).
Any suggestions appreciated!

0 Likes

#2

Snippets have a default “exit point” (where your cursor goes when you press Tab after the last field) that sits after the last character in the snippet, which in your case would be after the } character. Technically it counts as an extra field in the snippet.

This is visible in the status line where you can see sublime telling you Field 1 of 3 even though you only have $1 and $2 defined. Each press of Tab takes you to the next field, so in normal operation you need to press it twice to jump to the “third” field, which is the end of the snippet.

It seems like there may be some additional interplay with Emmet going on here because I would expect that when you type fz and press Tab it should jump to the end of the snippet and not leave the text highlighted.

In any case, if your intention is to leave the cursor inside of the braces at the position of your $2 for further editing, you should change the $2 to $0. That tells sublime that the snippet should have an “exit point” that’s at that location instead of at the end of the snippet. In that case there are only two fields, and after you press Tab once, the cursor will skip to the middle of the braces and the snippet will be completed.

4 Likes

#3

you might also want to try setting the following preference to true:

// Controls if auto complete is shown when snippet fields are active.
// Only relevant if auto_complete_commit_on_tab is true.
"auto_complete_with_fields": false,
3 Likes

#4

Brilliant, thank you, changing the 2 to 0 works fine now - thanks for such a thorough explanation! :slightly_smiling:

1 Like