StuWeldon
StuWeldon

Reputation: 717

Vim Can't Save File (E212)

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?

Upvotes: 70

Views: 150829

Answers (12)

Nigel Williamson
Nigel Williamson

Reputation: 11

I have experienced this in Kali!! The default account requires escalation to root with "sudo" in order for the file to be editable. e.g: sudo vim / at this point all standard expectations appear to follow.

Upvotes: -1

unixia
unixia

Reputation: 4320

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.

Upvotes: 0

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.

Upvotes: 1

phantastes
phantastes

Reputation: 91

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

sudo vi name-of-file

Upvotes: 9

whalesofthesky
whalesofthesky

Reputation: 35

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.

Upvotes: 2

DivDiff
DivDiff

Reputation: 983

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.

Upvotes: 1

CodeMing
CodeMing

Reputation: 31

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

Upvotes: 3

Rajini
Rajini

Reputation: 41

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.

Upvotes: 3

Gank
Gank

Reputation: 4667

You can mkdir first, then save it.

Upvotes: 21

Ingo Karkat
Ingo Karkat

Reputation: 172598

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.

Upvotes: 3

Brent Faust
Brent Faust

Reputation: 9309

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.

Upvotes: 108

Peter
Peter

Reputation: 132257

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.

Upvotes: 19

Related Questions