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) It u... [Read Full]
markdown guide
 

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.

 

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?

 

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!

 

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.

 

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?

 

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...

 

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

 

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.

 
 

I use this map:

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

However, I prefer to edit files with this alias:

alias svim='sudoedit'
 

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.

 
 

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

 

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

 

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

 
 

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

 

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

code of conduct - report abuse