Adding line number by applying an external filter

There are occasions I want to add line numbers to my files inside vim (certainly you can turn on numbers by vim with set number, but that does not write to the file), below is a filter script to do the trick.

  %cat add_number.pl
  #! /usr/bin/perl
  my $i = 1;
  while (<>) {
    printf "%3d: %s", $i++, $_;
  } 

Enable in vim

set equalprg=add_number.pl

Select the block of code or the entire piece of Code (gg=G) then press = to apply the filter. You can come up with any filter per your desire to do all kinds of crazy things. Handy!

^ Top of Page