Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

making my script work with vi

by Plankton (Vicar)
on Jul 15, 2004 at 17:56 UTC ( #374750=perlquestion: print w/replies, xml ) Need Help??
Plankton has asked for the wisdom of the Perl Monks concerning the following question:


I am trying to write a simple script that I could use while editting files with vi. One of the things I like about vi is that you can do things like :1,$ !program. Here's a demostration of what I want to be able to do ...
bash-2.05b$ vi junk NOTES doc backup notes scripts cpio_out bashrc ~ ~ ~ :1,$ !sort backup bashrc cpio_out doc notes NOTES scripts :wq
In the above example I used sort to modify my file. I want to be able to do this with this script ...
bash-2.05b$ cat twc #!/usr/bin/perl -w use strict; my $file = shift; if ( !defined( $file ) ) { print "usage: $0 <dir|file>\n"; exit 0; } if ( ! -e $file ) { print "#$file\n"; } else { print "$file\n"; }
... but when I do something like this ...
bash-2.05b$ vi junk backup bashrc cpio_out doc notes NOTES scripts ~ ~ ~ :1,$ !./twc
... I get this ...
usage: ./twc <dir|file>
How should I be handling the input in my Perl script?

Plankton: 1% Evil, 99% Hot Gas.

Replies are listed 'Best First'.
Re: making my script work with vi
by Paladin (Priest) on Jul 15, 2004 at 18:13 UTC
    When you use ! with vi, it sends the text to the program on STDIN, and replaces it with whatever the program outputs on STDOUT. Try something like this:
    #!/usr/bin/perl -w use strict; while (<STDIN>) { chomp; if ( ! -e ) { print "#$_\n"; } else { print "$_\n"; } }
Re: making my script work with vi
by gmax (Abbot) on Jul 15, 2004 at 18:18 UTC

    As you can learn from your example with sort, the external command takes your lines from the standard input.

    Change your script this way:

    #!/usr/bin/perl -w use strict; while (<>) { chomp; print "file is [$_]\n"; if ( ! -e $_ ) { print "#$_\n"; } else { print "$_\n"; } }

    However, if your Vim has direct Perl support, you can execute something like this:

    :1,$ perldo s/^/#/ unless -e $_

    See Editing features for advanced users for an explanation and more examples on similar features.

     _  _ _  _  
    (_|| | |(_|><
Re: making my script work with vi
by etcshadow (Priest) on Jul 15, 2004 at 19:59 UTC
    STDIN -> STDOUT, just as the others said... You might consider just putting a #!/usr/bin/perl -p at the beginning of your program and thus skipping the whole while (<STDIN>) { ... } loop.

    Also, you can say :%!program to mean the whole file should be filtered, instead of :1,$!program. Just a couple fewer keystrokes is all.

    ------------ :Wq Not an editor command: Wq
Re: making my script work with vi
by dave_the_m (Prior) on Jul 15, 2004 at 18:15 UTC
    Your script should be getting its input from STDIN and writing its output to STDOUT. vi will not supply your script with any command-line parameters of its own accord.


Re: making my script work with vi
by NovMonk (Chaplain) on Jul 15, 2004 at 18:22 UTC
    I asked a similar question once-- maybe the answers here might give you some more ideas. Good Luck.



Re: making my script work with vi
by ninja_byte (Acolyte) on Jul 19, 2004 at 21:44 UTC
    Disclaimer: I'm kinda new at this. Comment: Maybe this is going on because your script is expecting a filename as a command line argument. See if the script works while reading from STDIN instead/as well ..?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://374750]
Approved by sleepingsquirrel
Front-paged by cchampion
[james28909]: i have discovered hash of arrays
[james28909]: @{$hash{$key}}$_;

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2017-04-27 19:19 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (512 votes). Check out past polls.