I recently used a macro when editing c++ code. I had about 300 header files and they basically had the form:
... includes and other junk
namespace foo {
namespace bar {
... some classes and other code ...
}
}
... possibly more code
And I wanted to add another namespace enclosing the the other two. The final file should look like:
... includes and other junk
namespace top {
namespace foo {
namespace bar {
... some classes and other code ...
}
}
}
... possibly more code
Very tedious and a very error prone to do with a script. But with a vim macro it was very simple. Simply open all the files that need changing then record:
- search for "namespace foo"
- insert a line above "namespace top {"
- Go back to the "namespace foo" line
- Hit '%' to find the closing brace
- Append the line '}' after it
- Reformat the code block with =a}
- Go to next file with :wn
Then you can run the macro as many times as there are files to be changed.
Personally, I find myself using recorded macros and visual block editing commands all the time to speed things up. They can be a bit tricky at first to see the problem as a repeated command over a range that can be moved around easily using vim commands. But once you get in the habit of seeing the problem in that manor there are opportunities all the time for them to save you time.