Wednesday, 24 April 2013

vi - Saving file with sudo(root rights)

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)

No comments:

Post a Comment