views:

204

answers:

5

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 !

+3  A: 

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.

gahooa
+1  A: 

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.

ldigas
+3  A: 

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...

Pierre-Antoine LaFayette
Nice example - I guess the "$" implies Shift , so Shift-A goes to the end of line in insert mode ?Also, have you ever used a multifile macro - copying text from file 1 to file 2 and doing some specific operations on it... ?
shan23
No $ as in <Shift><4> as in go to the end of the line :) As for multifile macros, I believe you can execute a macro on all buffers (probably with :bufdo) but (AFAIK) I don't think you can create a macro that edits across multiple files.
Pierre-Antoine LaFayette
And the <Esc> means escape to command mode then use $ to jump to the end of line, then A to enter insert mode after the cursor position...
Pierre-Antoine LaFayette
But , AFAIK , `A` is the same as `<Shift><4><A>` . In command mode , `a` inserts text after the cursor position , whereas `A` inserts text at end of line !! So , the $ keystroke is redundant :)
shan23
You're right, I mistyped the macro :) Good eye.
Pierre-Antoine LaFayette
The funny thing about Vim is that you're fingers are programmed to do the keystrokes for you and you rarely realize what you're actually doing.
Pierre-Antoine LaFayette
Yep - thats why I'm forever looking for ways to define more and more keyboard shortcuts , and remove the mouse for the picture !!
shan23
A: 

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

Sam
But thats mapping keys...not the same as a macro !!
shan23
+2  A: 

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:

  1. search for "namespace foo"
  2. insert a line above "namespace top {"
  3. Go back to the "namespace foo" line
  4. Hit '%' to find the closing brace
  5. Append the line '}' after it
  6. Reformat the code block with =a}
  7. 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.

Neg_EV
Nice one - good way to span a macro across multiple files !!
shan23