Vim - Save file as root without reopening file

File requires root permission to be written, but sometimes we forget to add sudo before vim command. The result is after editing the file with a lot of changes, :w cannot save them.

This can be avoided by adding a simple command to $HOME/.vimrc:

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

Then when we type :w!!, it will be mapped to :w !sudo tee > /dev/null %, which will let we save the file with sudo without re-opening the file.

vim 

See also

comments powered by Disqus