DEV Community

Connor Bode
Connor Bode

Posted on

8 1

Sudo save from within VIM

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!

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (1)

Collapse
 
danielsiefert profile image
Dan Siefert

Thanks for the tip!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay