24

Possible Duplicate:
Becoming root from inside Vim

It could happen to anyone. The admin types:

vi /etc/resolv.conf

as a regular user. He edits the /etc/resolv.conf file, but when he wants to hit :x he suddenly notices that he was editing the file in read-only mode because he opened it with a normal user, not root.

Are there any vi commands to get root privileges, so that the admin won't have to hit :q! and sudo su - and vi /etc/resolv.conf and edit it again?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
LanceBaynes
  • 39,295
  • 97
  • 250
  • 349

1 Answers1

51

In the page Top Ten One-Liners from CommandLineFu Explained is suggested this trick (the #3):

:w !sudo tee %

this write the current buffer to the stdin of the command after the !. The % symbol is substituted with the current filename.

enzotib
  • 50,671
  • 14
  • 120
  • 105