Edit file vi, not opened by sudo
Opened a file in vi without sudo
. Edited some stuff, realized you can't save it. You close the file, open with sudo, redo the edits.
How about, switching to sudo while vi is still open?
Here's what you got to do:
-
Open file and edit normally.
vi filename
-
Edit normally.
-
Try saving as you usually do.
:w
. Meet the error
E212: Can't open file for writing
Press ENTER or type command to continue -
Choose to type the command, just type
:w !sudo tee %
-
Press "O" for Ok. Exit vim with
:q!
Your edits are saved.