简体   繁体   English

Sublime Text:使用一个命令创建一个包含多个子文件的文件夹

[英]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" 例如,我将打开命令并输入名称“ desktop-header”

the result would be the following being created 结果将是创建以下内容

desktop-header (folder) 桌面页眉(文件夹)

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

I've been following BEM and end up needing to create all the blocks manually. 我一直在关注BEM ,最终需要手动创建所有块。

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. 是的,您可以在a命令中编写任意python代码。 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. 相对于当前视图,这将创建一个文件夹,其中包含3个文件。

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",
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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