Save a read-only file in VIM using sudo

Have you opened a read-only file and modified it but couldn't save? Not anymore, I found a quick tip which allows VIM users to save the file as 'sudo' even if you didn't use 'sudo' at the time of opening.

Create a shorthand mapping in VIM as follows:

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

You can save this to your ~/.vimrc file to persist it.

Now every time you want to override the read-only status and save changes as sudo, just hit 'w!!' and you'll be good.

Love the power of VIM!

Published September 25 2011

blog comments powered by Disqus