Fix “E212 Can’t open file for writing” in Vim

Vim is a popular text editor that has been around for a few decades. It’s commonly included as a standard option to edit text files with most Linux distributions and Mac OS X. Vim is incredibly powerful and flexible – for example, it can work with several files at once through tabs, or easily run automated tasks from inside itself. A huge range of other text editors are introduced from time to time, but Vim has remained a popular option for those wanting more customization and control over their workflow.

One of the most popular error messages that confuses Linux beginners is the “E212: Can’t open file for writing” in vim. The full error log can look like below :

"path/to/example.txt" E212: Can't open file for writing Press ENTER or type command to continue
Code language: PHP (php)

The cause of this error, according to Vim manual, is :

For some reason the file you are writing to cannot be created or overwritten.
The reason could be that you do not have permission to write in the directory
or the file name is not valid.

In this article, we will show you a few ways to fix the “E212: Can’t open file for writing” while working with Vim.

Install vim in Ubuntu

If you see the error message “Command ‘vim’ not found”, it means you don’t have the vim editor installed.

Install vim in Ubuntu

You can install it by running the following command in Ubuntu:

sudo apt-get install vim
Code language: JavaScript (javascript)

Check the file path for its existence

The first thing you need to do is checking the directory or the file path in order to be sure that it does exist. If it doesn’t or one of its parent directory doesn’t, trying to open it with Vim will definitely raise “E212: Can’t open file for writing” error.

In the example below, I’m trying to edit a file in /home/tmp/example.txt but getting “E212: can’t open file for writing” every time I try to save it, as shown in the image below.

In this scenario, you will not be able to create or edit the “example.txt” file inside the /home/tmp directory, as it does not exist.

Now you have two options. You can exit vim , run mkdir tmp to manually create the directory and then start creating the file all over again. Or you can direcly run a command without leaving vim by following the instructions below :

  • Press ESC key to get out of “Please ENTER or type command to continue” prompt.
  • Type !sudo mkdir /home/tmp” followed by Enter key in order to make a new tmp directory inside home. The exclamation mark prefix tells vim to send this command to the system instead of recognizing it as an internal one. Remember to replace /home/tmp/ with your path.
  • Vim now ask for your root password, input it like you usually do when running sudo.

Make sure you have write permission

We’ve seen many users “E212: Can’t open file for writing” error when trying to edit sources.list file. This is because the file you’re trying to edit is placed in a directory where you don’t have the write permission, or have no permission at all. Run the following command to be really sure that this is the case and the destination file is owned by another user :

ls /etc/apt/
Make sure you have write permission

If you’re logged in as a non root account, prefix the command with sudo usually get rid of the problem.

sudo vim /etc/apt/sources.list
Code language: Access log (accesslog)

Check for Read-only filesystem or full disk space

If none of the above solution worked for you, the cause might has something to do with the filesystem or storage capacity.

While being a possible reason, it’s unlikely the storage capacity is the real reason. If it does, you may have seen countless system notifications before you can actually editing anything. But in order to be really sure, run df -h in a terminal and look in the output for 100% use on any mounted path.

The last thing that causes E212 error in Vim is a read-only filesystem. In order to verify if this is the case, run mount command in a terminal. You should find that your device is mounted with rw permission, and not read-only (ro).

If your device is mounted as read-only, run the following command to re-mount it with the correct permission. Remember to replace /mount/point and /partition/identifier with the proper value.

sudo mount -o remount,rw /partition/identifier /mount/point # or sudo mount -o remount,rw /
Code language: Shell Session (shell)

Once the command completes its job, you should be able to edit your files again without encountering “E212: Can’t open file for writing”.

We hope that the information above is useful to you. If you’re interested in a modern source code editor, check out VSCode as well as our guides on how to enable/disable word wrap in VSCode, How to use LaTeX in VSCode or how to automatically indent your code in Visual Studio Code.

Click to rate this post!
[Total: 13 Average: 4.8]

Leave a Comment