The Vim Trick Which Will Save Your Time and Nerves
jovica Sep 11
Whether you're beginner or advanced Vim user, I'm sure you've seen this at least once:
E45: 'readonly' option is set (add ! to override)
It usually goes like this:
You open a file with Vim and make some changes. When you try to save the file and see the message above.
Then you realize that you didn’t run Vim with
sudo?! Argh, so annoying isn't it?
Today, you're gonna learn how to solve this easily, and once for all.
It's very simple, all you need to do is to add this line to your
cnoremap w!! execute 'silent! write !sudo tee % >/dev/null' <bar> edit!
When you get to the same situation again and you can't save your file, simply run
:w!! command, and your changes will be saved!
Now, how this works:
cnoremap- tells Vim that the following mappings that is used when in command-line mode
w!!- the mapping (shortcut) itself.
execute '<command>'- executes a command between the quotes
silent!- run it silently
write !sudo tee % >/dev/null- the magic trick which would take another post to explain
<bar> edit!- this calls the edit command to reload the buffer and then avoid messages such as "the buffer has changed".
Hint: you need to type
:w!! fast, so it expands into the full command. Typing these four strings slowly and hitting
Enter won't work.
I share tips like this at Mastering Vim Quickly Newsletter.
I also wrote a book Mastering Vim Quickly: From WTF to OMG in no time
You can also get the best Vim tips by following @MasteringVim
when should you quit your current job to pursue your goals. I see postings all the time of how people quit their job saved up money for x time and begin working toward there goal of getting a better career. I want to talk about the other half of this for a minute of is this really a smart move?