Save file as root after editing as non-root
Depending on the extent of your changes, it might be faster to save (:w
) your file with a different name, and then use sudo
and cat
to overwrite the content of the original file:
sudo sh -c 'cat changed > file'
Note that both cp
and mv
will replace the original file and its attributes (ownership, permissions, ACLs) will be lost. Do not use them unless you know how to fix the permissions afterwards.
Try
:w !sudo tee "%"
The w !
takes the entire file and pipes it into a shell command. The shell command is sudo tee
which runs tee
as superuser. %
is replaced with the current file name. Quotes needed for files that have either spaces or any other special characters in their names.
Save the file elsewhere (like your home folder) and then sudo mv
it to overwrite the original?