Logged in to vi, made changes, forgot to sudo first - now what

Solution 1:

In this case, I write the file with :w /tmp/tmpfile. Then I go out and move /tmp/tmpfile to my old file with sudo rights.

Solution 2:

From SO:

:w !sudo tee %

I actually find myself using this way to do it more frequently now:

:%!sudo tee %

I think it's a little more intuitive, as I know what :%! does, whereas I don't have a visceral understanding of :w !. Also, it's easy to miss the very important space between the w and the !.