Saturday, January 2, 2010

Re: Adding Numbers

> I have questions about getting vi to add numbers in sequence
> within a file.
>
> Example:
>
> dummy01
> dummy02
> dummy03
> dummy
> dummy
> ....
>
> etc.
>
> So If I have 1000 dummy's and instead of hand typing all this
> in a file. I wonder if there was an easy to accomplish this
> using vim.

For the whole file, you can do this:

:%s/$/\=printf('%04d', line('.'))

or, for a sub-range of the file

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

Adjust the format string for the required padding.

-tim

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

No comments:

Post a Comment