What is your favorite/most productive macro that you have used in Vim?

I'm trying to know how vim experts use macro's in vim for day-to-day coding - I have a lot of custom shortcuts/mappings that I use frequently , but haven't come across any good ideas of macros.

It may have been a macro which you had used before , to simplify a task tremendously - I just need ideas as to how to productively use this feature in vim !


Macros are generally an "on the fly" automation tool. Lets say you have a list of items:

apple
orange
banana
grapes
cantaloupe

You can quickly write a macro to make them into an html list for example. Starting from the beginning of the word apple record the following macro: i<li><Esc>A</li><Esc><CR> into register 1.

The <CR> is key because it allows you to jump to the next line and thus use the "count" modifier on your macro execution which provides multiline macro execution

Then go to the beginning of orange and use 4@1 ([count]@[register letter]) to apply the macro to the rest of the entries to get this:

<li>apple</li>
<li>orange</li>
<li>banana</li>
<li>grapes</li>
<li>cantaloupe</li> 

Imagine using that on a list of 200 items. Saves time no?

Of course this example is trivial and you could do this with a search and replace but you can imagine more complex examples...


I use macros for a lot of temporary custom keystroke-saving. But it's on a case-by-case basis.

For example: Delete 4 characters, insert mode, ([]), exit insert mode, move down, back four spaces. Then use . to repeat it.


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.

    链接地址: http://www.djcxy.com/p/8696.html

    上一篇: 你对PHP开发有什么理想的vim配置?

    下一篇: 你在Vim中使用过的最喜欢/最有成效的宏是什么?