Sublime Forum

How can I clean override snippets in Sublime Text 3

#1

I started to program C because of University, and I wanna be able to make my own snippets. What Im looking for is not a way to replace the current snippets that ST3 has for C, I wanna override them in a way that I can implement my version of the For Loop (for example) in a way that I dont edit theirs. And I wanna be also able to add my own snippets too.

Is there a way to do this? Or I need to add my own snippets? Like my own For Loop snippet? Because if I do that whenever I type in “for” instead of 1 suggestion, I have 2, my custom snippet and the ST3 snippet, with my snippet coming in 2nd.
I just wanna override them to prevent that, and still be capable to add my owns without any problems :smiley:

Thank you very much,
PatriqDesigns

0 Likes

#2

It’s a little involved, but doable. The gist of it is that any file in any .sublime-package can be overridden by placing a file of the same name in your local Packages directory.

Here’s an example of doing it for the ‘main’ snippet in C:

  1. Determine the file name:
    a) Enter sublime.log_commands(True) in the console
    b) Insert the snippet via the command palette (important, not via the tab trigger), this will show the filename you need in the console. You’ll see this:

command: insert_snippet {“name”: “Packages/C++/010-main()-(main).sublime-snippet”}

  1. Write the new snippet. For example:
<snippet>
    <description>main()</description>
    <content><![CDATA[
int main(int argc, char ** argv)
{
    $0
}
]]></content>
    <tabTrigger>main</tabTrigger>
    <scope>source.c++</scope>
</snippet>
  1. Save it to the name indicated above: Packages/C++/010-main()-(main).sublime-snippet

You can find the Packages directory on your computer via the Preferences/Browse Packages menu item. Note that the file must go in the ‘C++’ package, and not the ‘User’ package.

0 Likes

#3

I recommend using PackageResourceViewer for this. It’s as simple as opening a file with it, doing changes and then saving.

However, it’s still a bit annoying if you have many snippets to override or want to effectively “remove” all the default ones (by replacing with empty files). There is currently no other way that I know of to correctly remove all snippets without each ST upgrade undoing the changes, e.g. by removing all snippets from the .sublime-package.

0 Likes