Alex Rivera | Logout

Run a macro in all buffers in vim

Asked 2010-06-11T16:03:55.000
47

I know about the :bufdo command, and was trying to combine it with a macro I had recorded (@a) to add a #include in the proper spot of each of the header files I'd loaded. However, I couldn't find an easy way to run the macro on each buffer. Is there a way to execute a macro through ex mode, which is what :bufdo requires? Or is there another command I'm missing?

Edit
Report

3 Answers

58

You can do it like this:

:bufdo execute "normal @a" | write

The normal command will run the macro, but it has to be run using :execute, otherwise the pipe character will be interpreted as a normal-mode character.

answered 2010-06-11T16:19:35.757
16

You have to use normal to execute normal mode commands, such a macro execution (@a) in command mode:

:bufdo normal @a
answered 2010-06-11T16:11:39.613
0

The only alternative I could come up with was to add the :w and :bn commands to the macro, so that it would automatically save and move to the next buffer. I could then run the command on the other 52 buffers by typing 52@a. This worked, but to me is a much different conceptual model than :bufdo, and I'm hoping someone else will point me in the direction of something more similar to :bufdo.

answered 2010-06-11T16:07:10.560

Your Answer