Sublime Forum

Is there no way to add multiple snippets to a single file?

#1

I am astonished to find that you need to have one .sublime-snippet file for EVERY SNIPPET. That is a deal breaker for me, since I use a LOT of snippets. I was preferring Sublime over Atom, but ran into this snafu. In Atom I currently have ALL of my snippets in a single file.

0 Likes

Allow multiple snippets in same sublime-snippet file
Sublime Text 3 vs the Others
Snippet Not Working Properly in HTML/SASS/SCSS but only in CSS & JS
#2

you could create a .sublime-completions file instead - basically the same functionality, but you can have multiple completions per file, and the tab trigger shows up in autocomplete

example

3 Likes

#3

Doesn’t .sublime-completions behaves differently from snippets?

0 Likes

#4

The two are fairly similar to each other, although I’m not fully sure what all of the underlying differences between the two actually are. It seems like for most cases you could interchangeably use one or the other, depending on what you’re trying to do.

Snippets:

  • Are “bulky”, since you need a separate file for each one
  • Use a CDATA section in XML, which makes it easier to throw in free form text, since you only need to escape the magic $ characters used for expansions and fields
  • Show up in the command palette if they apply to the current scope
  • Each specify their own scope

Completions:

  • Are “slimmer” because you can throw a bunch of them into a single file
  • Use JSON, which means that you need to quote a fair number of things to make the JSON valid
  • Don’t show up in the command palette
  • Share the same scope as all other completions in the same file

On the other hand, both support variables, fields and field substitutions and show up in the auto-completion list. The unofficial docs mention that if there is a snippet and a completion both with the same expansion text, a snippet always wins if you use the full trigger and a completion always wins on a fuzzy match, which I guess you could use to your advantage depending on what you were trying to do.

There’s also the idea that if there are multiple completions that have the same trigger, you can press Tab repeatedly after the completion inserts to swap between the matches, while for a snippet the text is inserted and that’s that. For example:

{
    "completions":
    [
        "test1",
        "test2",
        "test3",
        "test4",
    ]
}

In a plain text file, entering te and pressing tab will cycle through the four options; if you do it in an HTML file, it cycles between all HTML tags that start with te plus these completions. Of course, this doesn’t work if you use an expansion with fields in it, since in that case the Tab presses skip between fields.

I think which you use really comes down to what you intend to use them for. For more complex code templates or things not used very often (so that they show up in the command palette as a reminder) I would probably favour some snippets. On the other hand, if you’re just using a bunch of smaller bits of text that apply to some programming language, maybe completions might be better.

5 Likes

Removal of Emmet
Sublime Text 3 vs the Others
How many snippets file can I put into User folder?
#5

Tips:

If you like snippets (I do) and don’t like auto-completion (I don’t) you can set your preferences :

{
  ...
  "auto_complete": false,
  "tab_completion": true,
...
}

Enjoy!

0 Likes

#6

Maybe you can create all your snippets in a Snippets folder in Packages/User/'s ST folder.

0 Likes

#7

I think it’s worth noting that some (non-word?) characters can be the first char of a snippet but not a completion. So if you’re trying to trigger on, say, $ or <, you may need a snippet.

2 Likes