簡體   English   中英

Sublime Text:使用一個命令創建一個包含多個子文件的文件夾

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

是否可以創建一個命令,以創建一個文件夾和多個具有相同名稱和不同擴展名的子文件?

例如,我將打開命令並輸入名稱“ desktop-header”

結果將是創建以下內容

桌面頁眉(文件夾)

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

我一直在關注BEM ,最終需要手動創建所有塊。

我什至不知道要搜索什么才能找到答案。

謝謝!

是的,您可以在a命令中編寫任意python代碼。 只需選擇“ 工具”>“開發人員”>“新插件...”,然后粘貼以下代碼。 相對於當前視圖,這將創建一個文件夾,其中包含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)

然后創建如下的綁定:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM