Sublime Forum

Saving snippets, where?

#1

Hi, I have a bit of a problem getting the hang of snippets. Got a bunch of questions:

1- Where do I save them?
default for me (Ubuntu) is .config/sublime-text-3/packages/User.
That seems a bit messy to me. I need to separate my snippets in several different scopes.
Do I have to make those folders myself, on that location? Shouldn’t I save css snippets in a css scope file, and php snippets in a php file?

2- I made a few different snippets. Some work, some do not.
It’s the default new snippet. I set a TabTrigger in it.
Set the scope to html, doesn’t work. Set the scope to css, works in css files.
Obviously, I’m new to snippets, but this stumps me.
Who can help me out?

0 Likes

#2

The path that Sublime is defaulting to is your User package, and is indeed the best place to store your snippets. That package is meant to contain your own specific customizations to Sublime text, and Sublime makes sure to always load that package last to ensure that your changes are always applied.

That said, you can create your own directory structure inside of the User package and store the snippets there. For example you could create a snippets folder, and then inside of that folder folders for your different uses (css, html, etc).

It’s hard to say without seeing the snippets that you actually created. Without that, the best advice I can give is that you use Tools > Developer > Show Scope Name from the menu (see that menu entry for it’s key binding). That will show you the exact scope at the cursor position in the current file.

There is some unofficial documentation on scopes that comes in quite handy, but I would recommend that you open up several different types of files and run that command at several different positions and watch how the scope is changing to get a better feel for what exactly is happening there.

The more of the terms that you use, the more you dial in exactly where you want to be. To start with you probably want to go with just the first term and then dial in as you see the need. For your case, that would be text.html for your snippet to work in any HTML file, or source.css for standard CSS files. Things like SASS may be different (I don’t do a lot of web stuff).

I’ve never used it, but BetterSnippetManager by @math2001 may help you out. I believe that it has its own custom command you can use to define a new snippet, and it will populate the current scope for you to modify as well as making sure to save your snippet in a subfolder based on the current scope.

2 Likes

#3

Hi Terrence, thanks for your fast reply. Here’s the test snippet I created:

hello --> source.html -->

I can’t get the code correctly here, but it’s the default new snippet. tools>developer>new snippet
I set the Tabtrigger to hello
and the scope to source:html.

In a html file I can tab what I want after hello, nothing.
When I change the scope to source:css, and I open an css file, it works.

0 Likes

#4

The reason that works with CSS and not with HTML is because the scope selector for HTML is text.html, not source.html. although for CSS it is indeed source.css.

In general programming languages have a base scope that starts with source and plain text formats (plain text, markdown, HTML, XML, etc) have a base scope that starts with text, although I must admit I’m not 100% sure where that convention comes from (historical?) or why CSS is not classified as text the way HTML is.

In any case, try something like this:

<snippet>
    <content><![CDATA[
Hello, ${1:this} is a ${2:snippet}.
]]></content>
    <tabTrigger>hello</tabTrigger>
    <scope>text.html</scope>
</snippet>
1 Like