Adding Numbers

0 views
Skip to first unread message

Phillip Bruce

unread,
Jan 2, 2010, 6:42:05 AM1/2/10
to vim...@googlegroups.com
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.

--
Phillip

Christian Brabandt

unread,
Jan 2, 2010, 8:05:33 AM1/2/10
to vim...@googlegroups.com
Hi Phillip!

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

Tim Chase

unread,
Jan 2, 2010, 8:42:33 AM1/2/10
to vim...@googlegroups.com
> 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

John Little

unread,
Jan 2, 2010, 11:00:22 PM1/2/10
to vim_use

> 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


Paul

unread,
Jan 3, 2010, 6:02:12 PM1/3/10
to vim...@googlegroups.com
On Sat, Jan 02, 2010 at 03:42:05AM -0800, Phillip Bruce wrote:
>I have questions about getting vi to add numbers in sequence within a file.

Have a look at the visincr plugin:

http://www.vim.org/scripts/script.php?script_id=670

--

.

Matt Wozniski

unread,
Jan 4, 2010, 12:08:54 PM1/4/10
to vim...@googlegroups.com
On Sat, Jan 2, 2010 at 8:05 AM, Christian Brabandt wrote:
> 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.
>>
>> 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.
>
> 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)

This does the trick in one line, and should work as far back as 7.2.000:

:call append('$', map(range(100), 'printf("dummy%02d", v:val)'))

~Matt

Christian Brabandt

unread,
Jan 5, 2010, 8:39:18 AM1/5/10
to vim...@googlegroups.com
Hi Matt!

On Mo, 04 Jan 2010, Matt Wozniski wrote:
> This does the trick in one line, and should work as far back as
> 7.2.000:
>
> :call append('$', map(range(100), 'printf("dummy%02d", v:val)'))

True and so easy!

regards,
Christian
--
:wq

Reply all
Reply to author
Forward
0 new messages