Linux Tips| E212 Can't Open File for Writing in Vim (3 Ways)

Updated by Brithny on Nov 15, 2022
  | Computer Instruction

"I am trying to edit sources.list using vi editor but getting the following error while saving the file:

/etc/apt/sources.list" E212: Can't open file for writing" - from Stack Overflow

can't open file for writing in Vim

If you're a Linux user using Vim, you may have encountered the following error while trying to write to a file: E212 can't open file for writing. This article will show you three ways to fix this error. All of these methods work in Ubuntu and other Debian-based distributions. Let's get started!

Workable Solutions Step-by-step Troubleshooting
1. Check File Permissions Edit the "sources.list" file using a non-root-user with the following command...Full steps
2. Check the File Lock Find the file with this command: lsattr /home/admin/$filename...Full steps
3. Fix Read-only File System Check the disk space with this command. $ df -h. Then, make space by deleting...Full steps

Reasons - E212 Can't Open File for Writing

This problem can be caused by many things, from a permissions issue to low disk space. In this part, we'll analyze the reasons. Let's get started!

When using the vi command to modify the content of a Linux file, it is found that it cannot be saved. Every time you finish writing, you can use the ":q!" command to exit normally. However, when you use the ":wq!" command to save the file and exit, these error messages prompt:

  • Vim Can't Save File (E212)
  • Vim can't open file for writing
  • Root user can't open file for writing
  • /etc/apt/sources.list" E212: Can't open file for writing
  • E212: Can't open file for writing. Press ENTER or type command to continue

You receive this error for two main reasons:

  • The current user has insufficient permissions.
  • This file may be in use by other programs or users.

Solutions - 3 Ways to Fix Can't Open File for Writing in Vim

E212: can't open file for writing is an error that can occur when trying to save a file in Vim. There are a few different ways to fix this error, depending on the cause. 

Method 1. Validate User and Check File Permissions

The general cause of the error is the permission issue. The solution is to add sudo in front of the file to temporarily provide administrator privileges when using the vi command to open the file. For example, use the command "sudo vi hosts" to open and edit the file.

Step 1. Edit the "sources.list" file using a non-root-user with the following command.

$ vim /etc/apt/sources.list

Step 2. Get "/etc/apt/sources.list" E212: Can't open file for writing" error.

Step 3. Run sudo permissions to fix the error.

$ sudo vim /etc/apt/sources.list

Validate user and check file permissions to fix can't open file for writing in Vim

Method 2. Check If the File Is Locked to Fix Can't Open File for Writing

If you are a root or sudo user, but you still can't open the file for writing. Then, you can try to exit the file and check if the "I" command locks it.

check the lock to fix can't open file for writing in Vim

Step 1. Find the file with this command:

lsattr /home/admin/$filename

Step 2. Check the I lock.

If there is an "I" displayed, the file is locked. You can execute this command to unlock your files.

chattr -i $filename

After this, execute q! to exit the file. When viewing the file again, it is found that the content has been changed and saved.

Method 3. Check for Read-only File System or Capacity Issue

You still face this error if you have permission and use the sudo command. Then, you need to check for a read-only file system or capacity issue.

Step 1. Check the disk space with this command.

$ df -h

check read-only file system to fix can't open file for writing in Vim

Step 2. Then, make space by deleting or removing unnecessary files using the rm command.

Step 3. Run mount command to check for your device. It must be mounted as read-write (rw) not read-only (ro).

$ mount

Step 4. Fix the file system errors and re-mount it to a read-write file system.

$ sudo mount -o remount,rw /partition/identifier /mount/point

or

$ sudo mount -o remount,rw /

Then, you have resolved the "E212: Vim can't open file for writing" error. You can watch this video to get help if you still have problems.

  • 0:17 fix can't open file for writing while using Vim
  • 0:50 make sure the path
  • 2:57 check the file permission

How to Recover Lost Linux Files with Software

What if you lost data after fixing the "E212: Vim can't open file for writing" error? Don't worry. EaseUS Data Recovery Wizard is a helpful tool for recovering lost data on Linux systems. It has a simple interface that makes it easy to use, even for those who need to become more familiar with data recovery programs.

  • EaseUS Data Recovery Wizard can recover deleted files from any storage device, including hard drives, USB flash drives, and memory cards.
  • It also supports various file types, such as photos, videos, documents, and emails.
  • Best of all, EaseUS Data Recovery Wizard is very easy to use. This makes it an excellent choice for anyone who needs to recover lost data on a Linux system.

Download this software and find files in Linux.

 Download for Win Recovery Rate 99.7%
 Download for Mac Trustpilot Rating 4.6

Step 1. Install and Launch EaseUS Data Recovery Wizard on your Windows PC. Choose "Linux Recovery" on the left panel. 

Step 2. This software will detect your IP and Port automatically. You need to input the user name and password. If the IP address or Port is not right, you can correct it manually. Then, click "Connect Now". 

choose linux recovery

Step 3. After successfully connecting to your Linux device, EaseUS recovery software will start scanning the disk to find all lost and deleted files. When the scan process completes, click "Filter" > "Type" to specify lost file types.

choose the recovered data type

Step 4. You can preview the scanned files first. Then, select the target data and click the "Recover" button to restore lost files in Linux. 

recover windows 10 lost data

Conclusion 

Follow the solutions above to fix the "can't open file for writing" error in Vim without losing data. If you have any questions or need help, feel free to reach out and contact us. We're here to help!

 Download for Win Recovery Rate 99.7%
 Download for Mac Trustpilot Rating 4.6

Can't Open File for Writing FAQs

Read the questions and answers below to get additional help.

1. What to do if you are faced with the action can't be completed error?

To fix the action can't be completed because the file is open:

  • Run File Locksmith
  • Stop Processes with Task Manager
  • Empty the Recycle Bin and Restart
  • Restart Windows Explorer
  • Delete Temp Files

2. How do you fix cannot open file for writing in Vim?

You can fix the can't open file for writing in three ways:

3. How to exit Vim in the terminal without saving?

To exit Vim without saving changes:

  • Switch to command mode by pressing the Esc key.
  • Press : (colon) to open the prompt bar in the bottom left corner of the window.
  • Type q! after the colon and hit Enter to exit without saving the changes.

4. What is Vim used for?

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems.

Was This Page Helpful?

 
 
 

NEW Released

EaseUS
DriverHandy

Get It

EaseUS Data Recovery Services

Request a free evaluation >>

EaseUS data recovery experts have uneaqualed expertise to repair disks/systems and salvage data from all devices like RAID, HDD, SSD, USB, etc.