0

This question already has an answer here:

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?

marked as duplicate by Rich, Herb Wolfe, statox Oct 31 '17 at 14:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

You need the write with sudo trick:

:w !sudo tee %

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

1

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.