2

I want to generate sequential file names that take the last 2+ digits from current buffer's name and count upwards from there. Like this: 08a01 > 08a02 > 08a03 > ....

The snippet I use (thanks for initial advice, Ingo Karkat!) leaves out the zeros, producing sequences like 08a01 > 08a2 > 08a3 > ....

if b:current_buffer_name =~ '\d\+$'
    let lastDigit = matchstr(b:current_buffer_name, '\d\+$')
    let newDigit = lastDigit + 1
    let s:new_file_name = substitute(b:current_buffer_name, '\d\+$', newDigit, '')
else
    let s:new_file_name = b:current_buffer_name . '01'

How can I tell Vim in a function that it should count upwards "with zeros"? I tried adding let &nrformats-=octal before the if-condition (as suggested here), but that didn't work.

Thanks for any explanations!

Community
  • 1
  • 1
martz
  • 839
  • 6
  • 21

2 Answers2

3

try this:

change this line:

let newDigit = lastDigit + 1

into:

let newDigit = printf("%02d", str2nr(lastDigit) + 1)

didn't test, but by reading your codes, it should work.

it hardcoded 2, if your string was foobar0000001, it won't work. In this case, you need get the len(lastDigit) and use it in the printf format.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • I was trying this solution but althought I remove `octal` value from `nrformats` option, it does the calculation using it. Try with a filename like `foobar004562`. – Birei Dec 11 '13 at 23:13
  • @Birei oh, thx, I neglected the `octal` thing! adding a `str2nr()` will fix it. answer updated. – Kent Dec 11 '13 at 23:24
  • 1
    btw, the option is for `c-a/x`, won't influence the `+` expression. @Birei – Kent Dec 11 '13 at 23:34
  • Yes. I was reading that section of the docs. It's a little confusing. I was expecting to work on every operations with numbers, but hey, we will have to deal with it :-) – Birei Dec 11 '13 at 23:42
  • @Kent works as expected, thanks very much, also for the comments. I have to look more into Vim's builtin functions (it feels "obvious" to find a `str2nr` somewhere in vimdocs now. :-). – martz Dec 12 '13 at 08:00
1

I don't know how to avoid doing the sum without vim taking into account that the number is not octal with leading zeros. I tried with set nrformats-=octal but neither it worked. Here is my workaround extracting the number in two parts, zeroes by one side and the other digits from leading zeros by the other side and calculate its length using printf():

let last_digits = matchlist(bufname('%'), '\(0\+\)\?\(\d\+\)$')
echo printf('%0' . (len(last_digits[1]) + len(last_digits[2])) . 'd', last_digits[2] + 1)

Some tests:

With a buffer named 08a004562, last_digits will be a list like:

['004562', '00', '4562', '', '', '', '', '', '', '']

and the result will be:

004563

And with a buffer named 8a9, last_digits will be:

['9', '', '9', '', '', '', '', '', '', '']

and the result:

10
Birei
  • 35,723
  • 2
  • 77
  • 82
  • Thanks! I will use @Kent's solution as it is shorter. But yours is educational since I initially tried to figure out a similar workaround myself (extracting zeroes and other digits separately). – martz Dec 12 '13 at 08:09