I have an HTML table with rows like this:

<tr> <td>DATE</td> <td>NUMBER</td> <td>DESCRIPTION</td> <td>DEADLINE</td> </tr>

<tr> <td>6/7</td> <td>1</td> <td>foo1</td> <td>bar1</td> </tr>

<tr> <td>6/15</td> <td>2</td> <td>foo2</td> <td>bar2</td> </tr>

<tr> <td>6/20</td> <td>3</td> <td>foo3</td> <td>bar3</td> </tr>

... etc ...

What I'd like to do is I'd like to shift all of the entries in the DESCRIPTION column down. So I'd like to move foo1 one step down, I'd like to move foo2 one step down, etc. But I would like to preserve everything else in the table. How can I do this in vim?


What I understand is that you want the table to look like that, with a weird empty cell under DESCRIPTION:

<tr> <td>DATE</td> <td>NUMBER</td> <td>DESCRIPTION</td> <td>DEADLINE</td> </tr>
<tr> <td>6/7</td> <td>1</td> <td></td> <td>bar1</td> </tr>
<tr> <td>6/15</td> <td>2</td> <td>foo1</td> <td>bar2</td> </tr>
<tr> <td>6/20</td> <td>3</td> <td>foo2</td> <td>bar3</td> </tr>

Am I right? Here is one way you can do it:

  1. Format this code with :%!column -t or a Vim plugin:

    <tr>  <td>DATE</td>  <td>NUMBER</td>  <td>DESCRIPTION</td>  <td>DEADLINE</td>  </tr>
    <tr>  <td>6/7</td>   <td>1</td>       <td>foo1</td>         <td>bar1</td>      </tr>
    <tr>  <td>6/15</td>  <td>2</td>       <td>foo2</td>         <td>bar2</td>      </tr>
    <tr>  <td>6/20</td>  <td>3</td>       <td>foo3</td>         <td>bar3</td>      </tr>
  2. Move to first item then delete it with dit. foo1 is now in the default register.

  3. Start a macro with qx (use any available letter instead of x).

  4. Move to the line below with j.

  5. Visually select the content of the tag with vit.

  6. Replace the selected text (foo2) with the content of the default register (foo1) using p.

  7. Stop the macro with q. The content of the default register is now foo2, ready to use for the next time the macro is invoked.

  8. Apply the macro as many times you need with something like <number of rows>@x.

End result:

<tr>  <td>DATE</td>  <td>NUMBER</td>  <td>DESCRIPTION</td>  <td>DEADLINE</td>  </tr>
<tr>  <td>6/7</td>   <td>1</td>       <td></td>         <td>bar1</td>      </tr>
<tr>  <td>6/15</td>  <td>2</td>       <td>foo1</td>         <td>bar2</td>      </tr>
<tr>  <td>6/20</td>  <td>3</td>       <td>foo2</td>         <td>bar3</td>      </tr>

Condensed version:

:%!column -t<CR>

Use another named register at step 2 if you don't want to mess with the default register (and your system clipboard if applicable).

If you only want to increment the number in foo2 and Co, The VisIncr plugin is very good.


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.