Imagine this scenario:
You're logged into a server. You've been editing a file in vi
for 30 minutes and you're finally about to finish. You type :wq
to save and quit, but a warning comes up.
'readonly' option is set (add ! to override)
Dammit. You forgot that the file is owned by root, and you don't have the privileges you need to write to it.
How can you get privileges without losing your edits?
Use this great trick:
:w !sudo tee %
How does this work?
There's a great explanation on Stack Overflow, but we'll provide a brief summary:
:w [file]
will write the current document to[file]
:w ![cmd]
will pipe the current document to[cmd]
, a command of your choice:w !sudo tee [file]
will write the current document to[file]
, using superuser privileges. Thetee
command allows you to pipe stdin to a subcommand / file and also to stdout.%
refers to the current filename in VIM, so:w !sudo tee %
is going to write the current document to the opened filename, with superuser privileges.