Fix E212 Can't Open File for Writing in Vim with 3 Proven Ways - Qiling  

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


"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:

You receive this error for two main reasons:

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.

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. Deep Data Recovery 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.

Download this software and find files in Linux.

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

Select file types that you want to recover

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, Qiling 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.

select files and click recover

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.

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!

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:

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:

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.

Related Articles


Is this information helpful?     

What can we do to improve this information? (Optional)
Refresh Please enter the verification code!