2017-03-12 25 views
0

是否可以創建一個命令來創建具有相同名稱和不同擴展名的文件夾和多個子文件?Sublime Text:使用一個命令創建包含多個子文件的文件夾

例如我將在「桌面報頭」的名稱打開命令和類型

其結果將是下面所創建

桌面報頭(文件夾)

  • 桌面header.js
  • 桌面的header.php
  • _desktop-header.sass

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

我甚至不知道要搜索什麼來查看是否有答案。

謝謝!

回答

2

是的,你可以在一個命令中編寫任意的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", 
}, 
相關問題