Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a macro in all buffers in vim

Tags:

vim

macros

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?

like image 603
Caleb Huitt - cjhuitt Avatar asked Jun 11 '10 16:06

Caleb Huitt - cjhuitt


People also ask

How do you repeat a macro in Vim?

To replay the macro once, move the cursor to the next line and press @h where h represents the register on which you saved the macro. Notice that the macro automatically moves the cursor to the next line as required. This allows you to repeat its execution. To repeat the macro execution, press @@.

How do I navigate between buffers in Vim?

Pressing Alt-F12 opens a window listing the buffers, and you can press Enter on a buffer name to go to that buffer. Or, press F12 (next) or Shift-F12 (previous) to cycle through the buffers.

How do I close all buffers in Vim?

Just put it to your . vim/plugin directory and then use :BufOnly command to close all buffers but the active one.


2 Answers

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.

like image 156
Dave Kirby Avatar answered Oct 05 '22 06:10

Dave Kirby


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

:bufdo normal @a 
like image 41
Christian C. Salvadó Avatar answered Oct 05 '22 06:10

Christian C. Salvadó