8

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?

11

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. – Durga Swaroop Feb 23 '16 at 12:17
  • 1
    @DurgaSwaroop: Have you tried opening gvim as Run as Administrator? Perhaps it would solve your purpose... or otherwise it might be possible that the file is being locked by another application... – Cylian Feb 24 '16 at 9:30
  • 1
    @DurgaSwaroop: The parent process must be run as administrator to complete its sub-process involving administrative tasks to do... Please have a look at windows.microsoft.com/en-in/windows/… .... otherwise disable UAC settings.... – Cylian Feb 24 '16 at 9:38
  • 1
    Editing 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. – dash-tom-bang Feb 24 '16 at 23:14
  • 1
    Thanks @dash-tom-bang, I've added reference to that good example in my answer. – rolinger Feb 25 '16 at 13:55
2

You can use my SudoEdit plugin.

  • Does this work on windows properly? In the Readme file it says it probably won't. – Durga Swaroop Feb 24 '16 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. – Christian Brabandt Feb 24 '16 at 18:20
2

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.

0

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 
0

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.

-1

Type :f "newfilename" in command mode then when leaving :w to save under the new file name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.