I have a HTML code like this.
<tr>
<td>$value</td>
<td>$value</td>
<td>$value</td>
</tr>
I want to changes all $value with a value from an array new_value = ['Noodle', 'Rice', 'Pizza'] I thought it will be solved if doing some macro things, here is my first attempt.
:let new_value = ['Noodle', 'Rice', 'Pizza']
:let i = 0
qq
/$value
:s/$value/\=new_value[i]/
:let i += 1
q
But when I run this macro, it's not running smoothly.
You need to move down one line in the macro. As it currently stands you run the substitute command on the same line every time. So only the first $value is replaced. (assuming your cursor started on the first <td> line.)
:let new_value = ['Noodle', 'Rice', 'Pizza']
:let i = 0
qq
/$value
:s/$value/\=new_value[i]/
:let i += 1
jq <-- Added j
If you ran the macro as you currently have it would do the replacements properly if all the text was on one line.
Although a better solution would be this
:let new_value = ['Noodle', 'Rice', 'Pizza']
:%s/$value/\=remove(new_value, 0)/g
The first line creates a list of words and the second replaces all instances of $value with the head of the list. After this is done new_value will be empty. (assuming that the size of new_value is equal to the number of $value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With