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.
Macros, as gahooa already said, are more of a quick solution for a temp problem. For a more permanent approach one would define either mappings or functions for doing ... what needs to be done.
I love having my files tagged with ctags and/or cscope, so I have a key mapping for re-indexing changed files.
For example, you're editing foo.c in project bar and have made a few changes. Press (magic keystroke) and vim runs ctags -a -o foo.c
If you want to get really fancy, you can have a seperate TAGS file per project and also add a key mapping to reindex a whole project.
ctags rocks
精彩评论