Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Can't Save File (E212)

Tags:

vim

macos

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?

like image 322
StuWeldon Avatar asked Feb 19 '13 01:02

StuWeldon


People also ask

What is E212 error?

Running :h E212 inside Vim prints the following: 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.

How do I save a file in Vim?

The command to save a file in Vim is :w . To save the file without exiting the editor, switch back to normal mode by pressing Esc , type :w and hit Enter . There is also an update command :up , which writes the buffer to the file only if there are unsaved changes.

How Force Quit Vim?

In Normal Mode , you can also press ZZ or ZQ to quit Vim. Where ZZ is the same as :x and ZQ is the same as :q! .

How do I save and quit Vim?

The simplest way to both save and quit out of VI or VIM is with a keyboard shortcut ZZ. Note the capitalization, which means the save and quit command is executed by pressing Escape, then holding the Shift key then pressing Z twice, thus: Press the ESC key, then hold the Shift key then press Z twice.


1 Answers

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.

like image 81
Brent Faust Avatar answered Sep 21 '22 12:09

Brent Faust