# Fix Vim E37 Cannot Write Readonly File Permission Error
You make changes to a file in Vim, try to save with :w, and get:
E37: No write since last change (add ! to override)
E212: Can't open file for writingOr:
"myfile.txt" E45: 'readonly' option is set (add ! to override)
E212: Can't open file for writingVim 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:
ls -la myfile.txtIf the file shows:
-r--r--r-- 1 root root 1234 Apr 8 10:00 myfile.txtYou cannot write to it unless you are root or change the permissions.
Check if the file is immutable (even root cannot modify):
lsattr myfile.txt
# If you see: ----i-------- myfile.txt
# The file has the immutable attribute setSolution 1: Force Write with Sudo
If you have sudo access, the easiest approach is to write through sudo from within 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:
[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:
command! W w !sudo tee % > /dev/nullThen 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:
chmod u+w myfile.txtOr if the file is owned by another user:
sudo chown $USER:$USER myfile.txtSolution 3: Remove the Immutable Flag
If lsattr shows the immutable flag:
sudo chattr -i myfile.txtThen save normally from Vim with :w. Reapply the immutable flag if needed:
sudo chattr +i myfile.txtSolution 4: Write to a Different Location
If you cannot modify the file in place, write to a temporary location and move it:
:w /tmp/myfile.txt.newThen from the shell:
sudo mv /tmp/myfile.txt.new myfile.txtUnderstanding the readonly Option
Vim's readonly option is different from filesystem permissions. Vim may set readonly automatically:
- When opening a file with
viewinstead ofvim - When opening a file owned by another user
- When the file lacks write permission
Check the current state:
:set readonly?If it shows readonly, toggle it off:
:set noreadwriteOr:
: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:
sudoedit /etc/nginx/nginx.confsudoedit 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.Vim does not run as root (reducing the risk of accidental system damage)
- 2.The original file is only replaced if the edit completes successfully
- 3.A backup of the original is automatically created
Configure your preferred editor:
export EDITOR=vim
sudoedit /etc/nginx/nginx.confThe Nuclear Option: Force Write
If you are root and still get E212, the filesystem may be mounted read-only:
mount | grep "on / "
# If you see: /dev/sda1 on / type ext4 (ro,relatime)Remount as read-write:
sudo mount -o remount,rw /Then save from Vim normally.