Normally, you can use :w to write changes in vim, or :wq to save the changes and exit. But occasionally, you’ll encounter the “Vim can’t open file for writing (E212)” error when trying to write the changes. According to Vim documentation, E212 occurs if the file you’re writing to cannot be created or overwritten. This could be because the file name is invalid or because you don’t have permission to write in the directory. Verify File Name and Path It’s possible that you’re using some invalid characters like * in the filename. You can try saving the file with a different name to test for this. w: newfilename In practice, the more common occurrence is that users try to save the file to a directory that doesn’t exist. While you can use editors like Vim to create files, the directory must exist beforehand. There are a few ways to deal with this situation. You can save the file to a different directory by specifying the path like so w: /home/anup/doc.txt You can prefix commands with ! to execute them directly from the editor. In this case, you can create the missing directory from inside Vim. !sudo mkdir /full/path/to/directory/ !sudo mkdir /home/anup/newfolder/ For instance, if we needed to save doc.txt in the /home/anup/newfolder directory, the above command would create this directory. Then, we’d be able to save the file (:w). Finally, you can also exit Vim (:q), use mkdir to create the missing directory, then open the file with Vim again and save it. This method is a bit roundabout though, so it’s usually better to create the directory from inside Vim as demonstrated above. Provide Root Permission If you run Vim as a non-root user and try to write to a root-owned file or directory, you’ll encounter this error. The fix in this case is very simple. Just launch Vim with root privileges. sudo vim /home/anup/doc.txt Mount File System as Read-Write Although uncommon, you can also encounter this error if the device containing the file is mounted as read-only. To check for this, you can first list your devices and note the partition identifier for the device like so sudo lsblk -e7 Now, use the mount command to list the mounted filesystems. mount If the device is indeed mounted as read-only (ro), remount it as read-write (rw) like so sudo mount -o remount,rw /partition/identifier /mount/point For instance, to mount /dev/sda2 as rw at /, you’d use sudo mount -o remount,rw /dev/sda2 /