I'm editing a system-wide file called ssh_config in /etc/ssh folder using vi without root privilege. And I save this file by using the following command:
:w !sudo tee %
But it doesn't work, I'm getting this error
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
shell returned 1
Press ENTER or type command to continue
then I try another command by explicitly provide root password.
:w !echo password | sudo -S tee %
And a warning show
W12: Warning: File "user_config" has changed and the buffer was changed in Vim as well
See ":help W12" for more info.
[O]K, (L)oad File:
I already try all the above options but none of these work. Any solution? Thanks.
2 Answers
If you edit the file, THEN decide you need to be root, your process needs improvement.
Think before you type.
Read
man sudoedit, set up your $EDITOR and $VISUAL environment variables, and it's easy.
In emergencies ("I typed 97 lines of config and it won't let me save!"), save the file somewhere else (where you have write permission) :w/tmp/oops, exit the editor and use sudo cp /tmp/oops !$ to copy it to the right place.
Due to a design change, neovim doesn't yet support interactive sessions through an external program via a bang command so the :w !sudo tee % > /dev/null approach for saving as a different user with sudo doesn't work like it does in vim.
A solution is to use the suda.vim plugin instead.