Yongqiang Zhou Yongqiang Zhou -4 years ago 95
Linux Question

Can I gain root permission without leaving vim?

Sometimes when I edit a file which require root permission (e.g. files under /etc), but I forget run vim as sudo.

After edit finished, and type

to save and leave, I find I can't, even using
, because the file is readonly.

If I leave and re-edit the file, all my work will lost, but if not, I cannot save my edit. So, how can I gain root permission to write without leaving vim?

Answer Source

To force a save use the following command

:w !sudo tee %

It will prompt you for your password.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download