Sudo-Saving a File in Vi as Root
How many times have you accidentally opened a protected file with vi, edited a whole bunch of stuff in it and only at the point you wanted to save it, have you remembered that you’d forgotten to “sudo” before editing the file? If you do a lot of Unix work, I bet this has happened to you more than once. It is anything but a pleasant experience.
However, you do not have to quit without saving (i.e. lose your changes) and re-edit the file as root, instead you can get quick sudo from within the vi itself by typing:
:w !sudo tee %
instead of the usual “:w”