I have looked for this a lot already and every answer is about Vim in Linux. Is there a way to save a read-only file right from Vim in Windows?
As on other vi/vim versions, you can use
:w! to force the write, as long as you have permissions to override the read-only property.
For example, if you have set the file as read-only, the above will work. If Windows itself or an administrator has set it read-only, you will need another option.
As @dash-tom-bang commented, one good example is within Program Files, where files are protected by default. This Microsoft knowledgebase article explains how that works in practice.
My account is set as the Admin but
:w!is not working. It says can't open file for reading. Though i can do the exact same thing outside of vim. Feb 23, 2016 at 12:17
1@DurgaSwaroop: Have you tried opening
Run as Administrator? Perhaps it would solve your purpose... or otherwise it might be possible that the file is being locked by another application...– CylianFeb 24, 2016 at 9:30
1@DurgaSwaroop: The parent process must be
run as administratorto complete its sub-process involving administrative tasks to do... Please have a look at windows.microsoft.com/en-in/windows/… .... otherwise disable
UACsettings....– CylianFeb 24, 2016 at 9:38
1Editing files inside of the Program Files hierarchy for instance,
:w!doesn't fly and you must run gVim as an administrator or run Vim in an command prompt that was run as administrator. Feb 24, 2016 at 23:14
1Thanks @dash-tom-bang, I've added reference to that good example in my answer.– rolingerFeb 25, 2016 at 13:55
You can use my SudoEdit plugin.
Does this work on windows properly? In the Readme file it says it probably won't. Feb 24, 2016 at 9:37
From my tests it should. I don't use Windows often however and I have never gotten any feedback for that. That is the reason for the readme. It also depends on the what method you use. Feb 24, 2016 at 18:20
There's another possibility that has not been addressed in the other answers: vim will refuse to save a file if it is open and locked by another Windows application. Many ordinary Windows apps (Office, for example) lock files that they open. If this is the problem, no amount of messing with permissions or :w! will help you. You simply need to close the file in the other app, and then you will be able to save it from vim. It is unfortunate that vim does not clearly signal this situation to the user.
For win7: Start > Run > Cmd (as Admin), and run these commands to make the dir tree writable by admin:
cd parent/folder :: /f file, /a admin, /r recurse, /d y = nopromt takeown /F . /A /R /D Y > nul: :: /t /q /c .. traverse quiet continue icacls . /t /q /c /reset icacls . /t /q /c /grant :r Everyone:F icacls . /t /q /c /setowner Administrators
Run gVim as an administrator, e.g. by right-clicking on, or using the Shift+F10 keyboard shortcut on, the gVim shortcut in your Start menu and picking Run as an administrator.
Open the file you want to edit and save your changes using
:w as normal.
I use gvim to edit
C:\Windows\System32\drivers\etc\hosts after setting its permissions to "full control", the trick involved setting "inherit permissions" to "no" first. Then it started working without ever the "!". I had also run
attrib but that was not effective.
:f "newfilename" in command mode then when leaving
:w to save under the new file name.