User login

Save a file in Vim after editing and only realizing it's read only when saving

Superseded by a better way to force saving from in vi when read-only, noted in the comments and in Agaric's own post there as :w !sudo tee %

If you've used Debian or any other type of Linux or Unix-like for editing files on your server, at some point you will have done a whole bunch of editing without seeing the "READ ONLY" warning, and then... you try to save.

Sometimes, throwing an exclamation point on your w is enough:

:w!

http://uw714doc.sco.com/en/FD_create/Stopping_vi.html

having to search for something as simple as this is what I get for using the ZZ shortcut all the time.

Does not always work.

That is, you should sudo vi document.txt up front when needed.

But if you've done all this editing you don't want to lose, all is not lost! Save it to your user directory:

:w! ~/tempfile.txt

And then just copy over that mean readonly file, using sudo this time:

sudo mv ~/tempfile.txt meanreadonly.txt

Resolution

Searched words: 
vi how to save read only file vim vi saving a read-only file

Comments

A Better Way

:w !sudo tee % It works

:w !sudo tee %

It works

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.