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 !
!
After editing your file with vim, press "esc" and then ":". Then type the following command:
w !sudo tee %
Then press "Enter". Then type
:q!
to successfully exit from the editor.