There are three ways to fix the error: E212: can't open file for writing. Check the file permission, save files with sudo vim, and check for a read-only file system. If you lose Linux data during this process, run EaseUS Linux data recovery software to restore lost Linux data.
"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
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 |
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:
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.
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
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.
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.
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
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.
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.
Download this software and find files in Linux.
Step 1. Select a drive and start scanning
Launch EaseUS Data Recovery Wizard and hover over the partition where you lost data. This could be an internal hard disk, external disk, USB, or SD card. Then, click "Search for Lost Data".
Step 2. Check and preview scanned files
Use the file format filter on the left or upper right corner to narrow the scan results until you find the files you need. Then, you can click the "Preview" button or double-click a file to preview its content, if you'd like.
Step 3. Recover lost data to a secure location
Click the checkbox next to the file and click "Recover" to restore the lost data to your local storage or Cloud drive. We recommend that you do not store the recovered data on the disk where you lost it earlier.
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!
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
How to Upload Files to Google Drive Automatically
Solved: CMD.exe/CMD Keeps Popping up in Windows 10
8 Tips | How to Fix Windows 7 Stuck on Welcome Screen
OneDrive Files Corrupted | How to Restore Them in 2024