8

Context

I use vifm as my file manager (that's off topic here, i know, but keep reading) which allows me to edit filenames inside a vim buffer.

One issue i found myself annoyed at are numbered files where I want to keep leading zeros (e.g. movie0001.webm). I can't get vim to format them properly.

The question

I have a vim buffer containing:

mov70.webm
mov71.webm
mov72.webm
mov73.webm
mov74.webm
mov75.webm
mov76.webm
mov77.webm
mov78.webm
mov79.webm

And my cursor starts at the m in the first line. I then do VG to mark everything and 69<C-x> to transform it into:

mov1.webm
mov2.webm
mov3.webm
mov4.webm
mov5.webm
mov6.webm
mov7.webm
mov8.webm
mov9.webm
mov10.webm

But i would really like to reach this instead:

mov01.webm
mov02.webm
mov03.webm
mov04.webm
mov05.webm
mov06.webm
mov07.webm
mov08.webm
mov09.webm
mov10.webm

What i have tried

The problem with numbers with leading zeros is that vim will treat them as octal, therefore I changed nrformats to an empty string:

:set nrformats=

Yet, that did not help. I have searched around in :h nrformats without finding anything useful.

  • 3
    Sounds more like a job for :subsitute + printf – muru Jun 24 '16 at 22:21
6

This can be done in two relatively simple steps:

  1. Decrement the lines with ctrl-x
  2. Run a substituion on the changed lines to add the leading zeros: '[,']s/\d\@<!\d\>/0\0/

You could turn that into a command/function if you think you'll need to do this often.

Another way is to do as muru mentioned and use substitute + printf, which can preserve the string length of the replaced digits:

'<,'>s/\d\+/\=printf('%0*d', len(submatch(0)), submatch(0) - 69)

This could also be turned into a reusable command:

command! -nargs=1 -range Decr <line1>,<line2>s/\d\+/\=printf('%0*d', len(submatch(0)), submatch(0) - <args>)

Which you can run with :'<,'>Decr 69 to decrement the visual lines by 69.

  • I'm still learning viscript but len(submatch(0)) is a great trick. Added it to my vimrc, and also added a Incr command. – grochmal Jun 25 '16 at 23:59
7

While I'd definitely go with :s + printf for complex replacements, I can get the effect you desire if I start from 00, and have set nrformats-=octal. That is:

  1. Select the numbers in a visual block:

    enter image description here

    Note that I have added mov76.webm - you don't actually have ten files in your example list.

  2. Replace with zeros and select the same region again: r0gv

    enter image description here

  3. Use g<c-a>:

    enter image description here

Without set nrformats-=octal, you'll see 010, 011, etc.

  • Your 0s look like eyes. Creepy. – romainl Jun 25 '16 at 14:06
  • Ooops, that was a mistake with the mov76.webm, i used a bad directory. I edited the question. Yay, for the andale font. – grochmal Jun 25 '16 at 14:31
  • @grochmal That's the Ubuntu Mono font. :D – muru Jun 25 '16 at 14:35
  • @romainl and now I can't unsee it. – muru Jun 25 '16 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.