Input from stdin

50 views
Skip to first unread message

Elias Diem

unread,
Oct 18, 2011, 4:55:25 AM10/18/11
to v...@vim.org
Hello Vim users,

why does this work?

$ find /path -name bla | vim -

But this doesn't

$ find /path -name bla | vim

Thanks Elias

Paul

unread,
Oct 18, 2011, 5:38:37 AM10/18/11
to vim...@googlegroups.com
On Tue, Oct 18, 2011 at 10:55:25AM +0200, Elias Diem wrote:
>why does this work?
>
>$ find /path -name bla | vim -
>
>But this doesn't
>
>$ find /path -name bla | vim

Because standard input is not the same as command line arguments.

From 'man vim':

vim [options] [file ..]

and:

- The file to edit is read from stdin. Commands are read
from stderr, which should be a tty.

--

.

Elias Diem

unread,
Oct 18, 2011, 7:50:02 AM10/18/11
to v...@vim.org

Yes, but what's the difference to say for example 'less'?
If I pipe the results of 'find' to 'less' I can do:

$ find /path -name bla | less

Without giving the '-' argument to 'less'. Why doesn't this work
with vim?

Thanks Elias

C. Meissa

unread,
Oct 18, 2011, 8:01:57 AM10/18/11
to vim...@googlegroups.com
Hi,

Elias Diem schrieb am 18.10.2011 13:50:02:
> Yes, but what's the difference to say for example 'less'?
> If I pipe the results of 'find' to 'less' I can do:

For the same reason why mplayer and wget and tar use "-":
I guess it’s intentionally coded that way…

Regards
C.M.

Tim Chase

unread,
Oct 18, 2011, 8:21:50 AM10/18/11
to vim...@googlegroups.com, Elias Diem, v...@vim.org
On 10/18/11 06:50, Elias Diem wrote:
> Without giving the '-' argument to 'less'. Why doesn't this work
> with vim?

Because Vim can take piped commands:

[2]tim@bigbox:~/tmp$ seq 10 > test.txt
[3]tim@bigbox:~/tmp$ (echo ':5s/$/five'; echo ":wq") | vi test.txt
Vim: Warning: Input is not from a terminal
[4]tim@bigbox:~/tmp$ cat test.txt
1
2
3
4
5five
6
7
8
9
10

-tim

Ben Fritz

unread,
Oct 18, 2011, 10:09:30 AM10/18/11
to vim_use
WHAT?!

That is one of the coolest thing I've learned on the list in a while.

Of course, I have no idea when I'd ever use such a thing. Does it also
work for normal-mode commands? Sadly, even your simple example doesn't
seem work on the Solaris server which I use at work (I get a "Vim:
Error reading input, exiting..." message), so I cannot test it myself.

Tim Chase

unread,
Oct 18, 2011, 10:32:20 AM10/18/11
to vim...@googlegroups.com, Ben Fritz
On 10/18/11 09:09, Ben Fritz wrote:
> On Oct 18, 7:21 am, Tim Chase<v...@tim.thechases.com> wrote:
>> On 10/18/11 06:50, Elias Diem wrote:
>>> Without giving the '-' argument to 'less'. Why doesn't this work
>>> with vim?
>>
>> Because Vim can take piped commands:
>>
>> [2]tim@bigbox:~/tmp$ seq 10> test.txt
>> [3]tim@bigbox:~/tmp$ (echo ':5s/$/five'; echo ":wq") | vi test.txt
>> Vim: Warning: Input is not from a terminal
>
> WHAT?!
>
> That is one of the coolest thing I've learned on the list in a while.
>
> Of course, I have no idea when I'd ever use such a thing.

That's was sorta my reaction as well. I've never actually used it
for anything *practical* but it does allow you to do normal mode
commands too:

[2]tim@bigbox:~/tmp$ seq 10 > temp.txt
[3]tim@bigbox:!/tmp$ echo "3G>4jZZ" | vi temp.txt


Vim: Warning: Input is not from a terminal

[1493]tim@bigbox:~/tmp$ cat temp.txt
1
2
3
4
5


6
7
8
9
10


The only catch is that it's sometimes tricky to do things
involving control-characters or possibly where timing is of the
essence.

Notably, I also tried using

vim -w script.txt filea.txt

to record one vim session into a script, and then playing the
script back into vim on other files:

vim fileb.txt < script.txt

with a disappointing lack of success (in this test case,
filea.txt and fileb.txt were identical to begin with). Ah, well.

-tim

Sven Guckes

unread,
Oct 18, 2011, 11:47:01 AM10/18/11
to v...@vim.org
* Elias Diem <pub....@webconect.ch> [2011-10-18 16:55]:

> On Tue, Oct 18, 2011 at 10:38:37AM +0100, Paul wrote:
> > On Tue, Oct 18, 2011 at 10:55:25AM +0200, Elias Diem wrote:
> > >why does this work?
> > >$ find /path -name bla | vim -
> > >
> > >But this doesn't
> > >$ find /path -name bla | vim
> > Because standard input is not the same as command line arguments.
>
> Yes, but what's the difference to say for example 'less'?
> If I pipe the results of 'find' to 'less' I can do:
>
> $ find /path -name bla | less
>
> Without giving the '-' argument to 'less'.
> Why doesn't this work with vim?

with a pager you'd expect its input to come from stdin, anyway.
so it was programmed differently. all a matter of history, too.

besides, the committe that came together to regulate
all this years before less and vim were created,
could not agree on a simplification or generalization..

but if you have any ideas on how to make them consistent
with the future not-yet-existent programs - let us know! ;)

Sven

--
$ export PAGER="vim -"
$ command | $PAGER

Elias Diem

unread,
Oct 18, 2011, 3:22:39 PM10/18/11
to v...@vim.org

That's an answer, man. Thanks Tim.

Elias Diem

unread,
Oct 18, 2011, 3:37:37 PM10/18/11
to v...@vim.org

Thank you Sven too.

Reply all
Reply to author
Forward
0 new messages