DEV Community

The Vim Trick Which Will Save Your Time and Nerves

jovica on September 11, 2018

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) ...
Collapse
 
bradenbest profile image
Braden Best • Edited

I just use :w !sudo tee % when that happens. I don't come across that situation enough to justify a mapping, and the command is easy to reinvent when needed.

w <file> writes to a file, and can be redirected to an external program's stdin if file is !<command>

sudo tee <file> takes its input and writes it to both stdout and file.

% is vim shorthand for the file open in this buffer.

Btw: sudo -e some_file. Try it.

Collapse
 
tux0r profile image
tux0r

the magic trick which would take another post to explain

Here's a comment to save you another post:

  • ! executes a command and returns its result. This was adopted from Vim's predecessors, it already exists in ed (which begat em which begat en which begat ex which begat vi which was cloned as stevie which begat Vim, eventually).
  • tee splits the input (here: the contents of the buffer which is % in Vim's language) into an output file (redirected into the void, >/dev/null) and an output stream (returned).
  • write is the equivalent to :w.

Wouldn't /dev/zero be faster, by the way?

Collapse
 
jovica profile image
jovica

hah - thanks :) It's just hardly understandable for beginners in this kind of short form - that's what I had in mind when I mentioned another post.

I didn't test the difference between the /dev/zero and /dev/null, but it shouldn't make a really big difference in this case. Cheers!

Collapse
 
michaelmior profile image
Michael Mior

At least on my machine, /dev/zero and /dev/null are the same speed. Even if they were different, I doubt it would be enough to matter unless you were writing a really massive file.

Collapse
 
supahambition profile image
Supahambition

Or use the chmod command to change the permissions of the file right?
Idk about you guys but I'm not particularly comfortable with running vim through Super User all the time. But maybe that's just being paranoid?

Collapse
 
jdnewman85 profile image
jdnewman85

This 'trick' alleviates the need to run vim itself as root. Changing permissions is not always preferable. How do you edit system configs? Full blown root user?

Your paranoia has caused you to be less safe...

Collapse
 
italovieira profile image
Ítalo Vieira • Edited

sudoedit (or sudo -e) is the right way to do when you have not yet open the file with Vim.

Collapse
 
vinneycavallo profile image
vinney cavallo

Sometimes you want the permissions staying the same, but you need the file changed regardless. Maybe this is bad practice, I dunno. Would be curious to hear it discussed by folks who know more than I do.

Collapse
 
samyi profile image
Sam Yi

Man though crowd. I like this shortcut. Thanks for posting.

Collapse
 
jovica profile image
jovica

Haha, thanks :)

Collapse
 
kevcui profile image
Kevin Cui

Nice trick! Unfortunately, at this moment it's not working in Neovim. I have to use a plugin instead github.com/lambdalisue/suda.vim

Collapse
 
gnu profile image
Gnu's Not Unix • Edited

I use this map:

cnoremap WW w !sudo tee > /dev/null %

However, I prefer to edit files with this alias:

alias svim='sudoedit'
Collapse
 
atomicwrites profile image
David Castro

Just a warning for anyone that uses this, sudoedit or sudo -e makes a temporary copy of the file and then moves it on top of the real file when the editor exits. Which means that writing your changes doesn't actually modify the file, which caused me much head banging because no matter what I did I'd get the same error since I was making changes that were buffered in sudoedit until I closed my editor, but I was retrying after just writing the change.

Collapse
 
ploppy1337 profile image
ploppy

Oh wow that‘s stupid... not very usefull.

Collapse
 
gcorrel profile image
gcorrel

Well, I decided to follow you here just so, I can learn more from you. I'm a Software Developer by the way.

Collapse
 
necauqua profile image
Anton Bulakh

I've maid a :W command, instead of this ugly expansion with cnoremap
And a :WQ too)

Collapse
 
cebtenzzre profile image
Cebtenzzre

Will this work for paths with spaces?

Collapse
 
jerpint profile image
jerpint

How does it work practically? When am I prompted for a password?

Collapse
 
vinneycavallo profile image
vinney cavallo

If you’re attempting to edit a file for which your user on the system doesn’t have write access, say.