Last Updated: September 29, 2021
·
2.069K
· itseranga

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

1 Response
Add your response

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 ·