I'm using vim for scripting and configs and noticed that I sometimes forget to open file with sudo and therefore when I made some editings I got the error:

E45: 'readonly' option is set (add ! to override)

So I have to exit and make all the edits I just made for scratch. This is very annoying. Is there a way to approach it? Maybe I can switch from readonly mode without exiting vim?


2 Answers 2


You need the write with sudo trick:

:w !sudo tee %

See also @ChristianBrabandt's SudoEdit plugin which also works on Windows and supports undofiles.


you could reset the readonly option with :set noro

But the underlying problem in your case is access-right. As a normal user you (and vim) cannot write a root-file.

To save your changes with root-permission you can try :

:w !sudo tee % > /dev/null

Not the answer you're looking for? Browse other questions tagged or ask your own question.