ASIC/FPGA Design and Verification Out Source Services
VIM running a shell command on a block
- This pages show how to mark a block in vim and execute a shell command on it.
- First you need to mark the block in your code.It is done by putting tags: mt on the top and mb on the bottom.
- Second run the shell command::'t,'b !sort:'t,'b !uniq
- The example, which I selected, sorts the text and removes multiple entries by executing the sort followed uniq shell commands.
-
Another example, which may be very handy. At work I often need to number vectors in an increment form. So I easily create the following lines:
signal sig_0 : std_logic;
signal sig_0 : std_logic;
signal sig_0 : std_logic;
signal sig_0 : std_logic;
signal sig_0 : std_logic;
signal sig_0 : std_logic;
signal sig_0 : std_logic;
signal sig_0 : std_logic;
begin
Next I mark my area of work and fire the following perl script on that block:
:'t,'b !perl vi_inc.pl
signal sig_0 : std_logic;
signal sig_1 : std_logic;
signal sig_2 : std_logic;
signal sig_3 : std_logic;
signal sig_4 : std_logic;
signal sig_5 : std_logic;
signal sig_6 : std_logic;
signal sig_7 : std_logic;
begin
The script can be easily improved do other functions as well. The increment script is shown below:
- #!/bin/perl
- $cnt=0;
- while (<STDIN>) {
- if($_ =~ /([^\d]*)([0-9]*)(.*)/) {
- $a=$1; $b=$3;
- $out=$a . $cnt . $b;
- print("$out\n");
- $cnt++;
- }
- }#while
Note that for simple renumbering, one can use cat command:
:'a,'b !cat -n
In this case the block of text, which is marked by tags a and b, is numbered between 1 to N.
A complex example for scripting from within vim is shown at the following page.
To remove all lines from a file except for ones, which start with the word FILE:
:'a,'b !grep "^FILE"
For simple increment / decrement operation a MACRO can be created using:
^a and ^x for increment / decrement.
Contact me now at: |