Sublime Forum

Activate snippet from plugin for a new view


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:
    Hello, ${1:this} is a ${2:snippet}.
    	<description> demo description </description>
  • C:\Users…\AppData\Roaming\Sublime Text 3\Packages\MyPlugin\
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"})


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)



It worked! Thanks!