Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What is the easiest way in Vim to go from:

hello
world
test
test2
test3
test4

to

<table>
<tr>
<td>hello</td>
<td>world</td>
</tr>
<tr>
<td>test</td>
<td>test2</td>
</tr>
<tr>
<td>test3</td>
<td>test4</td>
</tr>
</table> 

For a list of arbitrary length?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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
share|improve this answer
    
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.

share|improve this answer
    
+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 :) –  nXqd 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>

%s/^/<td>/
%s/$/<\/td>/

goto the first line and insert

</tr>
<tr>

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

</table>

Go back to the top and change the first line to

<table>

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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