It comes up from time to time that I want to number a section of
a file I'm editing in Vim. There are actually lots of solutions
that I've found more or less useful, but usually a bit less.
They worked, but I always felt I had to reformat stuff after.
I did some Googling and I have a result I'm pretty happy with now
based on one from the Vim tips wiki. I thought you might like to
see it (and yes I'm sure that Emacs has something for this
already).
----- lines from my .vimrc
" The -range option tells that our command takes a line range and puts them
into <line1> and <line2>
:command! -range -nargs=0 NumberLines call NumberLines(<line1>,<line2>,1)
:command! -range -nargs=1 NumberLinesFrom call
NumberLines(<line1>,<line2>,<args>)
function NumberLines(l1, l2, start)
let ln1 = a:l1
let ln2 = a:l2
let linetoinsert = a:start
while ln1 <= ln2
let currLine = getline(ln1)
let currLine = substitute(currLine, "^[0-9]*\. ", "", "" )
let newLine = linetoinsert.". ".currLine
let linetoinsert = linetoinsert + 1
call setline(ln1, newLine)
let ln1=ln1+1
endwhile
endfunction
-----
You can use the function with a colon command on a region, like so
:%NumberLines
or
:%NumberLinesFrom 30
The way I usually like to use the commands, though, is to use the
line-by-line visual mode to select a region and then give the
colon command on the selected region.
I think my solution is a bit nicer than the ones I found on the
internet, because it will first remove any existing line numbers
and then insert new ones (so you can rearrange text and renumber
it easily).
Thought you might like it.
--
Don Bindner <[EMAIL PROTECTED]>
-----------------------------------------------------------------
To get off this list, send email to [EMAIL PROTECTED]
with Subject: unsubscribe
-----------------------------------------------------------------