4 ms·
If you forgot to sudo vi, you've already edited the file, saving the file as root without first saving it as your own and then having to move it: <ESC>:w !sudo
by aequitas 8y ago
If you forgot to sudo vi, you've already edited the file, saving the file as root without first saving it as your own and then having to move it:
<ESC>:w !sudo tee %
Pipes the content of the file to a tee process as root and lets tee save it under the current file name. Vim will tell you the file has changed on disk and ask if it should reload it afterwards.
- dllthomas 8y ago> If you forgot to sudo vi IMO, it's better not to do that in the first place (which, of course, only makes your tip all the more useful!)
- mugurel_chirica 8y agoMake sure you are 100% confident that you know the proper syntax for this when you use it. I once blanked by mistake an important file, lucky for me I had a backup.
- dllthomas 8y agoGenerally a good idea, before you sudo anything. That's one of the reasons to prefer this approach to `sudo vim`, actually - that's essentially "sudo of anything vim does", and that's a lot of very different things just a couple keystrokes away.