I would like to edit files like C:\Widows\System32\Drivers\etc\hosts that need administrative privilege to be modified.

How can save the file on Windows if Vim has not been starter with administrator privileges?

  • 1
    I wonder if you can use a PowerShell command to clobber the file with contents coming from stdin and then use the suggestions in stackoverflow.com/q/7690994/9447571 to run it with elevated privileges, if so you could use :w !… to write the contents to that command and have it write to the file. (This would be akin to the :w !sudo tee % >/dev/null solution for Linux/Unix.)
    – filbranden
    Aug 6, 2022 at 14:58
  • Thanks. The suggestion mention elevated privilege but what is I need another account (user) ? Aug 6, 2022 at 15:41

3 Answers 3


Some time ago I wrote a small utility xmove.

Basically, it saves stdin into temporary file and re-runs itself with elevated privileges. Finally, the second instance moves temporary file to the proper destination.

So this is supposed to do the trick: :w !xmove % and then set nomod. You can pull, build and check if it still works.

  • Thanks. I'll have a look. But what is I need another account (user) to write the file? Aug 6, 2022 at 15:48
  • 1
    @VivianDeSmedt If you want to impersonate a user other than administrator you need to add that feature yourself.
    – Matt
    Aug 6, 2022 at 15:57
  • Thanks! I have no access to my development environment this week. I'll add personalization when I come back. I'll keep you informed. Aug 6, 2022 at 16:17

I have created the SudoEdit vim plugin, that allows to elevate using several different methods (including UAC) and that should also work on Windows.

  • Hi @Christian, I have tried to use the solution on Windows but I face some problems. With the SudoWrite command I get the following error message: Line 118: EW37: No write since last change (add ! to override) but adding '!' doesn't help. With the SudoRead command a copy of the file is created in C:\Users\Public but the code seems to try to access the folder C:\Users\Public instead of the file and fails. Aug 13, 2022 at 20:02
  • can you please create an issue about this? I can then look into it Aug 15, 2022 at 6:15
  • Hi @Christian I have created an issue and a Pull Request with my proposed changes. Unfortunately that doesn't solve my problem entirely since to change C:\Windows\System32\Drivers\etc\hosts I need both another user and the elevated mode and it seems that runas provide only the first. Aug 15, 2022 at 8:03

The straightforward solution would be to save the file to some other location (where you do have write access), like \temp or \Users\<your-user-name>\Documents, with the :w <pathname> command, and then go to a Command Prompt or PowerShell that’s running with elevated privileges (running as Administrator) and copy your temporary file over the target.  If you need to use the privileges of some other user (other than Administrator), use runas to start the Command Prompt or PowerShell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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