Saving locked files in VIM

Saturday, February 14, 2009

Saving locked files in VIM

I have opened a file in VIM and have done lot of editing, bugfixes, new code, beautification, whatever... there is lot of effort gone into it...

I give :w. Oops, it says "E45: 'readonly' option is set (add ! to override)", VIM definitely did warn me when I first started editing the file, I may have overlooked it.

Ok, I can live with it. It give :w!, hoping Santa will come. Oooops, "Permission denied!". What just happened? Now I remember, I copied that file as root. So, the ownership is with root now. (I should have given cp -p which preserves the ownership.)

What now? I need to change the permission and save again. If I don't have root permission, then all my work is gone and nothing can be done. Else... there are different ways...

1. Open a new shell from another window and change permission using chown or chmod.
2. More easily, directly open a shell from inside VIM using :shell. Then change permission.
3. I found the easiest way yesterday. Just give
sudo chown user:user %
(user is me and if I have sudo permission). The catch here is I don't even have to know the filename, % does exactly that.

Now I will give :w!, the ! is still needed as the file was opened readonly.

Was this info useful? Share your precious comments...

 See also... » Effective Use of VIM - Part 5

» Command Search in Linux

ATOzTOA : Latest Headlines


Tobin said...

You could also just :w /var/tmp/foo so you don't lose your work.

Post a Comment