Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Piping data to vim

by yoda54 (Monk)
on Nov 19, 2010 at 01:13 UTC ( [id://872399]=perlquestion: print w/replies, xml ) Need Help??

yoda54 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

I'm calling vim inside one of my perl scripts to edit various run time data (lists and such). Right now within my script I'm doing something like: data is written to a file, open with vim, save/close vim, openfile/refresh runtime data.

I'm sure there's a better approach. Is there a way pipe data directly to vim from perl? Do any of you have something like this out there?

Thanks for any suggestions.

Replies are listed 'Best First'.
Re: Piping data to vim
by roboticus (Chancellor) on Nov 19, 2010 at 03:53 UTC

    yoda54:

    You're using perl to automate vim? That just makes my head hurt. Why not just edit the text using standard perl features? I can only assume that there are some vim packages you have installed that you're trying to take advantage of.

    Anyway, if you want to automate vim from a perl script, it appears that Rolf already has you covered.

    You could also call your perl script from within vim, if that would help things. For example, I frequently call out to the sort command to sort data in a document while in vim. There's no reason you can't call a perl script to process a section of text.

    Or even uglier: you could have your perl script open a document in vim, and then tell it to execute a perl script from within vim. That'll certainly help you keep your job "interesting".

    ...roboticus

      What I need is a editor for data inside my perl script. Rather then writing my own I was thinking I could use vim instead.

      Thanks

        yoda54:

        I see. You may want to check out a few things on CPAN, then. I just took a brief look and found:

        • Proc::InvokeEditor: This module provides the ability to supply some text to an external text editor, have it edited by the user, and retrieve the results.
        • AnyEvent::EditText: This little module will start a text editor in a seperate process without stopping the current process. Usually something like a terminal with a vim instance running in it will be started, but also a graphical editor could be used (like gedit or gvim).
        • Term::CallEditor: This module calls an external editor with an optional text message via the solicit() function, then returns any data from this editor as a file handle. By default, the EDITOR environment variable will be used, otherwise vi.
        • Term::EditorEdit: Term::EditorEdit is a tool for prompting the user to edit a piece of text via $VISUAL or $EDITOR and return the result
        • Term::ReadLine::Zoid:This package provides a set of modules that form an interactive input buffer written in plain perl with minimal dependencies. It features almost all key-bindings described in the posix spec for the sh(1) utility with some extensions like multiline editing; this includes a vi-command mode with a save-buffer (for copy-pasting) and an undo-stack.

        Some of those look interesting. (There were a few pages of stuff that didn't look interesting, though...)

        ...roboticus

        The batch version of vi(1) is called sed(1). However perl is more powerful.

        Few proficient Perl programmers would choose your method, which is why you're not finding code already written to do this.

        As others have said, Perl's editing features are powerful. It seems like the reason you're looking to use vim is that your proficiency with vim is greater than with Perl. If so, the correct approach is to improve your proficiency with Perl and edit your data using Perl instead of kludging something with vim.

        If the data can be represented in a config format, there are many modules available to be used; just do a CPAN search on 'config'.

        Even if your data isn't nicely represented in a config format, it's possible that CPAN may contain a module which will solve your problem without using an external editor.

Re: Piping data to vim
by LanX (Saint) on Nov 19, 2010 at 02:01 UTC
    Vim's not my religion, but the standard approach seems to work! :)

    perl -e ' open my $vim,"|vim -"; print $vim 0..9 ; '

    Cheers Rolf

    UPDATE: I missed that you wanna catch vim's output, sorry I have no idea how to make vim write to STDOUT.

Re: Piping data to vim
by anonymized user 468275 (Curate) on Nov 19, 2010 at 08:58 UTC
    vim doesn't output the file to STDOUT, it outputs GUI instructions to talk to the presumed user. How to do it depends on whether you want to program or type the vim instructions. Either way you have to save the data to a file and then either shell out to vim (see the system function) or pipe the vim instructions terminated by "wq!\n" into vim (see the open function).

    Either way the data then has to be read back from the file, not a pipe.

    One world, one people

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://872399]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found