Use this command inside of
vim to write to the file with
:w !sudo tee %
% are 2 special
! pipes the contents of the current buffer to another command.
% means “the current file”
sudo tee FILE is a common trick to write to a file as root by
sudo. Note that in a wrong command
sudo cat >FILE for such purpose, the user that tries to write to the FILE is the current user instead of the
Some other common tricks for
sudo also work here, such as
:w !sudo sh -c "cat > %"