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
Comments
A Better Way
:w !sudo tee %
http://www.cyberciti.biz/faq/vim-vi-text-editor-save-file-without-root-permission/
:w !sudo tee % It works
:w !sudo tee %
It works
Post new comment