On Tue, October 20, 2009 8:55 am, Jeri Raye wrote:
> I have a long list with lines that start with a number in random order
> followed by tekst.
> Something like this:
>
> 109 AAA
> 23 blabla
> 1 xyzxyz
> 45 BBB
>
> What I want is that all number get one or more 0 needed then the
> largest (in character size) number
> So in the list above  the 'largest' number is 3 characters.
>
> 109 AAA
> 023 blabla
> 001 xyzxyz
> 045 BBB
>
> But if there was a bigger number for example 12345 in the list (not
> shown now in this example) ) the above list should become
> 00109 AAA
> 00023 blabla
> 00001 xyzxyz
> 00045 BBB
>
> How to do that?

Well, you need to calculate the max length of the first item. I am not
sure, you can do this on the fly, so I defined this function:

fu! MaxLen()
    return strlen(max(map(getline(1,"$"), 'matchstr(v:val, "^\\d\\+")')))
endfu

Using this function, I could then run a substitute command like this:
:%s/^\d\+/\=printf("%0".MaxLen()."d",submatch(0))/

Which worked for me.

PS: Anybody knows why map(getline(1,"$"), 'matchstr(v:val, "^\\d\\+")')
worked, but not map(getline(1,"$"), "matchstr(v:val, '^\d\+')"), which
would have the benefit of not having to double escape the regular
expression, thus looking clearer?

regards,
Christian
-- 
:wq


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to