Fix vi/vim error: E45: 'readonly' option is set (add ! to override)

Issue:

Ever started editing a document only to find out that when trying to save it :w or :wq, you forgot to open it with sudo? Vim knows what to do and gives you a hint saying "add ! to override".

Fix:

Instead of having to save it to a temp directory :w ~/temp then exiting and having to move it to the real location. Use the following the next time:

:w ! sudo tee %

This invokes the write function :w and opens a new shell ! and pipes your files contents via standard out into the program tee with sudo privileges. tee is an amazing program which outputs the contents of stdin to stdout and to a file. In this case the filename is the vim shortcode %.

Enjoy a new way to save your vim files.

Tim Coombs

Administrator of Slowb.ro and world leader of my own mind, the only place our ideas and thoughts are our own in a world gone mad

In a terminal https://slowb.ro

Subscribe to Slowb.ro's Blog

Get the latest posts delivered right to your inbox.

or subscribe via RSS