Sublime Forum

Activate snippet from plugin for a new view

#1

Hi all,

I want to create a new “template” so when I create some documents I preload some content. I have read this is better done with a snippet.

The snippet works via the TAB key. However, I cannot get it activated from my plugin to preload content in the new file. The code gets executed but the “run_command” does nothing. How can I make this work?

  • C:\Users…\AppData\Roaming\Sublime Text 3\Packages\MyPlugin\templ.sublime-snippet:
    <snippet>
    	<content><![CDATA[
    Hello, ${1:this} is a ${2:snippet}.
    ]]></content>
    	<tabTrigger>hello</tabTrigger>
    	<description> demo description </description>
    </snippet>
  • C:\Users…\AppData\Roaming\Sublime Text 3\Packages\MyPlugin\myplugin.py
def on_done(...):
        ...
        open(path, 'a').close()
        new_view = self.window.open_file(path)
        new_view.run_command("insert_snippet", {"name" : "Packages/MyPlugin/templ.sublime-snippet"})
0 Likes

#2

have you tried decoupling it from opening a file? i.e. just typing view.run_command… in the ST python console? do any errors appear there? (maybe the file hasn’t fully opened when that code runs. In which case, you could maybe use set_timeout_async to query when the view has finished loading and then insert the snippet)

0 Likes

#3

It worked! Thanks!

0 Likes