Sublime Forum

Generate Empty Template

#1

image

is it possible that with a click of a button we can generate a template structure inside a newly created folder with →

index.html
script.js
style.css

Files may be empty.

0 Likes

#2

It is possible with a plugin.

import os
import sublime
import sublime_plugin

class GenerateFilesCommand(sublime_plugin.WindowCommand):

    def run(self, dirs):
        index_html = os.path.join(dirs[0], "index.html")
        style_css = os.path.join(dirs[0], "style.css")
        style_js = os.path.join(dirs[0], "style.js")
        with open(index_html, "w+") as index, open(style_css, "w+") as style_css, open(style_js, "w+") as style_js:
            pass

    def is_enabled(self, dirs):
        return os.path.exists(dirs[0]) and len(dirs) == 1

and the corresponding entry in the User/Side Bar.sublime-menu

{
		"caption": "Generate Files",
		"command": "generate_files",
		"args": {
			"dirs": []
		},
},

So all you have to do is right click on any folder, choose Generate Files from the side bar context menu & it will generate those empty files for you.
If those file names are not hard coded, will probably require some more work.

Notes:

  1. Could use the pathlib module to make file creation easier but that’s not an option on the 3.3 runtime without a dependency. On the 3.8 runtime, would be much easier without the context manager hacks as the pathlib module is a part of stdlib.
1 Like