Vim Can't Save File (E212)

MacosVim

Macos Problem Overview


Sometimes when I create a file using vim some/path/newfile, vim lets me edit it, only to complain when I attempt to save my changes.

E212 Can't open file for writing.

This appears to happen only when the new file is located in a system directory.

:w! does not override this error.

How can I write the current buffer, without having to save it to a temporary location, exit, then rename it using sudo?

Macos Solutions


Solution 1 - Macos

This will ask you for the root password, then save your changes as you requested:

:w !sudo tee %

Then type (L)oad at the prompt, to re-load the file after it is saved.

Solution 2 - Macos

Add this line to your .vimrc:

cmap w!! %!sudo tee > /dev/null

and then you can do

:w!!

when you get into this position, and it will write the file using sudo. Very handy.

Solution 3 - Macos

You can mkdir first, then save it.

Solution 4 - Macos

You can avoid this problem by using "sudo" to start the editing session.

sudo vi name-of-file

Solution 5 - Macos

If you want a robust, easy-to-remember solution and don't mind installing a plugin, try SudoEdit.vim - Edit Files using sudo or su or any other tool.

Solution 6 - Macos

If this is the case in Windows 7 or later editions, run the VI editor as Administrator. Right Click of the application and select "Run as Administrator". This issue will be resolved. Moreover, the error is due to Administrative Privileges.

Solution 7 - Macos

vim some/path/newfile

you can try to do it in two steps,first create the folder 'some' and 'path' by use mkdir ~ ;second you go into the 'path' folder,use the command:sudo vim newfile.then save it

Solution 8 - Macos

Make sure the directory where you are trying to save the file exists and that you have permission to edit the file.

You can type :help message in Vim to get more information on this and other error messages. Try searching by typing /E212 once the docs come up. You can also view the documentation online at http://vimdoc.sourceforge.net/htmldoc/message.html and CTRL-F to find it.

Solution 9 - Macos

For what it's worth, you may also want to ensure you have sufficient storage in the partition where you're attempting to save the file. I had to free up some space in my /home drive and that resolved the issue.

Solution 10 - Macos

I know this is an old question, but this is what fixed it for me. Your file might be set to immutable meaning that you can't edit it.

You can check this with lsattr /path/to/file.txt

If it is use chattr -i /etc/resolv.conf to make it no longer immutable.

Solution 11 - Macos

Just had this issue outside of system directory. When I tried to open a file vim src/help/tips.c. Turns out help directory did not exist, the directory was named differently and it was one of those very rare occasions that I did not auto-complete with Tab.

So, in conclusion, if this happens for a file that is not at a place where you may have permission issues, look if the path leading up to the file is a valid one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStuWeldonView Question on Stackoverflow
Solution 1 - MacosBrent FaustView Answer on Stackoverflow
Solution 2 - MacosPeterView Answer on Stackoverflow
Solution 3 - MacosGankView Answer on Stackoverflow
Solution 4 - MacosphantastesView Answer on Stackoverflow
Solution 5 - MacosIngo KarkatView Answer on Stackoverflow
Solution 6 - MacosRajiniView Answer on Stackoverflow
Solution 7 - MacosCodeMingView Answer on Stackoverflow
Solution 8 - MacoswhalesoftheskyView Answer on Stackoverflow
Solution 9 - MacosDivDiffView Answer on Stackoverflow
Solution 10 - Macosmy code made me suicidalView Answer on Stackoverflow
Solution 11 - MacosunixiaView Answer on Stackoverflow