简体   繁体   中英

Sublime Text: Create a folder with multiple children files with one command

Is it possible to create a command that would create a folder and multiple children files with the same name and different extensions?

For example I would open the command and type in a name of "desktop-header"

the result would be the following being created

desktop-header (folder)

  • desktop-header.js
  • desktop-header.php
  • _desktop-header.sass

I've been following BEM and end up needing to create all the blocks manually.

I'm not even sure what to search to see if there is an answer to this.

thanks!

Yes, you can write arbitrary python code in the a command. Just select Tools > Developer > New Plugin... and paste the following code. This will create a folder, which contains the 3 files, relatively to the current view.

import os

import sublime
import sublime_plugin


class CreateBemFilesCommand(sublime_plugin.WindowCommand):
    def run(self):
        window = self.window
        view_path = window.active_view().file_name()
        if not view_path:
            sublime.error_message("Save your file first.")
            return
        base_folder_path, _ = os.path.split(view_path)

        def on_done(name):
            folder_path = os.path.join(base_folder_path, name)
            # add your file names
            file_names = [
                name + ".js",
                name + ".php",
                "_" + name + ".sass"
            ]
            file_paths = [
                os.path.join(folder_path, file_name)
                for file_name in file_names
            ]
            # create the folder
            os.makedirs(folder_path, exist_ok=True)
            # create the files
            for file_path in file_paths:
                with open(file_path, "a"):
                    pass
                # open the files in Sublime Text
                window.open_file(file_path)

        window.show_input_panel(
            "Input the folder name", "", on_done, None, None)

afterwards create a keybinding like this:

{
    "keys": ["alt+shift+b"],
    "command": "create_bem_files",
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM