banner



Vim Cannot Open File For Writing

Vim can't open file for writing error is one of the most common and frequent errors you will encounter, if you use Linux and vim editor. Whether you are a novice or Pro Linux user, "E212" is the error code, you don't like seeing. Most of the time, this error occurs due to insufficient rights or a directory path or file that doesn't exist, which you are trying to edit. In this post, I will let you know all the reasons and solutions to fix the "Vim can't open file for writing" error.

When you use Linux, you can get the below-mentioned error, while you try to edit a file -

  • /etc/apt/sources.list" E212: Can't open file for writing
  • Vim Can't Save File (E212)
  • Vim can't open file for writing
  • Root user can't open file for writing
  • Error in editing vimrc
  • E212: cant open file for writing ec2

Possible causes of the above-mentioned issues -

  • You don't have access or you are trying to edit a file, which is owned by root user
  • Path of the file, you are trying to edit doesn't exist (non existing file, non existing directory)
  • Trying to write a network drive and you don't have access or partial access
  • Trying to edit a read only file system
  • Not enough space exists in your current partition or drive.

Let's deep dive and fix errors and it's causes one by one.

Install vim editor in Ubuntu, if not there already

If you get the error: "Command 'vim' not found, but can be installed with:" it means you don't have vim (visual improved) editor installed. Run the following command to install the package in Ubuntu.

            $ sudo apt install vim          
Install vim in ubuntu using apt install vim command

How to Fix Vim can't open file for writing error

Solution-1 Check the directory or path of file, create it if it doesn't exist

First thing first, if you are getting vim can't open file for writing error, check the path and directory of the file. Let's understand it from an example.

I am trying to run a command "vim /home/tmp/sample.txt" and getting this error "/home/tmp/sample.txt" E212: can't open file for writing" while saving it as shown in the image.

vim can't open file for writing

In this scenario, the "tmp" directory inside /home doesn't exist. So if you will try to either create a new or edit the existing "sample.txt" file. This will not work and you will get errors.

In my case, I need to create the directory            tmp            inside the /home            first            to get rid of this "E21: can't open file for writing" error. This may happen in your case also. You may need to create the required directory or            maybe you just need to correct the required path            :-).

If you want to create a directory without closing the vim editor prompt because you have already edited the file and you don't want to lose changes. Then follow these steps-

  • Press "ESC" on your keyboard.
  • Type "!sudo mkdir "path of directory that need to be created". For example, in my case i need to create "tmp" directory inside /home.

E.g.

            :!sudo mkdir /home/tmp/          
create directory using vim editor

As soon as you will run this command and press enter, It may ask you to give your sudo credentials, and your required directory will get created as shown in the image.

validate directory after creating it from Vim editor

You can edit, save and exit your file now with the ":wq" command without any issue.

How to get the reason of error "E21: can't open file for writing" using vim help

To find out the reason for the error, follow these steps in the vim editor prompt, where you are getting an error.

  • Press "esc" key on your keyboard
  • Then type :h e212 as shown in image ":h -" represents help and e212 error. Same way you can check for any error, you encountered in vim editor
how to get error help e212 in vim
  • Press enter and you will get the error code and its reason. For example, in my case it is showing the follow -
            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.                      
/etc/apt/sources.list E212: Can't open file for writing

Press the "ESC" key and then type ":q!" to exit from help.

How to fix etc/apt/sources.list" E212: Can't open file for writing

If you use Linux, many times in your lifetime. You would have edited the sources.list file and I am sure, you would have encountered an E212 error. Follow solution-2 step by step to fix this issue.

Solution-2 Validate User or file permissions

If you are trying to edit a file and you have given the correct path but still you are getting "can't open file for writing error", then you are a victim of permission issues.

If you are logged in with a non-root user, make sure you start with the sudo command, if you are trying to edit a system file or file owned by root.

Let me show you an example, try editing /etc/apt/sources.list with a non-root user and you will understand, what I am trying to prove here.

If you will see the below image, the "sources.list" file is owned by root, so if you will try to edit it using a non-root-user with the following command

            $ vim /etc/apt/sources.list          
check ther permission of file first, if you are not able to edit the file in vim

You will get "/etc/apt/sources.list" E212: Can't open file for writing" error.

To fix it, you must edit this file using sudo permissions. run the following command to edit this file successfully.

            $ sudo vim /etc/apt/sources.list          

How to fix even if "Root user can't open file for writing"

If you are a root or sudo user, Also you have given the correct path to directory or file but still, you are not able to open the file for writing. Then you are trying to edit a read-only file system or your disk space is full, which doesn't allow editing and saving a file.

Solution3- Check for Read-only file system or Capacity issue

Use the "df" command to check the partition or disk space usage. If you see "disk" or "partition" available space is showing "0" bytes except for "/dev/loop".

Then make space by deleting or removing unnecessary files using the rm command. It will fix your "vim can't open file for writing" issue.

            $ df -h          
check for capacity issue

If you have enough space, then make sure your file system is not mounted as read-only. Run the "mount" command and check for your device, it must be mounted as read-write (rw) not read-only (ro).

For example, /dev/sda5, which is my root (/) partition, mounted as rw.

            $ mount          
check for file system is read only or read write in ubuntu

If it is showing mounted as read-only in your case, then remount it as rw. If it's happening due to file system errors, then 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 /          

You will be able to edit your file, once your file system will be rw and your "vim can't open file for writing" will vanish.

Video tutorial

Conclusion

I am hopeful, that one of the 3 solutions shared in this post will fix your "E212: Vim can't open file for writing" error. If you are still facing issues, You can let me know, via your comments. I will try to help you to the best of my knowledge.

Vim Cannot Open File For Writing

Source: https://cloudlinuxtech.com/fix-vim-cant-open-file-for-writing-e212/

Posted by: cornettinglacrievor.blogspot.com

0 Response to "Vim Cannot Open File For Writing"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel