创建文件时自动创建同名文件夹
Automatically create folder of same name on file creation
有什么办法可以设置sublime在创建某些文件时自动创建同名文件夹
我创建的着陆页的文件名都带有 lp_
前缀,我想观察何时创建具有此名称的文件,然后在另一个目录中自动创建同名文件夹(对于 css 和图像)。
这是否可以通过插件或 Grunt 之类的东西实现?
示例:
创建文件:lp_test.php
自动创建文件夹:/lp/lp_test/
您可以创建一个扩展 EventListener
并覆盖(例如)on_post_save_async
的插件。您可以使用这个简单的示例作为基础:
import sublime, sublime_plugin, os
# We extend event listener
class ExampleCommand(sublime_plugin.EventListener):
# This method is called every time a file is saved (not only the first time is saved)
def on_post_save_async(self, view):
variables = view.window().extract_variables()
fileBaseName = variables['file_base_name'] # File name without extension
path = 'C:/desiredPath/css/' + fileBaseName
if fileBaseName.startswith('lp_') and not os.path.exists(path):
os.mkdir(path)
编辑: 将 on_post_save
更改为 on_post_save_async
,因为它在不同的线程中运行并且不会阻止应用程序。感谢 发表评论并添加 python 突出显示。
有什么办法可以设置sublime在创建某些文件时自动创建同名文件夹
我创建的着陆页的文件名都带有 lp_
前缀,我想观察何时创建具有此名称的文件,然后在另一个目录中自动创建同名文件夹(对于 css 和图像)。
这是否可以通过插件或 Grunt 之类的东西实现?
示例:
创建文件:lp_test.php
自动创建文件夹:/lp/lp_test/
您可以创建一个扩展 EventListener
并覆盖(例如)on_post_save_async
的插件。您可以使用这个简单的示例作为基础:
import sublime, sublime_plugin, os
# We extend event listener
class ExampleCommand(sublime_plugin.EventListener):
# This method is called every time a file is saved (not only the first time is saved)
def on_post_save_async(self, view):
variables = view.window().extract_variables()
fileBaseName = variables['file_base_name'] # File name without extension
path = 'C:/desiredPath/css/' + fileBaseName
if fileBaseName.startswith('lp_') and not os.path.exists(path):
os.mkdir(path)
编辑: 将 on_post_save
更改为 on_post_save_async
,因为它在不同的线程中运行并且不会阻止应用程序。感谢