Vim - incremental numbering via regular expression search and replace

前端 未结 3 1071
太阳男子
太阳男子 2020-12-11 14:13

I have this code:

array (\'id\' => 1, \'name\' => \"Murka\", \'date_of_birth\' => \"2014-10-31\", \"breed_id\" => 1),
array (\'id\' => 1, \'na         


        
相关标签:
3条回答
  • 2020-12-11 14:20

    Another approach. go to the second line, put the cursor over the first 1, now start a visual block selection Ctrl-v, press j until the line you want increment and press g Ctrl-a

    0 讨论(0)
  • 2020-12-11 14:34

    A really easy solution:

    :%norm f1s^R=line('.')^M
    

    Obtained like this:

    :%norm f1s<C-v><C-r>=line('.')<C-v><CR>
    

    If you are not comfortable with typing complete macros on the command-line you can achieve the same result via recording:

    qq
    f1s<C-r>=line('.')<CR>
    q
    [range]@q
    
    0 讨论(0)
  • 2020-12-11 14:40

    I don't have an answer to your general question, but I do have one for your specific situation. You can make your command work by putting the comma into a positive look-ahead, like this:

    :let i=1 | g/1(\,\)\@=/ s//\=i/ | let i+=1
    

    Now it will only replace the 1.

    0 讨论(0)
提交回复
热议问题