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)
...
For further actions, you may consider blocking this person and/or reporting abuse
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.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
(orsudo -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.
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!
Man though crowd. I like this shortcut. Thanks for posting.
Haha, thanks :)
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.I use this map:
However, I prefer to edit files with this alias:
Just a warning for anyone that uses this,
sudoedit
orsudo -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 insudoedit
until I closed my editor, but I was retrying after just writing the change.Oh wow that‘s stupid... not very usefull.
Well, I decided to follow you here just so, I can learn more from you. I'm a Software Developer by the way.
Nice trick! Unfortunately, at this moment it's not working in Neovim. I have to use a plugin instead github.com/lambdalisue/suda.vim
I've maid a :W command, instead of this ugly expansion with cnoremap
And a :WQ too)
Will this work for paths with spaces?
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.