vim unable to write to files even though sudo is used

Solution 1:

This is the intended behavior of vim (and vi).

When you edit a readonly file, attempting to write the file the usual way (with :w) fails. This is to prevent you from accidentally changing a readonly file you might not wish to change.

If you really want to override the readonly permissions on a file that you own, and write your changes to the file, you must use the :w! command so vim knows this is what you want.

  • This is similar to how :q will not quit if there are unsaved changes, but :q! will.

If you want to write any changes and quit in one command, you can use :wq! or :x!.