On Wed, 17 Oct 2012, stosss wrote:
> Okay I have something that should be achievable but I can't figure it
> out. I have a lot of files with data for books. Each file is a book
> and is in chapters with lines of text.
>
> sample of existing data:
>
> <h2>1</h2><p>
> 1 line of text.<br>
> 2 another line of text.<br>
> 3 lines of text.<br>
> ...
> 10 More lines of text.<br>
> ...
> 100 Even more lines of text.<br>
>
> [...]
>
> What I want is:
>
> <h2 id="b001c001">1</h2><p>
> <span id="b001c001v001">1</span> line of text.<br />
> <span id="b001c001v002">2</span> another line of text.<br />
> <span id="b001c001v003">3</span> lines of text.<br />
> ...
> <span id="b001c001v010">10</span> More lines of text.<br />
> ...
> <span id="b001c001v100">100</span> Even more lines of text.
>
> [...]
>
> I can't figure out how to use %03d to change my numbers from 1 to 001
> I also would like to figure out how to use setreg() to increase this
> c001 part of the id label so it changes to c002 when it is supposed to
> so I don't have to reset my map at every chapter. I need to figure out
> the first part before I figure out the second part. The examples in
> the VIM tips aren't helping me figure this out. Maybe I am looking in
> the wrong place.
>
> I tried all sorts of things but nothing worked. I don't remember all
> the things I tried but here are a few attempts I made that didn't
> work:
>
> :%s@\([0-9]\+\)@%03d\1@
> :%s@\([0-9]\+\)@\=printf("%03d"\1)@
> :%s@\([0-9]\+\)@\=printf("%03d\1")@
>
> I don't understand how to use that number format in my search and
> replace. The examples on the pages linked above don't give me any
> clues. It has been a very long time since I have asked for help with
> VIM. I usually have been able to find answers in the tips. But not
> this time.
For the %03d part:
:%s@\(\d\+\)@\=printf("%03d", submatch(1))@g
See:
:help submatch()
But, that treats numbers that look like octal incorrectly:
E.g.:
abc010def -> abc008def
(because 010-base-8 is 8-base-10.)
So consume the zeros ahead of time:
:%s@0\*\(\d\+\)@\=printf("%03d", submatch(1))@g
--
Best,
Ben
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Wednesday, October 17, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment