fmthoma / vgrep

A pager for grep
BSD 3-Clause "New" or "Revised" License
104 stars 8 forks source link

"Vim: Warning: Input is not from a terminal" when using the "git vgrep" alias #49

Closed sjakobi closed 3 years ago

sjakobi commented 6 years ago

When I use git vgrep with the alias defined in the README, I run into the above warning when I try to open a file. In addition, vim doesn't open the file on the line selected in vgrep, but on the first line, which is rather inconvenient.

Is there a way to make vim play nice with git vgrep?

fmthoma commented 5 years ago

I did some digging, and apparently this is an issue with vim itself: https://github.com/vim/vim/issues/982. It occurs when the stdin passed to vim is not your TTY, but some file descriptor, e.g. a pipe.

vgrep runs the editor by passing its own stdin to the editor command. This works correctly for vim if you directly invoke vgrep, but not if it is part of a pipe, which is exacty the case for the git vgrep alias.

I can reproduce the issue with vim, while it works correctly with neovim and emacs.

A possible solution is to replace the stdin in createProcess in https://github.com/fmthoma/vgrep/blob/1a3761c04b1bcc0d5b40d4546470b5b5c6d74f47/app/Main.hs#L264 with a TTY (see https://hackage.haskell.org/package/process/docs/System-Process.html#v:createProcess), but this should be tested thoroughly with other editors.

fmthoma commented 5 years ago

@sjakobi Can you try with #50?

sjakobi commented 5 years ago

Thanks for looking into this, @fmthoma! I really enjoy using vgrep.

50 does silence the warning for me but the problem that, when I view a result, the file is always opened at the top, remains.

fmthoma commented 5 years ago

@sjakobi Unfortunately, I cannot reproduce the problem with the file being always opened at the top.

What vgrep basically does is call $EDITOR +n file, where file is the file to be opened and n is the line number. Everything from there is handled by the editor. Could you try vim +5 <some file> on your machine and check that the file is opened on line 5?

sjakobi commented 5 years ago

vim seems to work alright. I should point out that the problem only comes up with git vgrep. vgrep on its own opens files on the right line.

I thought that the problem might be related to the non-standard shell I'm using, fish, but it also happens in bash.

sjakobi commented 5 years ago

Oh I think I've found a solution. Adding a -n for line numbers to the git vgrep alias does the trick:

    vgrep = "!__git_vgrep () { git grep -n --color=always \"$@\" | vgrep; }; __git_vgrep"