Sudo Save a file as Regular User
The Problem
Know that feeling when you incorrectly open a file as a regular user but you just can’t damn save the file because it’s owned by root?
The Simple Fix
Too late in my life I realised there is a very simple fix, it’s simply to sudo dd of the output file like so:
:w !sudo dd of=%
% is simply the vim register that expands to the currently opened file.
vi.
The other alternative is to use tee as:
:w !sudo tee %
But that has the disadvantage of all printing the contents of the file to STDOUT.
Comments