Ever started editing a document only to find out that when trying to save it :w or :wq, you forgot to open it with sudo? Vim knows what to do and gives you a hint saying “add ! to override”.


Instead of having to save it to a temp directory :w ~/temp then exiting and having to move it to the real location. Use the following the next time:

:w ! sudo tee %

This invokes the write function :w and tells it to overrite the file if it exists !. Sends all the contents via standard out into the program tee with sudo privileges. tee is an amazing program which outputs the contents of stdin to stdout and to a file. In this case the filename is the vim shortcode %.

For more information use :help :w! while inside vim to bring up the help menu. (Use :w to close the new panel when done)

Enjoy a new way to save your vim files.