Oftentimes I will be editing a Linux configuration file using vim only to discover that I cannot save it because the file requires root permission to write to it. This ends up looking something like this:
[sourcecode lang="bash"]
vi /path/to/some/file.conf
[make some edits]
:w
VIM Message: E45: 'readonly' option is set (add ! to override)
:q!
$ sudo vi /path/to/some/file.conf
[make all my edits AGAIN]
:w
[/sourcecode]
I have gone through this process so many times that I knew there must be an easy fix for it. (I know about sudo !!
for running the previous command, but I only recently started developing the habit of using it.) After forgetting to use sudo while editing a configuration file yet again this morning, I finally decided to search Google and find a solution. Here it is:
[sourcecode lang="bash"]
vi /path/to/some/file.conf
[make some edits]
:w
VIM Message: E45: 'readonly' option is set (add ! to override)
:w !sudo tee %
[/sourcecode]
The :w !sudo tee %
command tells VIM to write the file (w
) but run the sudo command first (!sudo
) and read the writing of the file from standard input to standard output (tee
) using the same filename as the one we're editing (%
).
After saving the file as root, you'll get this message: "W12: Warning: File "/private/etc/smb.conf" has changed and the buffer was changed in Vim as well". You'll be given the option to reload it, but since you were already looking at the new version it doesn't much matter which option you choose (OK or Reload).
And last but not least, if you don't want to remember the syntax for this command, you can map the command in your ~/.vimrc file:
[sourcecode lang="bash"]
cmap w!! w !sudo tee % >/dev/null
[/sourcecode]
Now, if you forget to edit a file with sudo, you can simply type :w!!
to fix the problem!
Hello again, Raam!
Sometime last week, Apple released a security update for JS — did that remedy the situation you are addressing in this post?
Also, since you and I have exchanged an occasional idea and solution on things web-tech related, I thought you might like to take a look at my latest blog post, http://gitanajava-productions.vox.com/library/post/goo-goo-mama-google-wants-to-be-your-mother.html.
BTW, did the couchsurfing gig last summer, less through the site than through FOFF (friends of friends’ friends), while working a project on spec for a client. It’s cyclical — amazingly great some times, comfortable others, and once or twice it was “O geez, what was I thinking?” Overall though, it is a Frugal Adventurer’s dream come true.
Hi Neon,
I think you posted this comment on the wrong post (this is the post about saving files as root from inside VIM, not the Apple Java exploit). Apple released a fix for the issue shortly after I wrote that post, so it’s probably the one you’re referring to: http://db.tidbits.com/article/10352
I read your blog post and it’s very insightful! I have added your blog to my RSS reader and will watch for new posts! 🙂
Your summary of couchsurfing (“amazingly great some times, comfortable others, and once or twice it was ‘O geez, what was I thinking?’) sounds exactly how I suspect my experience will be. Nevertheless, I consider myself very much a “frugal adventurer”, so I’m looking forward to it!
Very useful command. thx a lot
My pleasure, KK! Glad I could help. 🙂
thanks, thst`s works!
You’re welcome, Fedor! 🙂
Raam, thank you sir!
I don’t know how long I’ve been doing the same routine –
vi /somefile
:q!
sudo vi /somefile
Alex
You’re most welcome, Alex! I’m glad that helped. 🙂
This is an old post but I stumbled onto it finding myself in just the situation you did. Thanks for the post.
I’m glad you found this helpful, Chris! 🙂
I’m just starting my journey with vim and I would like to thank you for making it easier for me 😉
You’re most welcome! 🙂