I am using NeoVim v0.4.3. This is the latest version available in repository. I have an Arch build with the most up to date packages and kernel 5.6.4-arch1-1.
Occasionally I edit a file with nvim that I don't have permissions to edit. I only realise this as I am about to save my work.
Recently I edited
/etc/pulse/default.pa
with command
nvim /etc/pulse/default.pa
and no use of sudo.
This file has the following permissions
$ ls -la /etc/pulse/default.pa
-rw-r--r-- 1 root root 4966 Jan 11 18:25 default.pa
So I did some googling and tried to save it with
:w !sudo tee %
and got 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
It also showed, on the highlighted bar, at the base of the screen, the following
/etc/pulse/default_2.pa [+][RO]
This did'nt work.
So I tried
:w !echo password | sudo -S tee %
It then displayed the following
:w !echo password | sudo -S tee %
[sudo] password for user:
W12: Warning: File "/etc/pulse/default.pa" has changed and the buffer was changed in Vim as well
See ":help W12" for more info.
[O]K, (L)oad File:
So I pressed O
and nothing happened.
I then tried :wq
and it complained the file was read only.
I then went through the process again and this time pressed L
and it showed
:w !echo password | sudo -S tee %
[sudo] password for user:
W12: Warning: File "/etc/pulse/default.pa" has changed and the buffer was changed in Vim as well
See ":help W12" for more info.
"/etc/pulse/default.pa" [readonly] 0L, 0C
Press ENTER or type command to continue
and nvim left me with a blank screen and an overwritten etc/pulse/default.pa
file with nothing in it.
I've recovered the file but want to know how to tee
the file down to disk when I open the file without the correct permissions.
I'm quite new to vim and think I got this working before but can't for the life of me remember how?
exe 'w !sudo tee >/dev/null %:p:S' | setl nomod
, and I export this variable in my shell (from~/.zshenv
):export SUDO_ASKPASS='/usr/lib/ssh/x11-ssh-askpass'
. On Ubuntu thisx11-ssh-askpass
file is provided by thessh-askpass
package. I don't know what's the equivalent package on arch, nor where it's actually installed on an arch system.sudoedit /etc/pulse/default.pa
(orsudo -e /etc/pulse/default.pa
) which will make a temporary copy of the file for you to edit. Make sure your$EDITOR
variable is set correctly so it uses nvim for editing the temporary file..bashrc
,export SUDO_ASKPASS='/usr/lib/ssh/x11-ssh-askpass
Thex11-ssh-askpass
package has same name on Arch. I installed this package withsudo pacman -S x11-ssh-askpass
& inserted this line into my~/.vimrc
,command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
. Now can I edit root owned files without losing data, saving with:W
(capital W). To make nvim more portable can I pass the password to sudo within nvim? I triedcommand W :execute ':silent w !echo password | sudo -S tee % > /dev/null' | :edit!
but this does not work.