Sublime Forum

Making Sublime Text 3 Read Only


Is there a way to make Sublime read only? Many times I’m in vs2019 doing a debug session and I keep Sublime up on my second monitor to use as a reference. I’ll be stepping through a file in VS and sometimes switch over to the same source file in Sublime to look up something. Then I can forget to switch focus back to VS…

Anyway, it would be nice to be able to “lock” Sublime into a read only mode.



There’s not a direct feature that does it, but a plugin can make a view read-only. For example if you use View Package File to open a package file, anything that comes from a sublime-package file is marked as read only as an indication that you can’t modify it.

One way to do this would be something like the following plugin:

import sublime
import sublime_plugin

class ToggleReadOnlyCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        view = sublime.active_window().active_view()
        view.set_read_only(not view.is_read_only())

    def is_checked(self):
        view = sublime.active_window().active_view()
        if view and view.is_read_only():
            return True

        return False

    def is_enabled(self):
        return sublime.active_window().active_view() is not None

That implements a toggle_read_only command that flips the state of a file. You could then bind that to a key as desired, or add it to the command palette. You could also create a Context.sublime-menu file in your User package like the following to add it to the context menu:

    { "command": "toggle_read_only", "caption": "Read-Only", "mnemonic": "R", "checkbox": true },

How you ultimately set this up depends on how you’d like it to work. For example, the command could toggle the state of every file in the current window (or force it to be one way or the other). You could also extend it so that once this feature is “turned on”, anything that’s opened in that window is automatically made read-only without your having to do anything.

You could also set it up to automatically mark files as read-only when they open based on their location on disk as well.

This example does it a file at a time under the theory that if you’re opening a file like this you perhaps know in advance that this is definitely a file you don’t want to modify, which leaves you free to edit other things as needed.



Thanks Terence!