Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

":w :term ..." (WAS: "w !sudo ..." does not work) #1716

Closed
fishman opened this issue Dec 22, 2014 · 25 comments
Closed

":w :term ..." (WAS: "w !sudo ..." does not work) #1716

fishman opened this issue Dec 22, 2014 · 25 comments
Assignees
Labels
bug issues reporting wrong behavior environment user system environment (terminal, shell, tmux) input tui
Milestone

Comments

@fishman
Copy link

fishman commented Dec 22, 2014

when i use the follow in vim it works but neovim will never successfully accept the password:

:w !sudo tee % > /dev/null

@fmoralesc
Copy link
Contributor

Can confirm.

@fwalch
Copy link
Member

fwalch commented Dec 22, 2014

never successfully accept the password

Sometimes it works (for me), but I have experienced the same problem. I'm on 951d00a.

@tarruda tarruda self-assigned this Dec 22, 2014
@tarruda tarruda added the bug issues reporting wrong behavior label Dec 22, 2014
@nrhtr
Copy link

nrhtr commented Jan 28, 2015

With a test.py like:

#!/usr/bin/env python
from getpass import getpass

pw = getpass("Enter password: ")
print("password is '%s'" % (pw,)

using :w !./test.py works as expected in vim but behaves strangely in nvim. The output is never displayed and when I strace'd the process while entering 'abcdefg' I noticed this:

[pid 24142] write(3, "Enter password: ", 16) = 16
[pid 24142] read(3, "a", 8192)          = 1
[pid 24125] read(0,  <unfinished ...>
[pid 24142] read(3,  <unfinished ...>
[pid 24125] <... read resumed> "b", 4094) = 1
[pid 24142] <... read resumed> "c", 8192) = 1
[pid 24125] read(0,  <unfinished ...>
[pid 24142] read(3,  <unfinished ...>
[pid 24125] <... read resumed> "d", 4094) = 1
[pid 24142] <... read resumed> "e", 8192) = 1
[pid 24125] read(0,  <unfinished ...>
[pid 24142] read(3,  <unfinished ...>
[pid 24125] <... read resumed> "f", 4094) = 1
[pid 24142] <... read resumed> "g", 8192) = 1
[pid 24125] read(0,  <unfinished ...>
[pid 24142] read(3,  <unfinished ...>
[pid 24125] <... read resumed> "\r", 4094) = 1
[pid 24142] <... read resumed> "\r", 8192) = 1
[pid 24125] read(0,  <unfinished ...>
[pid 24142] read(3,  <unfinished ...>
[pid 24125] <... read resumed> "\r", 4094) = 1
[pid 24142] <... read resumed> "\r", 8192) = 1
[pid 24125] read(0,  <unfinished ...>
[pid 24142] write(3, "\n", 1)           = 1
[pid 24142] write(1, "password is 'aceg'\n", 19) = 19
[pid 24142] +++ exited with 0 +++
<... read resumed> 0x12394c1, 4094)     = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=24142, si_uid=1000, si_status=0, si_utime=8, si_stime=3} ---

Is it that nvim and the subprocess are fighting over the input or something?

@tarruda
Copy link
Member

tarruda commented Jan 28, 2015

FWIW I've began working on a fix for this and all other terminal-related issues

@fmoralesc
Copy link
Contributor

This still seems to be an issue after #1820

@tarruda
Copy link
Member

tarruda commented Feb 21, 2015

@fmoralesc I'm currently extending the job module to allow spawning tty programs. After that I will integrate nvim with libvterm which will be used for running shell commands(in other words, bang commands will be ran in terminal windows, which will fix this issue)

@fmoralesc
Copy link
Contributor

fmoralesc commented Feb 21, 2015 via email

@tarruda
Copy link
Member

tarruda commented Feb 25, 2015

@pyrohh #2037 doesn't change the way shell commands are executed, it simply adds the "pty" option to job control. My next PR will take care of that though

@justinmk justinmk reopened this Feb 25, 2015
@justinmk justinmk changed the title sudo inside of neovim does not work :w should be able to write to any command (WAS: "w !sudo ..." does not work) Aug 19, 2015
@justinmk justinmk changed the title :w should be able to write to any command (WAS: "w !sudo ..." does not work) ":w :term ..." (WAS: "w !sudo ..." does not work) Aug 19, 2015
@justinmk
Copy link
Member

Changed the title to reflect what I propose is the solution to this issue: allow :w to send the buffer to any vim command (in particular, to :term). So this would work:

:w :term sudo tee % > /dev/null

There should be similar functionality for :r, that is, it should be able to get the output of any vim command without having to use :redir.

@justinmk justinmk added this to the 0.3 milestone Aug 19, 2015
@ZyX-I
Copy link
Contributor

ZyX-I commented Aug 19, 2015

I do not like the idea for special-casing :term command, it will raise questions about missing functionality. So you need to either

  1. Use something like :w !-sudo tee % > /dev/null: in place of syntax which looks like if you allow any command use special syntax which allows one command exactly.
  2. Actually allow any command. Specifically:
    • :w :{command} will do the following:
      1. setlocal bufhidden=hide, save previous bufhidden value.
      2. {command}.
      3. startinsert.
      4. set paste” (actually: disable any possibility to quit insert mode, including using <Esc> and <C-\><C-n>, _not_ set paste).
      5. Feed the file contents. In place of steps 4. and 5. fed contents may simply be modified in the fashion that makes special characters be treated literally.
      6. set nopaste”.
      7. setlocal bufhidden={previous value}. Condition: subjectable, something like “after buffer reappears in this window”. Presence of steps 1. and 7. is subjectable on their own.
    • :{range}w :{command} does basically the same thing, but only feeds a range.
    • :read :{command} will do
      1. Save cursor line number.
      2. set bufhidden=hide, save &bufhidden.
      3. {command}.
      4. Wait until someone tries to close the buffer. If {command} does not open a new buffer then next two steps are ignored.
      5. Save the entire buffer contents just before buffer was wiped out.
      6. After actually wiping the buffer paste saved lines after saved cursor line number. Wiping the buffer never closes the window, it just makes previous buffer (into which contents is :read) disappear.
      7. Restore &bufhidden.

Note: all these things may be done by the plugin with the clever use of *Cmd and some other autoevents.

Note 2: :r :{cmd} using :redir-like functionality is very different from :r :{cmd} reading from the terminal. :r :terminal will not do what you want if you use the same code used for :redir. Thus I was suggesting for :r :{cmd} to copy buffer contents and not to use something like :redir.

@justinmk
Copy link
Member

2. Actually allow any command

That's what I am proposing.

@ZyX-I
Copy link
Contributor

ZyX-I commented Aug 19, 2015

@justinmk But without the details it is hard to understand. You suggested :redir for :r (which will not work for :terminal) which clearly stated that you see :r being universal, but nothing for :w :{commmand}. And I do not think that my suggestion is trivial, special-casing :term is far easier.

@davidsteinberger
Copy link

@justinmk Is there any workaround to do a :w !sudo tee % > /dev/null in Neovim?

@ZyX-I
Copy link
Contributor

ZyX-I commented Dec 26, 2015

@davidsteinberger You may be able to replace sudo with the equivalent command which uses the GUI: AFAIK depending on the system are available or can be installed kdesudo, gksudo (not sure, but gksu exists), also something from LXDE.

I have not actually checked whether this variant will work (whether they pass stdin to the launched application) though.

@pjg

This comment has been minimized.

@justinmk
Copy link
Member

justinmk commented Aug 26, 2016

Here's a (ugly) workaround:

:%y | term sudo nvim +'set paste' +'norm! gg"_dG' +startinsert %
<C-\><C-n>
P

@sfwn
Copy link

sfwn commented Sep 27, 2016

@pjg Now I have to open a new terminal tab and type cmd like $ sudo xxx and type my password to get sudo permission, then in nvim use :w !sudo tee % since it won't let you type password again.

@stevenocchipinti
Copy link

I've also got an ugly workaround:

" Temporary workaround for: https://github.com/neovim/neovim/issues/1716
if has("nvim")
  command! W w !sudo -n tee % > /dev/null || echo "Press <leader>w to authenticate and try again"
  map <leader>w :new<cr>:term sudo true<cr>
else
  command! W w !sudo tee % > /dev/null
end

Adding -n to sudo doesn't fix the problem, but it does prevent the terminal from being messed up when you press it.

@lambdalisue
Copy link
Contributor

I made a plugin which solves this issue on neovim.

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

In case if you guys are looking forward the solution 👍

@kiooss

This comment has been minimized.

@Earnestly
Copy link

Suddenly after updating (it was a lot of commits so I haven't yet tried to bisect it) I'm also experiencing this. sudo reports this error:

sudo: no tty present and no askpass program specified

@justinmk
Copy link
Member

@Earnestly see #8389

@neovim neovim deleted a comment Jul 3, 2018
@neovim neovim deleted a comment Jul 3, 2018
@neovim neovim deleted a comment Jul 3, 2018
@justinmk justinmk added the environment user system environment (terminal, shell, tmux) label Dec 29, 2018
@LunarLambda
Copy link
Contributor

Sooo, I just ran into this too, what's the official status of this? A wontfix? Will be fixed but in a different way? Cuz I don't really wanna run all of neovim as root necessarily.

@KillTheMule
Copy link
Contributor

Can't tell you about the status, but just for this particular usecase, sudoedit is made for this.

@justinmk
Copy link
Member

justinmk commented Jan 15, 2019

@LunarLambda It is not wontfix, otherwise it would have been closed. However, I'm going to merge it with #1496 because both issues have the same root cause/solution.

Resolution/Summary


Locked to keep the summary visible. You can always chat or open a new issue if you have new information/topics to discuss.

@neovim neovim locked as resolved and limited conversation to collaborators Jan 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug issues reporting wrong behavior environment user system environment (terminal, shell, tmux) input tui
Projects
None yet
Development

No branches or pull requests