Sublime Forum

[Solved] How to automatically set file back to read-only after saving?

#1

Hello, the only results I’ve been able to find so far from the forums and plugins is people wanting to disable being able to edit read-only files and such.

However, I want to have the file automatically set to read-only each time I save it. Currently I can edit the file and save it just fine, but I have to manually go and set it to read-only and it’s a pain.

The reason I want to do this is for editing a game’s .ini file. The game resets the .ini to default if it detects that it isn’t one of the premade graphic options (high, medium, low, etc).

I tweaked the file for maximum performance, and am now improving the graphics one setting at a time (to look as good as possible while still having steady FPS).

As you can imagine, it is pretty tedious having to go back to the file properties and setting to read-only every time.

Are there any plugins that can do this? Or settings that I missed?

–Edit–

So as of right now, I figured out an okay solution.

I installed this plugin (seems to work in ST3 as well): https://packagecontrol.io/packages/ReadonlyWritable

I then learned how to make custom keybinds in ST3. Go to Preferences --> Key Bindings, then in the Default (Windows).sublime-keymap file you write:

[
    {
        "keys": ["ctrl+shift+r"],
        "command": "set_readonly",
        "caption": "Set Readonly"
    }
]

Obviously in the keys section you write whatever keybind you want.

I have to hit the keybind after each save, but that is less of a pain than going to the file properties every time.

–Edit #2

I made a very small Autohotkey script to make it so that when I hit save it runs the read-only keybind for me:

~^s::
Send ^+r
Return

Good enough of a solution for me!

0 Likes

#2

Glad you solved your problem!

For a pure Sublime solution, you could use the Chain Of Command plugin to bind a key directly in Sublime that runs the save command followed by the set_readonly command.

Since you’re using AutoHotKey to make the one key do two things, the advice is superfluous, but I’m including it anyway in case someone comes along and finds this thread and wants to do something similar but are unable/unwilling to use AutoHotKey for any reason.

0 Likes

#3

That worked perfectly, thank you so much! I’d much rather use the pure Sublime solution, one less program/script I have to remember to run!

As a side note, and to possibly help others in the future, I originally ran into problems setting my desired hotkey. I wanted to use Ctrl+Shift+S, which by default is “Save As.” It was taking priority over my Chain of Command keybind that I set in my user keybind file.

I installed PackageResourceViewer and used it to access Default/Default (Windows).sublime-keymap. That allowed me to remove the “global” bind, and replace it with my own from Chain of Command as you instructed.

Credit to skuroda for his instructions.

Thank you again for your help!

0 Likes