We use Sublime Text 3 in a business environment to edit several custom config text files. Some of our text files contain data that can get corrupted if we use Sublime Text. Is it possible to somehow get Sublime Text 3 to completely refuse to open/edit files with specific extensions?
Stop Sublime opening specific file types
Have you looked at the file_exclude_patterns
setting in preferences settings file ? If you want to say, exclude text files from appearing in the sidebar then setting file_exclude_patterns: ["*.txt"]
will exclude all text files from appearing (they still will be on disk but Sublime will not show them in the sidebar if a folder contains any such files).
At least you can do it via a custom plugin.
import sublime
import sublime_plugin
class StopOpenExts(sublime_plugin.ViewEventListener):
forbidden_exts = [".forbidden"]
def on_load(self):
file_path = self.view.file_name() or ""
if any(file_path.endswith(ext) for ext in self.forbidden_exts):
sublime.error_message('Forbidden file extension: "{}"'.format(file_path))
self.view.close()
file_exclude_patterns
may be needed too.
Thanks for the reply, however, I need to completely stop people from being able to open specific file types
Thanks jfcherng. I have implemented the code you provided, however I get the below error in the console:
File âC:\Users\######\AppData\Roaming\Sublime Text 3\Packages\User\ForbiddenFiles.pyâ, line 3, in
class StopOpenExts(sublime_plugin.ViewEventListener):
AttributeError: âmoduleâ object has no attribute âViewEventListenerâ
I have no idea whatâs happening since itâs a ST built-in API class unless you are using ST < 3124 somehow (I donât see a reason why not use ST 3211). So
Thanks again. I had an older version of Sublime Text 3. This is now working for me.
This seems to be working when:
-
Dragging a forbidden file into Sublime
-
Browsing to a forbidden file from within Sublime
However it doesnât work when:
-
Right clicking on the file and selecting âSublime Textâ from the âOpen Withâ sub menu
(The 1st time Sublime Text loads, it will open the file, any further attempts to do this while Sublime Text is open will produce the error) -
When setting the forbidden file type to open with Sublime Text as the default application in Windows, simply double clicking the file to open it will still allow it to open. (This method never hits the error)
I would appreciate any further assistance you can provide.
Thatâs because plugins are loaded later while the file gets loaded first when ST starts from fresh.
import sublime
import sublime_plugin
FORBIDDEN_EXTS = [".forbidden"]
def close_forbidden_view(view):
file_path = view.file_name() or ""
if any(file_path.endswith(ext) for ext in FORBIDDEN_EXTS):
sublime.error_message('Forbidden file extension: "{}"'.format(file_path))
view.close()
def plugin_loaded():
for window in sublime.windows():
for view in window.views():
close_forbidden_view(view)
class StopOpenExts(sublime_plugin.ViewEventListener):
def on_load(self):
close_forbidden_view(self.view)
I get error on loading the plugin:
Traceback (most recent call last):
File âC:\Program Files\Sublime Text 3\sublime_plugin.pyâ, line 196, in load_module
m.plugin_loaded()
File âC:\Users\#User#\AppData\Roaming\Sublime Text 3\Packages\Default\ForbiddenFiles.pyâ, line 16, in plugin_loaded
close_forbidden_views(view)
NameError: global name âclose_forbidden_viewsâ is not defined