2

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?

4
  • 2
    FWIW, I use this command: 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 this x11-ssh-askpass file is provided by the ssh-askpass package. I don't know what's the equivalent package on arch, nor where it's actually installed on an arch system. – user938271 May 1 '20 at 17:04
  • 2
    Also consider using sudoedit /etc/pulse/default.pa (or sudo -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. – filbranden May 1 '20 at 17:08
  • Thank you. I added this line to .bashrc, export SUDO_ASKPASS='/usr/lib/ssh/x11-ssh-askpass The x11-ssh-askpass package has same name on Arch. I installed this package with sudo 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 tried command W :execute ':silent w !echo password | sudo -S tee % > /dev/null' | :edit! but this does not work. – Kes May 1 '20 at 20:56
  • @user938271 can you put this up as an answer please so I can press the tick button, thank you – Kes May 1 '20 at 22:57
1

You could try this :W command:

com -bar W exe 'w !sudo tee >/dev/null %:p:S' | setl nomod

Broken down:

"               ┌ write the buffer on the standard input of a shell command (`:h w_c`)
"               │ and execute the latter
"               │
"               │   ┌ raise the rights of the `tee(1)` process so that it can write in
"               │   │ a file owned by any user
"               ├─┐ │
com -bar W exe 'w !sudo tee >/dev/null %:p:S' | setl nomod
"                           ├────────┘ │ 
"                           │          └ but write in the current file
"                           │
"                           └ don't write in the terminal

You may also need to tell sudo(8) which helper program should be executed to read your password.

According to man sudo, there are 2 ways to do it. You can either export the environment variable SUDO_ASKPASS, and assign it the path to the helper program; e.g.:

export SUDO_ASKPASS='/usr/lib/ssh/x11-ssh-askpass'

Here, the helper program is provided by the Ubuntu package x11-ssh-askpass; the name of the package, and the location of the file may differ on your OS. Export the variable from an init file sourced by your shell (e.g. ~/.bash_profile, ~/.zshenv, ...).

Or you can edit the sudo.conf(5) file, and include this directive inside:

Path askpass /usr/lib/ssh/x11-ssh-askpass

For more info about the cause of the issue, see this entry in the Neovim FAQ and the links it provides.

4

There’s a plugin for this. The reason the old :w !sudo tee % doesn't work with Neovim has to do with the refactoring following a client-server architecture, the team is working on a solution though that hopefully will be shipped with 0.5

2
  • I was sure I had :w !sudo tee % working somewhere before and thought it might have been in vim and not neovim, which you have confirmed. Thank you for the clear explanation. – Kes May 1 '20 at 22:56
  • I tried this plugin, and apparently it has a bug that prevents it from saving "large" files. The threshold for "large" is fairly low: it's failing for me on a file with 792 lines. github.com/lambdalisue/suda.vim/issues/5 – raddevon Jan 18 at 15:46
2

Try suda.vim : https://github.com/lambdalisue/suda.vim

In case you use vim plug, after plugging it (adding the line) with Plug 'lambdalisue/suda.vim' do a :PlugInstall and thereafter with :SudaWrite instead of the regular :w you will be prompted for a password and can save files as root which have been opened without root permission.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.