DEV Community

Clavin June
Clavin June

Posted on • Originally published at clavinjune.dev on

3 2

Vim Save Changes With Sudo

Photo by @6heinz3r on Unsplash

Introduction

Sometimes, when you need to modify a file that needs root permission, you forgot to use sudo before open it using vim. This is quite annoying when you have changed a lot of lines, but you can't save the modification.

Even though Vim has already warned us before editing using this line:

"W10: Warning: Changing a readonly file" -- Vim

That doesn't sounds like a warning, more like a not-so-threatening statement.

To avoid this problem, there are 2 ways that I know that may save your works too.

You Don't Have Root Access Way

:w /tmp/my-modifications
Enter fullscreen mode Exit fullscreen mode

You can save it to another file using :w <accessible-path>. Vim will write the current buffer to any path that you define there.

You Have Root Access Way

:w !sudo tee %
Enter fullscreen mode Exit fullscreen mode

Then you will be prompted to type your password and the modification will be saved. :w !sudo tee % will pass the current buffer to sudo tee % command, where % is your current filename.

Conclusion

I believe this problem is annoying to you as well, I hope this article will find you.

Thank you for reading!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

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

Okay