Sublime Forum

Stop Sublime opening specific file types

#1

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?

0 Likes

#2

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).

0 Likes

#3

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.

1 Like

#4

Thanks for the reply, however, I need to completely stop people from being able to open specific file types

0 Likes

#5

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’

0 Likes

#6

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 :man_shrugging:

0 Likes

#7

Thanks again. I had an older version of Sublime Text 3. This is now working for me.

0 Likes

#8

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.

0 Likes

#9

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)
0 Likes

#10

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

0 Likes

#11

revised…

0 Likes

#12

Thank you very much for your help. Much appreciated :smiley:

0 Likes