Saturday, January 2, 2010

Re: Adding Numbers

> I have questions about getting vi to add numbers in sequence within a file.

Tim suggested:

:%s/$/\=printf('%04d', line('.'))
:'<,'>s/$/\=printf('%04d', 1+line('.') - line("'<"))

If your targets are not one to a line in sequence, try

:let n = 1
:g/whatever/s//\=n/|let n+=1

This idiom is very flexible and easy to remember its bare bones.
Something more like Tim's answer would be

:let n = 1
:g/dummy/s//\=submatch(0).printf('%04d',n)/|let n +=1

for "dummy" anywhere in text, though it gets tricky with more than one
"dummy" on a line.

Regards, John


--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment