Find the answer to your Linux question:
Results 1 to 3 of 3
I would like to do the following: (1) save the contents of the current line being edited in VI (and ideally also the cursor position on that line) (2) export ...
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    2

    exporting line from vi/vim to shell for use in script

    I would like to do the following:
    (1) save the contents of the current line being edited in VI (and ideally also the cursor position on that line)
    (2) export this line as input to a perl script
    (3) perform several actions on that line from the perl script

    Ideally, I would like all this to be possible by typing a single command in vi.

    Is something like this possible? If so, could someone give me some guidance on how it might be done? I know, for instance, that a command like :6,8 !lpr will send lines 6-8 to the printer. I'm imagining that an appropriate modification of such a command might be what I want, but I'm not exactly sure. Thanks.

    -a

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    You sure can. There are two ways to do this.

    Approach 1 is to use an external Perl script that you've already written. In that case, you can choose a number of lines and use the ! command to filter them through an external program. See ":help !" for more information. This is what you proposed before. So for instance, if you wanted to filter lines 6-8, you could say:
    Code:
    :6,8 !my_perl_script_that_does_something
    The second approach is to use Vim's built-in Perl scripting functionality to actually run arbitrary Perl code from within Vim. See ":help perl" (and more specifically, ":help :perldo") for more information. This does require that your Vim was compiled with Perl support.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Posts
    2
    Thank you Cabhan. Your reply was very helpful to me.

    -a

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...