Picked this little tidbit up today, don’t know why it took me so long to figure it out….
If you are editing a file without
sudo, and you need
sudo in order to save and you don’t want to reopen the file because you’ve already made the edits, simply use this vim command:
:w !sudo tee %
It’s so simple too, uses that awesome little utility of tee to just pipe it back into the file and then vim recognizes that the file has changed on disk and prompts you to reopen it.