Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Possible Duplicate:
vim re-edit as root

I could have sworn I saw this question asked. But after looking though every search result for "vi" I'm stumped/lazy.

I've opened a file, made an edit and now I realize it's read only and I've opened it as non-root me.

share|improve this question
1  
Duplicate of serverfault.com/questions/22558/vim-re-edit-as-root –  chmeee Jun 30 '09 at 20:17
add comment

marked as duplicate by splattne Dec 27 '09 at 10:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

up vote 33 down vote accepted

I think you want something like this:

:w !sudo tee "%"

I first saw it on commandlinefu. The quotes are only necessary if the file path contains spaces.

share|improve this answer
    
+1 cunning :) –  David Pashley Jun 30 '09 at 20:06
    
Thank you sir. (please enter 15 char) –  MathewC Jul 1 '09 at 12:52
add comment

i do this occasionally, and if the changes i've made are trivial, i just exit and edit it again as root.

otherwise i save the file to /tmp, and mv/cp it as root to where it really belongs later. and use chown/chgrp/chmod to fix the ownership/perms.

share|improve this answer
1  
Rather than cp/mv and chown etc., you could 'sudo cat tmpfile > realfile && rm -f tmpfile', to keep the ownership and perms of the original but give it new contents. –  mlp Jul 12 '09 at 7:58
add comment

consider adding this line to your vimrc:

" Remaps :SW to sudo save the current file and tell vim to reload it command SW execute 'w !sudo tee % >/dev/null' | e! %

share|improve this answer
add comment

Try this http://blog.sriunplugged.com/2009/12/how-to-save-file-in-vi-not-opened-with-sudo/ . This is what you want i guess

share|improve this answer
add comment

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