Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Sometimes I start editing configuration files in /etc using Vim, but forget to use sudo 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 on sudo being set up for the user that would be even better.

share|improve this question
1  
I got into the habit of using sudoedit instead of vim for all admin editing. It's one way to never run into this problem anymore :). +1 for Gilles' answer. –  jw013 Jan 5 '12 at 1:26
    
These 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?. –  slm Aug 20 '13 at 12:58
add comment

4 Answers

up vote 20 down vote accepted

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.

share|improve this answer
2  
That's an interesting solution, but you could replace the path with %, which in vim will be the path of the file you're currently editing. –  Reid Apr 10 '11 at 13:54
    
You should wrap the % in quotes in case the filename contains a space. –  intuited Apr 10 '11 at 16:37
    
Also: this will not reset the 'modified' setting; you can do that manually with set nomodified. –  intuited Apr 10 '11 at 16:44
2  
Actually, 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. –  intuited Apr 10 '11 at 16:53
    
How is that different to the Commandlinefu -command by plaes? –  user2362 Apr 10 '11 at 23:49
show 4 more comments

Saving the file as root:

:w !sudo tee %

share|improve this answer
add comment

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.

share|improve this answer
add comment

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 http://stackoverflow.com/questions/95072/what-are-your-favorite-vim-tricks

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.