How to use sudo from within Vim to save file

It is very annoying to see message "E212: Can't open file for writing" when trying to save file.

Hopefully there is a simple solution (a bit longer but without "W12 Warning"):

:execute ':w !sudo tee % > /dev/null' | :edit!

Look at commandlinefu.com for more solutions like :W command below:

:command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu