tags:

views:

3961

answers:

11

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.

A: 

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

Ryan Fox
+3  A: 

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.

Chris Jester-Young
+13  A: 

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

:w !sudo tee myfile
Mark Harrison
`:w !sudo tee % >/dev/null` or `:w !sudo dd of=%` avoid having the content of the file echoed back as the file is saved.
jamessan
A: 

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.

num1
+2  A: 

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.

dbr
cat $tmp > $target is creative, but is there any reason not to just sudo mv the file?
ojrac
Good point.. There was also a big problem with sudo cat something > /etc/blah - that will use sudo to cat the file, then the regular user to write to /etc/blah (which wont work).. Fixed it in the answer, and added your better sudo mv suggestion..
dbr
A: 

maybe just save a copy in your home directory and "sudo mv" it later

paan
+44  A: 

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

As a shortcut, you can define 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.

hop
Awesome. Thanks!
ojrac
Does this work in gvim? Running the command causes Vim to prompt for password but not accept input. It eventually times out on two attempts and says: `sudo: 1 incorrect password attempt`
Casey
i would see no reason for gvim to behave differently... ar you sure sudo by itself works correctly?
hop
I same problem in gVim.
Frankovskyi Bogdan
If you need root privileges to save a new file, replace % with the name (including path) of the new file.
gvkv
+3  A: 

If you're using Vim, there is a script availible 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.

ephemient
I wouldn't recommend using this script as it doesn't take proper precautions with escaping filenames.
jamessan
+1  A: 

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.

pisswillis
The `sudoedit` command should be preferred since it doesn't require running vim as root.
jamessan
This still doesn't stop you tying vim instead of svim which is what the OP eluded to.
ScaryAardvark
A: 

You can try the method I have explained in my blog. Try it. http://blog.sriunplugged.com/shell-scripting/how-to-save-file-in-vi-not-opened-with-sudo/

Srijith R
A: 

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

James Snyder