How to save a current file opened with vim when ‘root’ access is required

Sometimes when editing a configuration you may find out that the file you opened was opened as ‘sudo’ hence you won’t be able to save it.

There is a solution to it so you don’t have to lose the configuration you have already done.

:w !sudo tee %

% means the current file

:w is saving the buffer not a current file, so if you type type in :w !sudo tee test2.txt ,this would save the buffer in the new test2.txt file instead of the current opened file.

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