What is the easiest way in Vim to go from:




For a list of arbitrary length?


You can add <table> and </table> manually: ggO<table>[ESC]YGpa/[ESC] Where [ESC] is the top-left key on your keyboard +)

  1. gg - go to the top of the file
  2. O - add new line above the current
  3. <table> - write <table>
  4. [ESC] - back to the command mode
  5. Y - yank current line
  6. G - go to the end of the file
  7. p - paste yanked line below the current
  8. a - begin insert mode and start inserting after current symbol
  9. / - add / symbol
  10. [ESC] - back to the command line

Now u can record a short vim macros to repeat another set of command multiple times:

:2[ENTER]qqO<tr>[ESC][DOWN]I<td>[ESC]A</td>[ESC][DOWN] I<td>[ESC]A</td>[ESC]o</tr>[ESC][DOWN]q

yep, now u can just repeat @q multiple times...(e.g. 11@q)

  1. :2 - go to the second line in the file
  2. qq - start recording macros in 'q' register
  3. O - add new line above the current
  4. <tr> - add <tr>
  5. [ESC][DOWN] - you know =)
  6. I - go to the begining of the line and start the insert mode
  7. <td> - write <td>
  8. [ESC] - ...
  9. A - go to the line end and start the insert mode
  10. </td> - ...
  11. [ESC][DOWN]
  12. repeat 6-10
  13. [ESC]
  14. o - add line below current and start the insert mode
  15. </tr> - ...
  16. [ESC][DOWN]q - end macros recording
  • im accepting this, though the full regex solution would be interesting as well – Sam Saffron Feb 15 '10 at 9:19

May be you are looking for zen-coding for vim.

  • +1, might not be exactly what the OP is looking for, but this looks awesome! – technomalogical Feb 15 '10 at 14:41
  • +1 zen coding suits this job . it'll increase your speed in coding :) – Dzung Nguyen Feb 19 '10 at 3:20

I know you're probably thinking of some clever solution using macros, but I'd let my fingers do the walking (since you're not likely to have an HTML table hundreds of lines long):

For each line of the file, replace the beginning of line with <td> And for each line, replace the end of line with </td>


goto the first line and insert


Yank those lines, press down-arrow until you're at the next insertion point and press p Continue to the bottom of the file, pressing p every two lines. There's a simple kind of rhythm here that I like to use: down, down, p, down, down, p, etc.

Change the last line to


Go back to the top and change the first line to


Obviously, I haven't tried this (and I concede that macros might be easier for some)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.