There are two possible ways to do this without using external tools: macro or Vim-script. In my opinion, the first method is a bit cumbersome (and probably not as effective as the solution given below).
The second method can be implemented as follows (put the code in your .vimrc or send it in another way):
function! NumberLines(format) range let lfmt = (empty(a:format) ? 'N%04d' : a:format[0]) . ' %s' for lnum in range(a:firstline, a:lastline) call setline(lnum, printf(lfmt, lnum, getline(lnum))) endfor endfunction command! -range=% -nargs=? NumberLines <line1>,<line2>call NumberLines([<f-args>])
Function
NumberLines lists all the lines of the file in the specified range and adds to each line its number in accordance with the specified printf-format (default is "N% 04d"). To simplify the use of this function, I am creating a new command here that accepts a range of lines for processing (by default, the entire file) and an optional format for the line number argument.
source share