Vim - incremental numbering via regular expression search and replace

谁说胖子不能爱 提交于 2019-11-27 07:31:11

问题


I have this code:

array ('id' => 1, 'name' => "Murka", 'date_of_birth' => "2014-10-31", "breed_id" => 1),
array ('id' => 1, 'name' => "Jurka", 'date_of_birth' => "2014-11-31", "breed_id" => 2),
array ('id' => 1, 'name' => "Nyash", 'date_of_birth' => "2014-12-31", "breed_id" => 3),
array ('id' => 1, 'name' => "Meowy", 'date_of_birth' => "2014-01-31", "breed_id" => 4),
array ('id' => 1, 'name' => "Yummi", 'date_of_birth' => "2014-10-31", "breed_id" => 2),
array ('id' => 1, 'name' => "Barss", 'date_of_birth' => "2014-05-31", "breed_id" => 2),
array ('id' => 1, 'name' => "Nonam", 'date_of_birth' => "2014-05-31", "breed_id" => null

I'll want to change all 'id' => 1 (except the 1st) so the number will increment by 1. It's simple to achieve with Emacs:

M-x replace-regexp
\(1,\)
\,(1+ \#),

As described here. After some research the maximum that I've been able to achieve with Vim is (inspired from here):

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

But this removes all the following commas:

array ('id' => 1 'name' => "Murka", 'date_of_birth' => "2014-10-31", "breed_id" => 1),
array ('id' => 2 'name' => "Jurka", 'date_of_birth' => "2014-11-31", "breed_id" => 2),
array ('id' => 3 'name' => "Nyash", 'date_of_birth' => "2014-12-31", "breed_id" => 3),
array ('id' => 4 'name' => "Meowy", 'date_of_birth' => "2014-01-31", "breed_id" => 4),
array ('id' => 5 'name' => "Yummi", 'date_of_birth' => "2014-10-31", "breed_id" => 2),
array ('id' => 6 'name' => "Barss", 'date_of_birth' => "2014-05-31", "breed_id" => 2),
array ('id' => 7 'name' => "Nonam", 'date_of_birth' => "2014-05-31", "breed_id" => null),

So I have to fix it (I know it's easy).

I am aware of this and macros, I'm just interested to know if there any one-line command solution in Vim.

More general question: is it possible in Vim to inject some logic like conditional statements, manipulating on regex back references? The example for this in Emacs would be:

C-M-% \(^.*\)\(linear-gradient(\)\(to right\|to bottom\)\(.*$\) <RET>
\& C-q C-j
\1-prefix-\2\,(if (equal "to right" \3) "left" "top")\4

This one helped me about a year+ ago to refactor some huge scary HTML code that had a much of inline CSS.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/26681541/vim-incremental-numbering-via-regular-expression-search-and-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!