To solve this problem, observe that the first block does not need to be moved. Go to the last line of the first block (in this case the <tt><nowiki></div></nowiki></tt>) then record a macro by typing:

+

To solve this problem, observe that the first block does not need to be moved. Go to the last line of the first block (in this case the <code><nowiki></div></nowiki></code>) then record a macro by typing:

−

:<tt>ma</tt> (set mark '<tt>a</tt>')

+

:<code>ma</code> (set mark '<code>a</code>')

−

:<tt>qa</tt> (start recording a macro to register <tt>a</tt>)

+

:<code>qa</code> (start recording a macro to register <code>a</code>)

−

:<tt><nowiki>jV/<\/div><CR></nowiki></tt> (select the next block – these commands work for this example)

+

:<code><nowiki>jV/<\/div><CR></nowiki></code> (select the next block – these commands work for this example)

−

:<tt>:m0<CR></tt> (move the block to the beginning of the file)

+

:<code>:m0<CR></code> (move the block to the beginning of the file)

−

:<tt>'a</tt> (return to mark '<tt>a</tt>')

+

:<code>'a</code> (return to mark '<code>a</code>')

−

:<tt>q</tt> (finish recording macro)

+

:<code>q</code> (finish recording macro)

−

Now you should be able to run the macro with <tt>@a</tt> (and then <tt>@@</tt> for each subsequent run) for each block you need to move.

+

Now you should be able to run the macro with <code>@a</code> (and then <code>@@</code> for each subsequent run) for each block you need to move.

−

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

+

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

−

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

+

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

==See also==

==See also==

Latest revision as of 06:41, July 13, 2012

It is often useful to record a macro – a series of commands that can be re-executed. This tip shows how to use a macro to move blocks of text: record the commands to move the first block, then replay the commands to move further blocks.