I sometimes open a read-only file in vi, forgetting to do chmod +w before opening it. Is there way to change the file from within vi?
chmod +w
Something like !
!
If you have the rights to write to the file, then you can just use exclamation mark to force it:
:w!
If you don't have the rights and need to change user, but still want to write to the file, sometimes you may go for something like
:w !sudo tee %