Sublime Forum

Built in sublime text snippet tempalte

#1

Hello, I wanted to know, if there is a way to edit the file that is brought up after I want to create a new snippet, i.e. there’s no <description></desription> tag, and I don’t want to manually insert this line every single time. Is there a way to edit this code block to fit my needs?

`

`

0 Likes

#2

The templates used for build systens, plugins, snippets and syntaxes are in the Default package; you can view them by using View Package File from the command palette and selecting Default/new_templates.py as the file to view.

That file is read-only and a part of a package that ships with Sublime, so modifying it directly it’s a good idea. If you use PackageResourceViewer you can use it to edit just that file and make an override to make the change you want.

Another thing you can do is select Tools > Developer > New Plugin... from the menu, then replace the stub content with the following and save it in the default location as something like my_new_snippet_template.py or something like that (basically, something descriptive of what it does):

import sublime
import sublime_plugin

import os

from Default.new_templates import reformat


class MyNewSnippetCommand(sublime_plugin.WindowCommand):
    def run(self):
        v = self.window.new_file()
        v.settings().set('default_dir', os.path.join(sublime.packages_path(), 'User'))
        v.settings().set('default_extension', 'sublime-snippet')
        v.assign_syntax('Packages/XML/XML.sublime-syntax')

        template = reformat(
            """
            <snippet>
            \t<content><![CDATA[
            Hello, \${1:this} is a \${2:snippet}.
            ]]></content>
            \t<!-- Optional: Set a description to describe the snippet -->
            \t<!-- <description>description</description> -->
            \t<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
            \t<!-- <tabTrigger>hello</tabTrigger> -->
            \t<!-- Optional: Set a scope to limit where the snippet will trigger -->
            \t<!-- <scope>source.python</scope> -->
            </snippet>
            """)
        v.run_command("insert_snippet", {"contents": template})


class TemplateListener(sublime_plugin.EventListener):
    def on_window_command(self, window, cmd, args):
        if cmd == "new_snippet":
            return ("my_new_snippet", args)

This defines a new command that is based on the original but with a modified template with extra content. The EventListener will catch attempts to use the built in command and tell Sublime to use the new command instead, so that the new command will seamlessly be used any time something tries to use the default one.

1 Like