Sudo Save a file as Regular User


Table of contents


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?

Saving file with vim fails as regular user

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.

Saving file with vim suceeds
This pretty much works everywhere, even with regular 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