E212: Can’t open file for writing
Press ENTER or type command to continue

If you use vim, you have probably come across this error before.
It normally happens when you don’t have permission to write to your chosen destination.

The temporary solution has traditionally been to save your changes to somewhere where you do have permissions (such as /tmp or ~), then use sudo to cp it across, or re-open the file in vim with sudo.

A collegue at work showed me a great trick to get around this when it happens (Thanks @rich0H). All you have to do is add this to your .vimrc file:

command! W w !sudo tee % > /dev/null

Then when you come across this error again, instead of of using :w you can use :W and it will prompt you for your sudo password before saving.

Works brilliantly!

Search and replace, vim and git

Search and replace, vim and git Continue reading

Using netrw instead of NERDTree for Vim

Published on December 28, 2016