Sublime Forum

How to force save a readonly file

#1

Hi there,

is there any way to force saving a readonly tagged file? I use ST2 on Mac and sometimes I edit files extracted from a Windows box and they are marked as readonly. Any other editor I have used before give me the option to force save but ST only say’s “Unable to save file xxxxx”.

Thanks in advance,

Ricardo

0 Likes

#2

Well, just in case any of you has the same problem … I’ve done a plug-in that does the trick:

import sublime, sublime_plugin
import os, stat

class ForceSaveCommand(sublime_plugin.WindowCommand):
	def run( self ):

		myFile = self.window.active_view().file_name()
		fileAtt = os.stat(myFile)[0]
		myPlatform = os.name

		if (myPlatform == 'nt'):
			if (not fileAtt & stat.S_IWRITE):
				print "Making "+myFile+" writable"
				os.chmod(myFile, stat.S_SIWRITE)
		else:
			if (fileAtt & stat.UF_IMMUTABLE):
				print "Making "+myFile+" mutable"
				os.chflags(myFile, not stat.UF_IMMUTABLE)
		
		self.window.active_view().run_command('save')

Then create a User Key Binding that supersedes the global save shortcut:

MacOS

{ "keys": "super+s"], "command":"force_save"}

Windows

{ "keys": "ctrl+s"], "command":"force_save"}

It works for both Windows XP and MacOS.

0 Likes