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?
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.
I have created the SudoEdit vim plugin, that allows to elevate using several different methods (including UAC) and that should also work on Windows.
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.
Commented
Aug 13, 2022 at 20:02
C:\Windows\System32\Drivers\etc\hosts
I need both another user and the elevated mode and it seems that runas provide only the first.
Commented
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.
: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.)