Saving Read-only Files in Vim – The sudo tee trick

How many times you had the following situation: You open a file with Vim and make some changes. When you try to save the file, you realize that you didn’t run Vim with sudo?!

There is a solution for this. The next time when you get into this situation, you can use the following command in order to save the changes you made:

:w !sudo tee % >/dev/null

There’s just one problem – it’s a bit hard to remember it. That’s why, if you’re going to need it often, add the following line to your .vimrc:

cmap w!! w !sudo tee % >/dev/null

This way, when you get to the same situation again, you can just type :w!! to save the file, even if you did not run Vim with sudo.


Over the years, Vim got a reputation that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I’ve started to write a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

If you want to get updates and sample content from the book, leave me your email below, and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.


If you liked the post, we should get connected - follow me on Twitter