TECH SOLUTIONS

Click here to edit subtitle

Forums

Post Reply
Forum Home > Unix Learnings > vi - Saving file with sudo(root rights)

Sourav Gulati
Site Owner
Posts: 83

Suppose you have open a file with "vi editor". After making changes you come to know that you need root user right to save it(sudo rights) then instead  reopening file with "sudo vi" and make changes again. you can save it as

:w !sudo tee %

Where,

:w - Write a file

!sudo - Call shell sudo command

tee - The output of write (vim :w) command redirected using tee. The % is nothing but current file name i.e. /etc/apache2/conf.d/mediawiki.conf. In other words tee command is run as root and it takes standard input and write it to a file represented by %. However, this will prompt to reload file again (hit L to load changes in vim itself)

--


November 27, 2012 at 6:56 AM Flag Quote & Reply

You must login to post.