Sublime Forum

How can I modify built-in packages/snippets?

#1

This used to be possible in ST2.

In ST3 I have only the User folder (located in C:\Users\USERNAME\AppData\Roaming\Sublime Text 3\Packages on my Windows machine) and the Packages folder (located in the program installaction directory) which contains compiled packages files.

I can add my own snippets, but they do not override existing built-in ones with the same trigger. For example I can define my own if-else (triggerd by “ife”) snippet but it will appear in the dropdown popup under the existing “ife” snippet.

Snippets are key functionality in my case. Is it just my impression or does ST3 really offer much less flexibility when working with snippets?

0 Likes

#2

From my experience, that is exactly how it worked in ST2 as well.
This inconsistency was my major gripe with ST2. Considering that it was possible to override pretty much everything else (preferences, key bindings), and that it’s the whole damn point of having “User” folder to begin with.

As for ST3, these .sublime-package files are just zip archives. You can extract such file to AppData Packages folder (not into User, but on the same level) and modify the file(s) within as necessary. So far the auto-updating seems to leave those untouched, so it can work unlike in ST2.

Although I’d still prefer if things were made consistent.

1 Like

#3

Here is some information on packages in ST3. As drifter mentioned, you can override the contents of the .sublime-package files. This is “better” as updates to ST won’t blow away your changes. Though you could do the whole ignore the default package and create a new one with the modifications as you would in ST2.

sublimetext.com/docs/3/packages.html

To make it a little easier to edit/view files from the sublime-package, I wrote PackageResourceViewer. “Editing” will create the file in the proper location, so you don’t have to manually extract everything.

1 Like

#4

That documentation seems to be wrong though.

"Packages may also be stored unzipped within a directory, or a mix of the two: any loose files in the package directory will override files stored in the .sublime-package file. "
“To override a file in an existing package, just create a file with the same name under the Packages/ directory.”
Etc.
It implies you can modify or add just the files you need.
But the reality is that you have to extract the entire package. What you have in Packages/ folder is all you will get, because .sublime-package is then entirely ignored.

0 Likes

#5

[quote=“drifter”]That documentation seems to be wrong though.

"Packages may also be stored unzipped within a directory, or a mix of the two: any loose files in the package directory will override files stored in the .sublime-package file. "
“To override a file in an existing package, just create a file with the same name under the Packages/ directory.”
Etc.
It implies you can modify or add just the files you need.
But the reality is that you have to extract the entire package. What you have in Packages/ folder is all you will get, because .sublime-package is then entirely ignored.[/quote]

Really? I just did a quick test replacing the Java main snippet (well added some random text). I was then able to call a snippet from the originally defined Java.sublime-package file that I did not redefine. It inserted the correct contents both times. I did this on W7 x64.

0 Likes

#6

@skuroda
Hmm, then it’s some inconsistent behavior again. My case:

Extracted and modified just Packages\HTML\html.sublime-snippet
Restart sublime, create a new html file, type html+tab, get my new snippet. But things like tag completions are missing (ie, type div+tab, get div and a tab).
Extracted entire HTML.sublime-package to Packages\HTML, modified html.sublime-snippet, everything works.

Edit: Although I wasn’t correct saying that .sublime-package gets entirely ignored. Things like syntax highlighting still work.

0 Likes

#7

Well I wasn’t restarting before, so that might explain it. When I follow the steps you outline, I see the same behavior. When I don’t restart the editor, I get the expected behavior. This is definitely a bug that should be addressed. I would think the original intent was to modify select files of a package. Hmm, I’d like to report this to jps to get this addressed, though I don’t know where the best way to get his attention is. I’ll add a post to the latest dev release. But I don’t know if he will look at it. That being said, I’ll probably repost it when the next dev release occurs.

edit:
My java example works fine. So I took a look at the console. There are errors loading the .py files when you override the snippet.

Traceback (most recent call last): File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 73, in reload_plugin m = importlib.import_module(modulename) File "X/importlib/__init__.py", line 88, in import_module File "<frozen importlib._bootstrap>", line 1577, in _gcd_import File "<frozen importlib._bootstrap>", line 1558, in _find_and_load File "<frozen importlib._bootstrap>", line 1522, in _find_and_load_unlocked ImportError: No module named 'HTML.encode_html_entities' reloading plugin HTML.html_completions Traceback (most recent call last): File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 73, in reload_plugin m = importlib.import_module(modulename) File "X/importlib/__init__.py", line 88, in import_module File "<frozen importlib._bootstrap>", line 1577, in _gcd_import File "<frozen importlib._bootstrap>", line 1558, in _find_and_load File "<frozen importlib._bootstrap>", line 1522, in _find_and_load_unlocked ImportError: No module named 'HTML.html_completions'

0 Likes

#8

Apart from the bug you spotted it beats me why default snippets should take precedence over the ones created by the user. Wouldn’t it be easier to just merge snippets in User directory with default .sublime-package files in such a way that would promote user snippets?

If I create a snippet, I don’t do it to have an alternative one but to override the default. In 99% of cases I wouldn’t need the default even as a second choice. And if I don’t want to use my snippet anymore I just remove it and fall back to the default one.

Thank you guys for the explanation. I will extract and modify the appropriate files. However this isn’t exactly what I’d call flexibility and ease of use.

I would like to be able to just move the contens of User folder whenever I install ST anew and have everything work as before. Now I will have to remember to move entire /Packages folder.

0 Likes

#9

I think the issue is that snippets, when loaded, are not mapped to a file name. The key bindings, menus, etc all have common file names. While iterating through these packages, a specific file name is found, then merged. By comparison, snippets file names are arbitrary.

I’ll propose an alternative solution though it is still less than ideal. Consider ignoring the default HTML (and whatever other language for that matter) package by default. You can then create a “custom” package with your defined languages. Then, add that repository to package control, and install via package control. By doing this, you would not have to worry about moving the entire packages directory when you move machines. The downside is that you will not see the any updates. But if you are doing a full extraction, you wouldn’t see them anyways. Anyways, just a thought to avoid moving the entire packages directory every time. Just as an example, here is facelessuser’s repo of updated language files.

github.com/facelessuser/sublime-languages

0 Likes

#10

[quote=“skuroda”]Here is some information on packages in ST3. As drifter mentioned, you can override the contents of the .sublime-package files. This is “better” as updates to ST won’t blow away your changes. Though you could do the whole ignore the default package and create a new one with the modifications as you would in ST2.

sublimetext.com/docs/3/packages.html

To make it a little easier to edit/view files from the sublime-package, I wrote PackageResourceViewer. “Editing” will create the file in the proper location, so you don’t have to manually extract everything.[/quote]

Thank you so much!! That solved the problem, but also showed me why I was experiencing the same problem as TS. In windows 64 I tried creating the folder where I installed Sublime Text 3.
C:\Programs\Sublime Text 3\Packages.
It seemed to make sense because of the “Package” folder which contains the original sublims-package file I was editing.
I was not aware I had to put it somewhere else:
C:Users\me\AppData\Roaming\Sublime Text 3\Packages\

I’m posting this in case someone else got it confused too. Save yourself the trouble, use the plugin skuroda made.

0 Likes

#11

So, is there any update on this one?
Anyone contacted the ST3 devs?

I would really like to override some built in snippets, but I keep seeing them in the auto-complete drop-down.

0 Likes

#12

How have you tried overriding the snippets? The PackageResourceViewer will allow you to (while inefficiently) override any snippets you want. I’m currently working on some other stuff, but I’d like to extend the plugin to allow some scriptable action to be run.

0 Likes

#13

I mean overwriting by creating your own Snippet file somewhere under User folder that has the same as the original snippet.
This worked ok in ST2.

0 Likes

#14

PackageResourceViewer was exactly what I needed. Thank you!

1 Like

#15

that was what i was looking for. So, I was confuse with how to use it because i’m new but I found That:

Once you’ve installed the plugin:

hit CtrlShiftP to open the command palette
type prv to get the Package Resource Viewer: options
select Package Resource Viewer: Open Resource

1 Like

#16

Same, thanks a lot. It really helped.

0 Likes

#17

It was exactly what I needed. Thanks a lot!

0 Likes

#18

I have to register to reply to your post. Your post help me a lot. Thank you!

0 Likes

#19

@metaraine, @juanmiguel431, This exactly what’s needed!

Now I could configure MarkDown Preview perfectly.

0 Likes

#20

Perfect. Remember to set "single_command": false under Package​Resource​Viewer Settings - User, to allow editing MarkdownPreview.sublime-settings.

0 Likes