Search code examples
regexvimvim-macros

incrementing a register n times skips values


I've been playing with 500 individual files using VIM to make one of the values unique. I won't paste that as it's a total kludge :)

After making one element Unique I have a single file with 500 records separated by a blank line.

Recorded a macro to mark row 1 then find the blank and mark the previous line then write that block to a file with @b incremented by 1.

Because I couldn't work out how to increment @a during the writing of the file I paste it increment it then yank it back to @a .

   "ap0:let @a="""^MAywdd

This all worked fine except every 7 numbers the increment command skips three so instead of XML0001.xml to XML0500.XML I get XML001.xml to XML0763.xml or thereabouts.

Why does the skip 3 numbers every 8th invocation?

I'm just curious as at the moment as long as the files names are unique it doesn't matter what they are numbered but I will eventually need to make the output names match what the value of @b should be.

EDIT Wild guess that it has something to do with the 0 to 9 registers losing track?

Is there a way to increment @a in insert mode something like

    :'a,'b w XML"Increment @A".xml 

Solution

  • The increment mystery

    As your numbers are prefixed with 0, Vim considers them to be octal, so the increment (with <C-a>, I suppose) jumps from 07 to 10. To fix that, you can remove that (today rather obscure) number format:

    :set nrformats-=octal
    

    Directly increment register

    If your register contained just the number, you could do:

    :let @a = @a + 1
    

    (Note: += doesn't work, because the register is of string type.)

    If your register contains the full filename, you have to use :help sub-replace-expression to match the first number and increment it:

    :let @a = substitute(@a, '\d\+', '\=submatch(0) + 1', '')
    

    Plugin recommendation

    My EditSimilar plugin provides a command that searches for a non-existing offset, and writes (portions of the buffer) to it:

    :file XML0000.xml   " Name the original buffer
    :9999WritePlus      " Writes XML0001.xml, XML0002.xml, etc.