How to save a file edited in VIM using superuser permission?

How many times have you edited a file just to find out that you need root credentials to save the changes?

In situations like these, a well known trick comes to your rescue.

# Use the below syntax while saving the file from within VIM.
:w !sudo tee % > /dev/null

The internals of the above VIM command has been beautifully explained by Nathan Long at http://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work.


References:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s