开发者

Vim/other editors bulk editing

开发者 https://www.devze.com 2023-02-14 11:24 出处:网络
I\'ve got this sample list: cat dog lion And this code in which i need insert it开发者_如何学编程 to:

I've got this sample list:

cat dog lion

And this code in which i need insert it开发者_如何学编程 to:

<li><img src="images/opcje/{value}.png" alt=""/><label for="wezel">{value}</label><input type="checkbox" id="wezel" {if strstr($opcje,"-{order_number}-")} checked {/if} name="opcje[]" value="-{order_number}-"/></li>

And example output:

<li><img src="images/opcje/cat.png" alt=""/><label for="wezel">cat</label><input type="checkbox" id="wezel" {if strstr($opcje,"-1-")} checked {/if} name="opcje[]" value="-1-"/></li>

Is there any fast and DRY method for this?


You could use a macro. It would work something like the following:

  1. vim my.xml
  2. :sp to split the window
  3. C-w j to go to the bottom window
  4. :e mylist.txt

Now you have both files open. Let's make a macro!

  1. qa to record the 'a' macro
  2. dw to delete the first list item
  3. C-w k to go to the top window
  4. 1G to go to the beginning of my.xml
  5. /\{value\} and press return to find the first instance of {value}
  6. P to past before {value}
  7. wd7l to jump to {value} and delete it
  8. C-w j to jump to the bottom window
  9. q to end recording macro

Now invoke your macro several times. For a 10-item list, try:

10@a

Voila!


You can do this in vim using a macro. You would start recording your macro by pressing q followed by some key, into which the macro would be saved. For instance qa would start recording the macro into key (or register) a. Then you would do the replacing once, but you must be sure to do it in such a way that if you execute the macro afterwards it will also work for the next word in your list. Press q again to stop recording. If you recorded your macro correctly, you can then execute the macro saved in register a by pressing @a, which would do the next replacement automatically.


Perhaps a small script like the following would be more appropriate:

#bash script
i=0;
for val in {cat,dog,lion}; do 
   i=$((i+1)); 
   sed -e "s/{value}/$val/g;s/{order_number}/$i/g" your_code > new_code_$i
done
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号