How to save a file with root-priviledges in a normal Vim session

This is one thing perhaps every person who tinkers with his/her Linux would love. You open Vim to edit a system file, do a couple of changes, and try to save. Oh you forgot to add sudo whoile opening Vim? Close, restart it, and do the changes again. Well here’s a great trick to fix that, I’ve been using it since quite a while.

Just add this to your .vimrc:

cmap w!! w !sudo tee % >/dev/null

To use this, instead of doing a :w! while saving, do a :w!! and then enter your password as prompted. Voila.

For those interested, this mapping uses the Linux utility named tee, which writes to file and stdout both. The file here is /dev/null (gutter :P ). cmap does mapping only in the console portion where you type commands.