DEV Community

loading...

Sudo save from within VIM

connorbode profile image Connor Bode ・1 min read

This is a critical trick for using VIM.

Here's the scenario: you've opened up a file that you don't have write permissions for. You've taken all this time to make all these edits, then you :wq to get outta there and BOOM!

Alt Text

You forgot, you needed to sudo to edit the file.

I do this all the time when editing /etc/hosts.

What's the solution?

Turns out it's really simple:

:w !sudo tee %>/dev/null

That's it. It'll prompt you for your password, and you'll be able to write the file and quit. Lifesaving technique here.

If you're interested in how this works, I have a quick explanation over at my blog.


Hope this tip was useful for you!

If you're interested in Linux, VIM, Python, Django, React Native, and many other things, follow me here on dev.to or on Twitter @connorbode.

I'll keep posting the stuff I find useful!

Discussion

pic
Editor guide
Collapse
danielsiefert profile image
Dan Siefert

Thanks for the tip!