66

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?

StuWeldon
  • 677
  • 1
  • 7
  • 9
  • 1
    Also, sometimes if you put a space between `w` and `!`, it works. (At least this works for me in linux using heavier vim `vim-gtk` not just lightweight `vim` although I'm sure it would work just the same in regular ole' vim.) I didn't know about `Peter`'s answer, though. `w!!` works for me, too. – dylnmc Oct 07 '14 at 13:38

12 Answers12

105

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.

Brent Faust
  • 9,103
  • 6
  • 53
  • 57
19

You can mkdir first, then save it.

Gank
  • 4,507
  • 4
  • 49
  • 45
  • 8
    This does not help. The question is not about directories that don't exist. It's about editing existing files as (for example) a normal user, when the file can be written by root only. In that case, there is no missing directory to create. – nobody Jul 14 '14 at 18:55
18

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.

Peter
  • 127,331
  • 53
  • 180
  • 211
  • I like this idea. Typing :w!! replaces the command with `!sudo tee > /dev/null`, but it just says n "lines filtered" and the file is not actually created. It appears that the /dev/null is causing that? – StuWeldon Feb 19 '13 at 01:59
  • 4
    Great idea -- `w!!` is much easier to remember than my solution. What works for me on OS X is `cmap w!! %!sudo tee %` – Brent Faust Feb 19 '13 at 02:19
9

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

sudo vi name-of-file
phantastes
  • 91
  • 1
  • 1
3

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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
3

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.

Rajini
  • 41
  • 1
3

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

CodeMing
  • 31
  • 3
2

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.

  • 1
    In particular, sometimes vim changes the current working directory of your buffer. In that case, an intermediate directory might not exist and that's what causes E212. You need to change the current directory back using `:cd ORIGINAL_FOLDER_PATH` to resolve this and be able to save again. – Zenul_Abidin Apr 12 '21 at 09:14
1

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.

DivDiff
  • 963
  • 2
  • 10
  • 22
1

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.

0

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.

unixia
  • 4,102
  • 1
  • 19
  • 23
-2

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.