Saturday, January 2, 2010

Re: Adding Numbers

Hi Phillip!

On Sa, 02 Jan 2010, Phillip Bruce wrote:

> Hi,
>
> 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.
>
> Any suggestions would be helpful.

I think, the easiest solution would facilitate a macro. Assuming you
have written dummy01 and the cursor is on the word dummy, record the
macro into register a like this:
qayyp$^Aq

(where ^A ist a literal Ctrl-A key press not the 2 keys ^ and A).

And then execute 100@a which executes the macro stored in register a.

Note, that you can also fill the register explicitly, if you know
exactly what to execute (^A ist inserted as Ctrl-V Ctrl-A):
:let @a="yyp$^A"
100@a

Another solution, which would only work with vim 7.2 with patch 295
included uses a little bit scripting:

:let list=repeat(["dummy"], 100)
:call map(list, 'v:val.printf("%02d",v:key+1)')
:call append("$", list)

regards,
Christian

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

No comments:

Post a Comment