Forgot to edit a file as sudo user?
Want to overwrite a file without quitting.
:w !sudo tee %
tee command is run as root and it takes standard input (or the buffer) and writes it to a file represented by %.
This will prompt to reload the file again - hit L to load changes within vi itself.
Append to ~./.vimrc file:
command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
Save and close the file.
Test, by opening a priviledged file without using sudo. Example:
vi /etc/hosts
Edit the file, and write as a privileged file with custom command, just type:
w:
sudo will require that you authenticate yourself, so enter your password when prompted.