zb9oyq
Last Updated: March 02, 2016
·
189
· itseranga
892414 10151468467596730 1847316210 o

Become root inside vim

Scenario

  • When you editing file(file which required root permission to edit) via vim without sudo, vim won't allow you to save the file
  • For an example
vim /etc/hosts
  • When doing changes vim says

Picture

  • When saving file vim syas

Picture

Solution

  • To overcome this problem you can type following command in vim
:w !sudo tee %
  • Then vim will prompt for sudo password

Picture

  • Finally hit L to load the changes into vim

Picture

Say Thanks
Respond

1 Response
Add your response

19920
Avantar2

Put the following into vimrc for convenient if you always forgot sudo.

"Allow saving of files as sudo when I forgot to start vim using sudo.

cmap w!! w !sudo tee > /dev/null %

over 1 year ago ·