Place the following in your .vimrc file
cmap w!! %!sudo tee > /dev/null %
Then when you need to make changes to a system file, you can override the read-only permissions by typing :w!!, vim will ask for your sudo password and save your changes.
This plugin: https://github.com/tpope/vim-eunuch allows similar behaviour.
works perfectly, thanks
This is a great tip! Thanks for saving me so many (:q! sudo !!)s
I prefer cmap w!! w !sudo tee %
cmap w!! w !sudo tee %
This is gold, thanks