Question
Often while editing config files, I'll open one with vi and then when I go to save it realize that I forget the "sudo". For example:
Is there any way to give vi sudo privileges to save the file while already opening the file? I seem to recall seeing something about this while looking up some stuff about vi a while ago, but now I can't find it.
How-To
Refer to below command inside command-line mode. % is replaced with the current file name, thus you can use:
vim will detect that the file has been changed and ask whether you want to it to be reloaded.
As a shortcut, you can define your own command. Put the following in your ~/.vimrc:
- command W w !sudo tee % >/dev/null
- cmap w!! w !sudo tee >/dev/null %
沒有留言:
張貼留言