Sublime Forum

Saving a read-only file

#1

If I open a read-only file in TextMate or BBEdit, make changes and save I get a prompt that the file was opened read-only and do I want to overwrite the file.

However, in ST it just generates an error that the file couldn’t be saved.

Could we get the behavior found in the other editors?

0 Likes

#2

Just started testing Sublime yesterday. So far I do really like it but this behavior for read-only files is really annoying.

Using it with perforce is pretty much impossible as I have to manually remove read only flag by saving files as then looking for the file and editing its permissions in windows. Notepad++ makes this much easier by not only blocking edits while its read only but you can right click on the tab and toggle the read only status of an open file very easily.

Tried to find something similar in sublime but failed…

KPr

0 Likes

#3

Any ideas on how to improve on the current limitations with read only files?

0 Likes

#4

I had the same problem, so I wrote a simple event handler.
On save it will check if the file is read-only and will ask if user wants to overwrite it anyway.

Just save it into PreSave.py in sublime’s Packages\User folder and it should just work.

import sublime, sublime_plugin, os, stat

class PreSaveCommand(sublime_plugin.EventListener):
	def on_pre_save(self, view):
		myFile = view.file_name()
		fileAtt = os.stat(view.file_name())[0]
		if view.is_dirty():
			if (not fileAtt & stat.S_IWRITE):
				if(sublime.ok_cancel_dialog('The file is Read-Only. Overwrite?', 'Overwrite!')):
					os.chmod(myFile, stat.S_IWRITE)
0 Likes

#5

[quote=“xapoh”]I had the same problem, so I wrote a simple event handler.
On save it will check if the file is read-only and will ask if user wants to overwrite it anyway.

Just save it into PreSave.py in sublime’s Packages\User folder and it should just work.

[code]
import sublime, sublime_plugin, os, stat

class PreSaveCommand(sublime_plugin.EventListener):
def on_pre_save(self, view):
myFile = view.file_name()
fileAtt = os.stat(view.file_name())[0]
if view.is_dirty():
if (not fileAtt & stat.S_IWRITE):
if(sublime.ok_cancel_dialog(‘The file is Read-Only. Overwrite?’, ‘Overwrite!’)):
os.chmod(myFile, stat.S_IWRITE)

[/code][/quote]

although I can’t imagine that under any situation I would edit a read-only file, that’s good code.

0 Likes

#6

It would be nice to hook postsave and let the file in the original sttus.

0 Likes

#7

Actually, I needed files to stay not read-only. That way later I can easily find files that were modified by me (not received from VCS) to check them in.

Code to re-set the read-only attribute is about the same as clearing it, so I’ll leave it as an exercise for the reader. :wink:

0 Likes

#8

I added one more even listener to the plugin.
I am using the TFS and if the read-only attribute was cleared for the file, later I’d have to find the file, properly check it out and commit. Of course, I have the Sublime TFS plugin installed. But it is hard to remember which files were already checked out.
In order to make it little easier, a new listener was added to the plugin. Now, if a read-only file is being edited, the plugin will prompt if user wants to check the file out first. After that check out will continue in the background (and in the Sublime status bar) and user can continue editing the file.
To avoid re-prompting on every keystroke, buffer_id will be saved and user will not be asked about the view again.

import sublime, sublime_plugin, os, stat, sublime_tfs

known_views = {}
class CustomFileCommands(sublime_plugin.EventListener):
    def on_modified(self, view):
        global known_views
        id = view.buffer_id()
        myFile = view.file_name()
        fileAtt = os.stat(view.file_name())[0]
        if (not (id in known_views) and (not fileAtt & stat.S_IWRITE)):
            known_views[id] = True
            if(sublime.ok_cancel_dialog('The file is Read-Only. Check out?', 'Go ahead!')):
                tfs = sublime_tfs.TfsCheckoutCommand(view)
                tfs.run(view)

	def on_pre_save(self, view):
		myFile = view.file_name()
		fileAtt = os.stat(view.file_name())[0]
		if view.is_dirty():
			if (not fileAtt & stat.S_IWRITE):
				if(sublime.ok_cancel_dialog('The file is Read-Only. Overwrite?', 'Overwrite!')):
					os.chmod(myFile, stat.S_IWRITE)
0 Likes

#9

[quote=“xapoh”]I had the same problem, so I wrote a simple event handler.
On save it will check if the file is read-only and will ask if user wants to overwrite it anyway.

Just save it into PreSave.py in sublime’s Packages\User folder and it should just work.
[/quote]

Unfortunately this doesn’t seem to work with ST3 - any idea why not, and any chance of an update? Thanks in advance.

0 Likes

#10

After a bit of research into plugin basics I found the problem - mysterious indentation problems from the copy-n-paste of the plugin, there was an error loading the plugin in the console ctrl ~. It now works as advertised.

0 Likes

#11

creepy Actually, I needed files to stay not read-only. That way later I can easily find files that were modified by me (not received from VCS) to check them in. creepy

If you are using Git, you could use:
git log --no-merges --stat --author=“Pattern” --name-only --pretty=format:"" | sort -u

Taken from: stackoverflow.com/questions/6349 … s-modified. There are some other great suggestions/advices, which might do the trick for what you want to achieve.

0 Likes

#12

Quick comment on mac(os x 10.8.5) I noticed that without maintaining the current file attrib the file becomes write only, so no read. The following fixes that by just adding write the current attribs map for the user by or’ing the IWRITE with the existing fileAtt.

import sublime, sublime_plugin, os, stat

class PreSaveCommand(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        myFile = view.file_name()
        fileAtt = os.stat(view.file_name())[0]
        if view.is_dirty():
            if (not fileAtt & stat.S_IWRITE):
                if(sublime.ok_cancel_dialog('The file is Read-Only. Overwrite?', 'Overwrite!')):
                    os.chmod(myFile, stat.S_IWRITE | fileAtt)
0 Likes

#13

This latest version ALMOST works perfectly, but I need the file to end up being set read-only like it was before I started. How can I change the plugin?

Never mind, it would seem that Visual Studio ALSO leaves the file no longer read-only.

0 Likes

#14

In case anyone is wondering why I need to do this, we are using IBM ClearCase and VS 2012 and so far the version control does not fully integrate so we work in VS in “temporarily offline” mode. And I despise VS and want to work in ST3.

0 Likes