# Fix Vim E37 Cannot Write Readonly File Permission Error

You make changes to a file in Vim, try to save with :w, and get:

bash
E37: No write since last change (add ! to override)
E212: Can't open file for writing

Or:

bash
"myfile.txt" E45: 'readonly' option is set (add ! to override)
E212: Can't open file for writing

Vim is refusing to write the file because the file is marked as read-only, or you lack filesystem permission to modify it.

Diagnosing the Problem

Check the file's permissions:

bash
ls -la myfile.txt

If the file shows:

bash
-r--r--r-- 1 root root 1234 Apr  8 10:00 myfile.txt

You cannot write to it unless you are root or change the permissions.

Check if the file is immutable (even root cannot modify):

bash
lsattr myfile.txt
# If you see: ----i-------- myfile.txt
# The file has the immutable attribute set

Solution 1: Force Write with Sudo

If you have sudo access, the easiest approach is to write through sudo from within Vim:

vim
:w !sudo tee %

The % expands to the current filename. tee writes stdin to the file, and sudo provides the necessary permissions. Vim will ask:

bash
[O]K, (L)oad File:

Press L to load the file from disk (since tee may have changed it).

To make this permanent, add a command to your .vimrc:

vim
command! W w !sudo tee % > /dev/null

Then use :W instead of :w when you need elevated permissions.

Solution 2: Fix File Permissions

If you own the file or have the appropriate permissions:

bash
chmod u+w myfile.txt

Or if the file is owned by another user:

bash
sudo chown $USER:$USER myfile.txt

Solution 3: Remove the Immutable Flag

If lsattr shows the immutable flag:

bash
sudo chattr -i myfile.txt

Then save normally from Vim with :w. Reapply the immutable flag if needed:

bash
sudo chattr +i myfile.txt

Solution 4: Write to a Different Location

If you cannot modify the file in place, write to a temporary location and move it:

vim
:w /tmp/myfile.txt.new

Then from the shell:

bash
sudo mv /tmp/myfile.txt.new myfile.txt

Understanding the readonly Option

Vim's readonly option is different from filesystem permissions. Vim may set readonly automatically:

  • When opening a file with view instead of vim
  • When opening a file owned by another user
  • When the file lacks write permission

Check the current state:

vim
:set readonly?

If it shows readonly, toggle it off:

vim
:set noreadwrite

Or:

vim
:set readonly!

This only affects Vim's behavior -- it does not change filesystem permissions. Even with readonly off, you still need write permission on the file.

Editing Root-Owned System Files

The safest approach for editing system configuration files:

bash
sudoedit /etc/nginx/nginx.conf

sudoedit copies the file to a temporary location, opens it in your default editor, and copies it back with the correct ownership and permissions. This is safer than sudo vim because:

  1. 1.Vim does not run as root (reducing the risk of accidental system damage)
  2. 2.The original file is only replaced if the edit completes successfully
  3. 3.A backup of the original is automatically created

Configure your preferred editor:

bash
export EDITOR=vim
sudoedit /etc/nginx/nginx.conf

The Nuclear Option: Force Write

If you are root and still get E212, the filesystem may be mounted read-only:

bash
mount | grep "on / "
# If you see: /dev/sda1 on / type ext4 (ro,relatime)

Remount as read-write:

bash
sudo mount -o remount,rw /

Then save from Vim normally.