¿Cómo lograr el aumento con vim replace?

I want to replace the code below:

<input name="rating" id="rating">
<input name="rating" id="rating">
<input name="rating" id="rating">
<input name="rating" id="rating">
...
<input name="rating" id="rating">

a:

<input name="rating1" id="rating1">
<input name="rating2" id="rating2">
<input name="rating3" id="rating3">
<input name="rating4" id="rating4">
...
<input name="rating100" id="rating100">

¿Cómo lograr el aumento con vim replace?

¡Gracias!

preguntado el 31 de enero de 12 a las 08:01

3 Respuestas

I'd start with just one line:

<input name="rating1" id="rating1">

Entonces grabar una macro qq y eso yanks the whole line yy, pastes it below p y la increments the two numbers Ctrl-awCtrl-a. Stop recording the macro q.

Entonces run the macro tiempos 98 98@q.

But then I've spent too much time playing VimGolf!

Respondido el 31 de enero de 12 a las 12:01

Assuming it is always the current line number you want to append to the word "rating", you can use this:

:%s#rating\zs#\=line(".")#g

Respondido el 31 de enero de 12 a las 13:01

This is quite an assumption, given this is clearly HTML code and there would therefore be a <head> sección anterior. - Johnsyweb

Very nice! I just tested and it seems that you can do ...\=line(".")+10... (replace 10 with whatever your starting line number is). +1 for teaching me something new. :) - Walter

There is a simple and efficient technique for implementing counter-based substitutions in Vim. I have proposed it in mi respuesta to the question "gVim buscar / reemplazar con contador". To understand the idea, see that answer as well as my answers to related questions: "Using Vim, how do you use a variable to store count of patterns found?"Y"CSV search and replace".

Similarly, the issue described in this question can be solved by the command

:let n=[0] | %s/="rating\zs\ze"/\=map(n,'v:val+1')/g

contestado el 23 de mayo de 17 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.