Friday, February 2, 2018

How to save changes of read-only file from vim.

It happened to me a lot that after doing some updates to a file by using vim, I forgot it needed root permission, so I have to use a known command which allow to me to do this operation without leaving the state.

:w !sudo tee %

But let's say we want this to be a little more automatic task, well I found this trick very useful which consists of adding the following line to your ~/.vimrc file:

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

So now you just need to execute:

:w!!

to get this done.