alt text

Did you ever have problem saving read-only files in Vi / VIM? Dont worry there is a super simple hack for that.

This is neat trick in VI / VIM will let you save a read-only file as sudo without exiting the session.

:w !sudo tee %

How does it work?

“:w” sends the current buffer of the file edited to tee command which in turn saves the buffer to the file name specified by ”%”. In this case ”%” is the name of the current file.

Creating a Shortcut in .vimrc

You can add the following line to your .vimrc  file to create a shortcut for this command.

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