Sublime Forum

Inserting specific code into existing file

#1

Hi everyone,

I am looking for some ideas how to make my life easier with this awesome tool :slight_smile:

Most of the time I am working on some HTML5 ads for my customers, and every single ad needs to have its specific tracking script that goes for the specific website.

So, it takes a lot of time to manually inserting those scripts and I was wondering if anyone has an idea how to automate that.

p.s. some scripts need to be inserted in the HEAD, some after the body and so on. So the script position is not always the same. Is there way to make a snipper with the script and make it search for the BODY start and pasting it there? and so on…

Thank you very much!

0 Likes

#2

By writing a Python plugin, you could try a combination of Python’s builtin

html.parser.Parser

and Sublime’s

view.sel().clear()
region = sublime.Region(text_position_to_insert)
view.sel().add(region)
view.run_command("insert_snippet", {"path": "Packages/User/MySnippet.sublime-snippet"})
0 Likes