Vim Tricks: sudo write current file
Execute sudo write from within vim
Problem
You've opened a file in vim, made a bunch of changes then when you go to save you realize that you needed to open with sudo.
:w
212: Can't open file for writing
Solution
Rather than closing vim and doing all the changes again you can execute a sudo write from within vim
:w !sudo tee % > /dev/null
vimrc
I've saved this command in my .vimrc so I can do this with :W
command W w !sudo tee % > /dev/null