V

 

ASIC/FPGA Design and Verification Out Source Services

Run a perl script from vim on a block, to do complex text operations.

  1. This page shows how to create a perl script, run it from within vim on a block of data to perform a complex text manipulation.
  2. In this site this issue was already talked, as a side topic.
    Today I had a list of more than 500 lines and I needed to extract all even lines into one specman list and the odd into another.

  3. This was too much to do manually. A perl script did the job with as much as one iteration with relatively low effort. It saved me a lot of annoying keystroke typing and time, that I decided to dedicate a page for this nice vim feature on my web site.

    The input was of this form:
            0b0010111001
            0b0010101110
    ...
            0b0001011101

  4. The output has to be in the following format:

    var negative_code_l : list of uint(bits:10) = {
        0b0010111001;
        0b0010101110;
        ...
        0b0001011101;
        0b0001011110
    };
    var positive_code_l : list of uint(bits:10) = {
        0b1101000110;
        0b1101010001;
        ...
        0b1110100010;
        0b1110100001
    };


  5. The perl script has to use STDIN, to process data, which is piped from vim. It also has to buffer the data arriving from vim into two buffers: even and odd.
    It than outputs the content of the buffers into the desired syntax. The script is shown below:

    1. #!/bin/perl

    2. $cnt=0;
    3. while (<STDIN>) {
    4.     chomp($_);
    5.     if($_ =~ /^ *(0b[0-9]*)/) {
    6.         $buf=$1;
    7.         if( ($cnt%2) == 0 ) {
    8.             push(@n_a, $buf);
    9.         } else {
    10.             push(@p_a, $buf);
    11.         }
    12.     }
    13.     $cnt++;
    14. }#while
    15. print("    negative_code_l : list of uint(bits:10) = {\n");
    16. foreach $j ( @n_a ) {
    17.     print(" $j;\n");
    18. }
    19. print("    };\n");
    20. print("    positive_code_l : list of uint(bits:10) = {\n");
    21. foreach $j ( @p_a ) {
    22.   print("      $j;\n");
    23. }
    24. print("    };\n");


    To execute the script form within vim, type, after putting tags at the start and stop, to mark the block of text, that vim passes to the perl script:
    :'a,'b !perl ~/bin/create_10Bpn_lists.pl Block can be used relative to a single tab:
    :'a+2,. !perl ~/bin/create_10Bpn_lists.pl
    From current cursor to first empty line:
    :.,/^$/-1!perl ~/bin/create_10Bpn_lists.pl


  6. You may want to see this simple perl script, that puts an incremental value into your text. Again this is done by calling a script from within vim: simple counter perl script
 


Also available on this project:


Home

Some tips for controlling the xterm title


change the default color scheme of gvimdiff







Search This Site


Feedback This Site




new pages on this site