Damian's notes – The sudoedit trick

Damian Kula

The sudoedit trick

Posted on 2026.09.17

I always forget what is the command to write to files I don't have permissions for. I don't edit root-permitted files often enough to have a muscle memory for that so I always do this awkward dance of searching for the sudo tee trick on stack overflow.

Here it is:

:w !sudo tee %

However, I think this got borked as now the nvim doesn't really like the interactive input from the command line and sudo would like to get the password from stdin. I am not doing that, no.

So what other options do we have? sudo nvim filepath. Well, this changes the $HOME so your nvim experience changes to vanilla. Annoying and distracting from the flow perspective. Maybe symlink the nvim config to the root's $HOME? Or maybe run sudo -E nvim filepath to preserve the env vars? Are you nuts? Do you really want to give root permissions to all those auto-updating plugins you installed without even looking at their repos? You better not, but also why you didn't think about that while typing sudo nvim?

There is a better way!

sudoedit filepath

This opens up the file in $EDITOR, running as your user, against a temp copy of the file. Then, it copies the file back and replaces the original target.

I wish I knew about that earlier.