Monday, April 12, 2010

Save file owned by root

Have just seen the nice trick on the Interwebs.

It happens often: one starts editing a file owned by root. One gets used rather quickly to the VIM's warnings about editing a file one does not have permissions to. Only much later realizing that you can't save it.

So you opened a VIM and edited a file, but can't save it. Instead of saving a temp one can use the trick:

:w !sudo tee %

P.S. ZOMG PONYZ!!1 For what I was always adding a custom shortcut :map <F6> :b#<CR> turned out to have an official shortcut: ^6 (:help CTRL-6). In VIM 7.1, this is sole shortcut of the form CTRL-<number>.

1 comment:

Anonymous said...

Be careful when doing this trick. file permissions may be reset to the ones by your umask instead of the perms you want.