Article

Save a file as root in vim

I often edit files without having the right permissions in VIM.

A terminal window showing a Vim text editor with an error message: E212: Can't open file for writing.

I changed the file and when VIM told me that I did not have the proper permissions to save it, I quit VIM and entered sudo !! to try it again.

This was of course stupid of me. Currently I try to be more mindful about these kinds of problems and to fix them.

Here are the lines that I just added to my .vimrc:

" Use :w!! to save as root!
cmap w!! w !sudo tee % >/dev/null

When I enter :w!! now, VIM saves the file as root.

I hope I will not encounter this problem again.

  • vim

Replies

To reply from the fediverse, follow @robert@rocu.de or look up the address of this post from your account.