How do you get sudo access for a file inside the vi text editor?

Viewed 80590

Often while editing config files, I'll open one with vi and then when I go to save it realize that I didn't type

sudo vi filename

Is there any way to give vi sudo privileges to save the file? I seem to recall seeing something about this while looking up some stuff about vi a while ago, but now I can't find it.

10 Answers

% is replaced with the current file name, thus you can use:

:w !sudo tee %

(vim will detect that the file has been changed and ask whether you want to it to be reloaded. Say yes by choosing [L] rather than OK.)

As a shortcut, you can define your own command. Put the following in your .vimrc:

command W w !sudo tee % >/dev/null

With the above you can type :W<Enter> to save the file. Since I wrote this, I have found a nicer way (in my opinion) to do this:

cmap w!! w !sudo tee >/dev/null %

This way you can type :w!! and it will be expanded to the full command line, leaving the cursor at the end, so you can replace the % with a file name of your own, if you like.

In general, you can't change the effective user id of the vi process, but you can do this:

:w !sudo tee myfile

If you're using Vim, there is a script available named sudo.vim. If you find that you've opened a file that you need root access to read, type

:e sudo:%
Vim replaces the % with the name of the current file, and sudo: instructs the sudo.vim script to take over for reading and writing.

Ryan's advice is generally good, however, if following step 3, don't move the temporary file; it'll have the wrong ownership and permissions. Instead, sudoedit the correct file and read in the contents (using :r or the like) of the temporary file.

If following step 2, use :w! to force the file to be written.

When you go into insert mode on a file you need sudo access to edit, you get a status message saying

-- INSERT -- W10: Warning: Changing a readonly file

If I miss that, generally I do

:w ~/edited_blah.tmp
:q

..then..

sudo "cat edited_blah.tmp > /etc/blah"

..or..

sudo mv edited_blah.tmp /etc/blah

There's probably a less roundabout way to do it, but it works.

A quick Google seems to give this advice:

  1. Don't try to edit if it's read-only.
  2. You might be able to change the permissions on the file. (Whether or not it will let you save is up to experimentation.)
  3. If you still edited anyway, save to a temporary file and then move it.

http://ubuntuforums.org/showthread.php?t=782136

Here's another one that has appeared since this question was answered, a plugin called SudoEdit which provides SudoRead and SudoWrite functions, which will by default try to use sudo first and su if that fails: http://www.vim.org/scripts/script.php?script_id=2709

I have this in my ~/.bashrc:

alias svim='sudo vim'

Now whenever I need to edit a config file I just open it with svim.

A quick hack you can consider is doing a chmod on the file you're editing, save with vim, and then chmod back to what the file was originally.

ls -l test.file (to see the permissions of the file)
chmod 777 test.file
[This is where you save in vim]
chmod xxx test.file (restore the permissions you found in the first step)

Of course I don't recommend this approach in a system where you're worried about security, as for a few seconds anyone can read/change the file without you realizing.

Related