How to save a current file opened with vim when ‘root’ access is required

Sometimes when editing a configuration you may find out that the file you opened was opened as ‘sudo’ hence you won’t be able to save it.

There is a solution to it so you don’t have to lose the configuration you have already done.

:w !sudo tee %

% means the current file

:w is saving the buffer not a current file, so if you type type in :w !sudo tee test2.txt ,this would save the buffer in the new test2.txt file instead of the current opened file.

Advertisements