Vim Tips Wiki
Advertisement

One might have lots of blocks of text that are clearly seperated but need to have their order reversed, as in this example:

<div class="myblock">
    ... html for first block, multiple lines ...
</div>
<div class="myblock">
    ... html for second block, multiple lines ...
</div>
<div class="myblock">
    ... html for third block, multiple lines ...
</div>

To solve this problem observe that the first block doesn't need to actually be moved. Go to the end of it (in this case the </div>) and type ma to set a mark. Start recording a macro with qa, and select your next block with jV/<\/div><CR> (in this example). :m0<CR> will then move the block to the top of the file. Complete the macro by returning to the mark with 'aq. Now you should be able to run the macro with @a (and then @@ for each subsequent run) for each block you need to move.

For the impatient, move to the last line of your block and type maqajV/<\/div><CR>:m0<CR>'aq@a@@@@@@@@; keep doing @@ until the file is reordered appropriately.

Note that this won't work for nested <div> tags (in this example). The matchit.vim extension may allow you to use % instead.

Credits: spiiph, eueu and seanw (myself) in #vim on freenode

Advertisement