How can I save a file I opened in vim as the wrong user?
Just simply type your command with sudo
like sudo vi filename.txt
.
Then, hit i
, edit what you want to change and hit Esc.
Then :wq
E45: 'readonly' option is set (add ! to override)
press Esc, then type :qa! and press Enter
Try the below command
:w !sudo tee %
Explanation
- :w – write
- !sudo – call shell sudo command
- tee – the output of write (:w) command is redirected using tee
- % – current file name
More info