Becoming root from inside Vim
Solution 1
sudo cannot change the effective user of an existing process, it always creates a new process that has the elevated privileges and the original shell is unaffected. This is a fundamental of UNIX design. I most often just save the file to /tmp as a workaround. If you really want to save it directly you might try using a feature of Vim where it can pipe a file to another process. Try saving with this command:
:w !sudo dd of=%
Tested and works. Vim will then ask you to reload the file, but it's unnecessary: you can just press o to avoid reloading and losing your undo history. You can even save this to a Vim command/function or even bind it to a key for easy access, but I'll leave that as an exercise to the reader.
Solution 2
Saving the file as root:
:w !sudo tee %
Solution 3
Call sudoedit
to edit files as root.
Think of the inconvenience when calling vim
directly as a warning that you're being too casual in doing something potentially dangerous.
Solution 4
You can put this in your .vimrc
cmap w!! %!sudo tee > /dev/null %
You trigger it by doing :w!!
- it will push the file through sudo tee
to the current filename (%
).
From a now-deleted post at https://stackoverflow.com/questions/95072/what-are-your-favorite-vim-tricks; see also: https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim
Solution 5
This also works well:
:w !sudo sh -c "cat > %"
This is inspired by the comment of @Nathan Long in this answer.
NOTICE:
"
must be used instead of '
because we want %
to be expanded before passing to shell.
Mad Scientist
Updated on September 18, 2022Comments
-
Mad Scientist over 1 year
Sometimes I start editing configuration files in
/etc
using Vim, but forget to usesudo
to start Vim. The inevitable result then is that after finishing my edits I encounter the dreaded notice that I don't have the permission to save the file.Mostly the edits are small enough that I just exit Vim and do the whole thing again as root. I could of course save to a location I can write to and then copy as root, but that is also somewhat annoying.
But I'm sure there is an easier way to become root or use
sudo
from inside Vim, without having to discard the changes. If the method would not rely onsudo
being set up for the user that would be even better.-
Admin over 12 yearsI got into the habit of using
sudoedit
instead ofvim
for all admin editing. It's one way to never run into this problem anymore :). +1 for Gilles' answer. -
Admin over 10 yearsThese alernative Q&A's from SO are useful so linking to them here: Getting root permissions on a file inside of vi? & How does the vim “write with sudo” trick work?.
-
-
msaif about 13 yearsYou should wrap the
%
in quotes in case the filename contains a space. -
msaif about 13 yearsAlso: this will not reset the
'modified'
setting; you can do that manually withset nomodified
. -
msaif about 13 yearsActually, if you really want to be careful, you should do
exec 'w !sudo dd of=' . shellescape(expand('%'))
in case the filename contains quotes, backslashes, dollar-signs, etc. -
jwodder about 13 yearsHow is that different to the Commandlinefu -command by plaes?
-
penguin359 about 13 years@hhh Using tee as @plaes suggests will send the entire file to your terminal screen as well as write it out to the filesystem which may not be very desirable, especially when editing a large file over a slow terminal/network connection.
-
jw013 over 12 yearsOne could always add
> /dev/null
to thetee
command. -
penguin359 over 12 yearsHmm, maybe someone needs to post the performance comparison between using "dd of=%" vs. "tee % >/dev/null" On one hand dd copies using tiny 512 byte blocks, but on the other hand, tee spins it's wheels writing a full copy of the file to the bit-bucket. I wonder who will win in the end.
-
towo over 7 yearsAnd if you've got moreutils installed, just use
!sudo sponge %
instead. -
TamaMcGlinn about 4 yearsthis throws an error for me:
sudo: no tty present and no askpass program specified
.askpass
is accessible directly and works fine normally.