Query about opening the file

I have a query regard the opening of the file. Suppose I have opened the file
Let's say view filename
And then I found that I have to do some changes in that, but I do not want to close the file
Then how I can change the permission of the file from read mode to write mode

Regards
Adi

In view you can "insist" that changes be written with the :w! command.

If the file needs its permissions on disk changed, try chmod +r $filename

That means, you are reading a file and you want to do some editing in that file? If so, press 'i' and start typing. press 'Esc' to go back.

I assume you mean +w instead of +r.

And, if you're editing the file in vi (or view, if it lets you modify text without switching to vi mode manually), you can change the mode of the file you're editing with the command:

:!chmod +w %

Don Cragun,
I just tried your solution on one of my files, unfortunately it didn't work.
I opened the read-only file using vi . Then I entered :!chmod +w % and then hit enter. It then asked me to [Hit return to continue] . When I hit enter, and tried to save the file :wq , it says File is read only . Am I missing something here?

Yes, since the file was not writeable at the time vi opened the file, it had to open it read-only. To write the modified buffer back to the file (that the chmod later made writeable), you still have to use the :w! as suggested by Skrynesaver (rather than just :w ) to get vi to reopen the file read/write instead of read-only so it can write the modified buffer back to the file.

In a case like this, vi should have displayed an indication that it opened the file read-only when you started the editing session. When using vim on OS X, the status on the last line of the display when opening an empty file that does not grant write permission is:

"filename" [readonly] 0L, 0C

Depending on what privileges your user account has it might be that you can't change the file to "writable", because you have not the necessary rights to do so. If this is the case, you can't neither do it inside or outside "vi" or "view".

What Don Cragun showed you was based on the assumption that you do indeed have the permissions necessary to change the file to "writable". (My apologies if i have pointed out the obvious, but long years spent with users made me expect always the most inspired but least logical, so to say.)

I hope this helps.

1 Like