What the code Error Means
Error E212 in Vim means Can't open file for writing. It occurs when you try to save changes to a file (:w or :wq), but the Linux operating system denies write access.
Typical scenario: You are editing a system configuration file (e.g., /etc/hosts, /etc/nginx/nginx.conf) or a file created by another user/process. The message at the bottom of Vim will look like this:
E212: Can't open file for writing
Or, if you are trying to save under a different name in a protected directory:
"/path/to/file" E212: Can't open file for writing
Causes
The error is not related to Vim itself—it is standard Linux system behavior ensuring security. Specific causes:
- The file is owned by another user. You are editing a file you do not own (e.g.,
root). Check with:ls -l /etc/hosts(owner isroot). - You lack write permission (w) for the file. Even if you are the owner, the write bit may be removed. Check: in the
ls -loutput, the owner's permission field lacks aw(e.g.,-r--r--r--). - You are trying to save the file in a directory where you lack write permission. For example, you are editing a new file
/root/newfile.conf. The problem may be the parent folder, not the file. - The file system is mounted in "read-only" mode. This happens for protected partitions or during disk errors. Check with:
mount | grep " / ". - File attributes (
chattr) are set as "immutable" (i) or "append-only" (a). Check with:lsattr /etc/hosts(if there is aniorain the first column).
Solutions
Solution 1: Use sudo to launch Vim (simple, but requires caution)
This is the fastest method if you need to edit a system file just this once.
- Exit your current Vim session without saving changes: press
Esc, then:q!andEnter. - Launch Vim as the superuser:
For example:sudo vim /path/to/filesudo vim /etc/fstab. - Make the necessary changes and save the file normally:
Esc,:wq,Enter.
⚠️ Important: Do not leave a terminal with
sudo vimrunning unattended. Avoid usingsudoto edit files in your home directories (/home/user/) unless absolutely necessary—it can disrupt ownership permissions.
Solution 2: Change file permissions (chmod)
If you own the file but write permission is removed, restore it.
- Exit Vim (
:q!). - Grant yourself write permission. The safest option is to add write for the owner (you) only:
For example:chmod u+w /path/to/filechmod u+w ~/.config/app/config.ini. - Alternatively, you can set standard permissions
644(owner: rw-, group: r--, others: r--):chmod 644 /path/to/file - Reopen the file in Vim (without
sudo) and save your changes.
Solution 3: Change file ownership (chown)
If the file is owned by another user (e.g., the www-data web server user) and you need to edit it regularly, change the owner to yourself or your group.
- Exit Vim (
:q!). - Change the owner. To become the file's owner, run:
sudo chown $USER:$USER /path/to/file$USERis an environment variable containing your username. This makes you the owner and sets your default group. If you only need to change the group, leaving the owner:sudo chgrp <group_name> /path/to/file. - You can now edit and save the file in Vim without
sudo.
Solution 4: Use a different editor with built-in sudo (for convenience)
Some text editors (e.g., nano or micro) have a built-in ability to save via sudo when permissions are insufficient. However, Vim does not have this feature. If you want to stay in Vim, this solution does not apply. As an alternative:
- Use
sudoedit. This is a secure utility that creates a temporary copy of the file with root privileges, edits it with your default editor (can be configured to use Vim), and then saves it:
It is preferable tosudoedit /path/to/filesudo vimbecause it does not run the editor itself as root.
Prevention
To avoid E212 in the future:
- Determine who should own the file. System configs (
/etc/) are usually owned byroot. Files in/home/belong to your user. Web server files belong to the server user (www-data,nginx). Edit files knowing their intended ownership. - Apply default permissions. For files in your home directories, permissions are usually
644(umask 022). If you create a file that needs to be executable, usechmod +x. - Use
sudoeditinstead ofsudo vim. This is a safer method for editing others' files, minimizing risks. - Do not remove protection from system files unnecessarily. If you granted yourself write permission (
chmod u+w) on a system file, revert it after editing:chmod u-w /path/to/file. This prevents accidental modifications by other programs.
Frequently Asked Questions (FAQ)
Can I disable permission checks in Vim?
No. This is a fundamental security function of the Linux kernel, not a Vim setting. Any process, including an editor, must operate within the permissions of the user who launched it.
What if chmod or chown also return "Permission denied"?
This means you are neither the owner of the file nor in the sudoers group (you lack administrative privileges). You first need to obtain superuser privileges (log in as the root account or use sudo for the chmod/chown command itself):
sudo chmod 644 /path/to/file
Why did the file become owned by root after using sudo vim and saving?
Because you saved it as the root user. You will now be unable to edit that file without sudo. This is a common mistake. Solution: revert the owner to yourself: sudo chown $USER:$USER /path/to/file.
What if it's a directory, not a file, that is protected?
The error will be similar. You need write permission on the directory (x to access its contents and w to create/delete files). Fix permissions on the directory: sudo chmod u+w /path/to/directory or change its owner.