Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #569 - Insert line numbers with a Perl filter

Created: September 30, 2003 19:55 Complexity: basic Author: mosh--AT--cs.albany.edu Version: 6.0 Karma: 8/2 Imported from: Tip#569

" Create a menu item to call perl on the file

" Edit the -e "script" before pressing return to filter thru perl

" Script below works shell=sh, and add line numbers to the file.


amenu Mo1.Format.NumberLines<TAB>:!perl :1,$!perl -ne \"printf(\\"\%3d:\%s\\",\$.,\$_);\"


"

" http://www.cs.albany.edu/~mosh Mohsin

Comments

If you don't have access to Perl, you can do it directly from VIM by calling this function:

function! Listing()

" set line numbers in front of lines 
let i=line("$") 
let pre = ' ' 
while (i > 0) 
if match(i, '^9*$') == 0 
let pre = pre . '0' 
endif 
call setline(i, pre . i . "\t" . getline(i)) 
let i=i-1 
endwhile 

endfunction


- Siegfried

Siegfried.Bublitz--AT--c-lab.de , October 1, 2003 0:13


Why not filter through UNIX cat command ? In vim you could simply type:

%!cat -n

and you got line-numbers for each line of the file.

Best regards, Dietmar

vim--AT--crystal.dream.at , September 26, 2004 19:30


set number or :set nu

cookiemonster_at-chipsahoy_dot-com , September 16, 2005 15:32


Advertisement