save a read-only file in vim
If you’ve opened a file read-only, or if you don’t want to run sudo vim
for some reason, you can use this trick to save the file anyway.
:w !sudo tee %
Because you’re working around the official save behavior, it’ll give you a warning that the file has changed on disk, and ask if you would like to reload it. It doesn’t matter which option you pick: the version you saved is exactly the same as what’s in your buffer already.