Vim – Re-Edit as Root

rootsudovimvimrc

I often open a file in vim, make some changes and when it's time to save the file is read-only.. (owned by another user). I'm looking for tips on how I could re-open the file
as root and keep my changes without first saving it to a temporary file for copy or re-edit
as root.

Best Answer

From this stackoverflow answer, by skinp

:w !sudo tee %

I often forget to sudo before editing a file I don't have write permissions on. When I come to save that file and get a permission error, I just issue that vim command in order to save the file without the need to save it to a temp file and then copy it back again.

Related Topic