http://qs1969.pair.com?node_id=430654


in reply to How do I create a simple, perl-based text filter for Vim

It works for me with vim version 6.1.320 and the code you posted.

I saved the code into a file named 'filter', set the file to be executable, and called it with the relative path on the command line. Here's the command I used in vim:

:1,$!./filter
The command ':!program' just runs an external program, it doesn't filter. You have to provide a range either at the colon prompt, by visual selection, or by motion selection. When I used just the ':!./filter' command as you suggest, it waited for me to type the input to the filter program, and to send it an end-of-file character.

If you can't make the program executable, then you may need to use ':!perl program' instead. That works for me, too.

This is really a vim question from what I can tell, as the program you provided works for me the same as any other. However, your node mentions Windows, and I don't have any form of vi on any of my Windows machines at the moment. There's no way for me to tell if there's something strange going on with a platform issue here.



Christopher E. Stith

Replies are listed 'Best First'.
Re^2: How do I create a simple, perl-based text filter for Vim
by dimar (Curate) on Feb 14, 2005 at 01:15 UTC
    mr_mischief,

    Thanks for your reply, which confirms a nagging suspicion: (this is really a problem with Windows and STDIN, not perl or Vim).

    You have to provide a range either at the colon prompt, by visual selection, or by motion selection.

    Understood, the method used to provide the range (visual selection in the instant case) was intentionally edited out of the original post for simplicity sake. Moreover, that does not seem to be at issue since the 'sort' (binary) variant worked as expected, while the 'myfilter.pl' variant did not work (all other things held constant) ceteris paribus. If the range were not properly specified, both variants should have failed.

    your node mentions Windows, and I don't have any form of vi on any of my Windows machines at the moment.

    Windows appears to be the culprit here. There were some problems on Linux but those were traced to script permissions. Moreover, none of the other suggestions in this thread (so far) have worked on Windows ... all produce blank output where the stuff from STDIN should be ... the test tags appear, but not the 'filtered' text.

      I'm not sure if it would be a Windows issue or a porting issue of vim, and I'd be interested to know if any of these suggestions work:

      • as someone already mentioned, try using just <> instead of <STDIN>, which may work better with the C library on Windows, although I'm not sure why it would
      • Try using read() in a loop instead of the bracket operator.
      • Try playing with buffering. It shouldn't make a difference, of course, since you're getting both what's before the text and after it in the same print(). I'd consider it an unknown here, just because the results seem so bizarre.
      • Try some simple debugging on the program, even though the source is working. Make sure your perl gets the variable filled when the script is run from the command line. You've probably already done this, but I can see how it'd be overlooked for such a simple project.

      If all else fails, remember that from within vim you can edit a second file, save it, etc. You could yank the needed text, ':new temp' to edit another file, put (paste) the text there, ':w' to write the file, ':!./filter temp' to change the temp file on disk, yank the changed version from that window, quit the new window, reselect the portion in the original file, and paste the buffer over the original section. Since vim has a macro recording function, I'd record parts of that mess as macros. That may be part of what you're trying to avoid, too, though. In any case, it's probably more of a pain than you want.



      Christopher E. Stith
      (this is really a problem with Windows and STDIN, not perl or Vim)

      That was my first thought too. Perl scripts often have difficulty with pipes on Windows.

      Have you tried wrapping the script you want to use as a filter with pl2bat? A quick test here works with Vim 6.3 on Win2K.