xargs vim

1,791 views
Skip to first unread message

Erik Wognsen

unread,
May 28, 2009, 9:49:33 AM5/28/09
to vim...@googlegroups.com
Hi..

Why can't i pipe to xargs vim? I mean.. I can, but something is not right.

After doing,

$ ls somefile | xargs vim

and exiting, the terminal is fubar and has to be reset.

And this does the same:
$ ls somefile | sed 's/^/vim /' | bash

What's the problem?

Regards,
Erik Wognsen

Gene Kwiecinski

unread,
May 28, 2009, 12:10:42 PM5/28/09
to vim...@googlegroups.com
>Why can't i pipe to xargs vim? I mean.. I can, but something is not
right.
>After doing,
>$ ls somefile | xargs vim
>and exiting, the terminal is fubar and has to be reset.

Only to hazard a guess, but 'vim' assumes an interactive tty input (eg,
'sh -i'), ie, tied to a screen, but streaming a command might assume
batch-mode. It *might* be divorcing the process associated with 'vim'
from the tty, and leaving it zombied somehow after it's finished.

Gary Johnson

unread,
May 28, 2009, 12:12:52 PM5/28/09
to vim...@googlegroups.com

When I do that, I see this message briefly at the top of the screen:

Vim: Warning: Input is not from a terminal

The problem is that xargs runs its argument commands in an
environment in which standard input is not the user's terminal.
Here's one way around that:

ls somefile | xargs sh -c '</dev/tty vim $*'

What I usually do, though, in situations like that is to run vim
like this:

vim $(ls somefile)

That assumes, of course, that the number of files doesn't exceed
MAX_ARGS or whatever that limit is called.

Regards,
Gary


Gary Johnson

unread,
May 28, 2009, 12:50:11 PM5/28/09
to vim...@googlegroups.com
On 2009-05-28, Gene Kwiecinski wrote:

> Sorry, deleted the original email, gotta reply to something else...

You couldn't just create a new thread?

> What about just plain 'vim *' on the command line? Some reason that
> doesn't work?

That works fine for simple cases where the file names match some
pattern, but it doesn't work for cases where you want to edit the
files that result from some complicated search pipeline such as this
one:

find include src -name \*.[ch] -mtime -3 -print

Regards,
Gary


Philip Rhoades

unread,
May 28, 2009, 12:55:28 PM5/28/09
to vim...@googlegroups.com
Gary,


vim `find include src -name \*.[ch] -mtime -3 -print | xargs`

Regards,

Phil.
--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: ph...@pricom.com.au

Gene Kwiecinski

unread,
May 28, 2009, 2:14:13 PM5/28/09
to vim...@googlegroups.com
>>Sorry, deleted the original email, gotta reply to something else...
>You couldn't just create a new thread?

Mmmmmmm, no.


>>What about just plain 'vim *' on the command line? Some reason that
>>doesn't work?

>That works fine for simple cases where the file names match some
>pattern, but it doesn't work for cases where you want to edit the
>files that result from some complicated search pipeline such as this
>one:
> find include src -name \*.[ch] -mtime -3 -print

Unno, he specified 'ls somefile |...', which to me would simply return
"somefile" on its own line, to be passed along to 'xargs'. So either
just use the given filename, or something like '*' for everything in the
directory, if "somefile" [sic] would actually be a directory-name.

'foo' == file:

> ls foo
foo
> _

'foo' == directory:

> ls foo
file1
file2
file3
...
fileN
> _

I just suggested a wildcard would be easier than going the roundabout
way through 'ls' and 'xargs'.

Anton Sharonov

unread,
May 28, 2009, 2:20:05 PM5/28/09
to vim...@googlegroups.com
2009/5/28 Gary Johnson <gary...@spocom.com>:

Another way of doing this:

find include src -name \*.[ch] -mtime -3 -print | vim -

, and then with series of CTRL-W_f CTRL-W_q to examine files from
the resulting list. (I use this technique pretty much to visit
files, which are changed in my subversion working copy - "svn
status | vim -")

--
Anton

Ken Bloom

unread,
May 28, 2009, 4:17:29 PM5/28/09
to vim...@googlegroups.com
On Fri, 29 May 2009 02:55:28 +1000, Philip Rhoades wrote:

> Gary,
>
>
> On 2009-05-29 02:50, Gary Johnson wrote:
>> On 2009-05-28, Gene Kwiecinski wrote:
>>
>>> Sorry, deleted the original email, gotta reply to something else...
>>
>> You couldn't just create a new thread?
>>
>>> What about just plain 'vim *' on the command line? Some reason that
>>> doesn't work?
>>
>> That works fine for simple cases where the file names match some
>> pattern, but it doesn't work for cases where you want to edit the files
>> that result from some complicated search pipeline such as this one:
>>
> > find include src -name \*.[ch] -mtime -3 -print
>
>
> vim `find include src -name \*.[ch] -mtime -3 -print | xargs`
>
> Regards,
>
> Phil.

Piping through xargs is a no-op in this case.

vim `find include src -name \*.[ch] -mtime 3 -print`

is what you want

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Erik Wognsen

unread,
May 29, 2009, 10:36:34 AM5/29/09
to vim...@googlegroups.com
>    ls somefile | xargs sh -c '</dev/tty vim $*'
>
>    vim $(ls somefile)

These two are very nice.. thanks!

Using it with find was exactly what I wanted.

Kind regards,
Erik

Reply all
Reply to author
Forward
0 new messages