Last Updated: February 25, 2016
·
620
· bchretien

sudo and vim

For all those who started modifying a system file and got stuck with permission errors when trying to save their modifications, here's the magic command:

:w !sudo tee %

And if you're afraid that you may not always remember that, you can set the following mapping in your .vimrc:

" Force saving files that require root permission 
cmap w!! w !sudo tee > /dev/null %

Then, you can simply use:

:w!!

Source: this SO question.