102

I want to run a macro I just recorded in register "x" on every single line of an open buffer, from my cursor to end of the buffer, in vim. How do I do that?

I know I can replay the macro n times:

15@x

...or just hold down @ until I reach the the last line, but I want to just hit a few keystrokes and be done with it.

starball
  • 20,030
  • 7
  • 43
  • 238
Kevin
  • 1,170
  • 2
  • 8
  • 10

5 Answers5

115

Personally I would do

VG:normal @x

Edit: changed register to the one you specified.

Randy Morris
  • 39,631
  • 8
  • 69
  • 76
75

You can do (in command mode)

:%normal @x
ryan_s
  • 7,826
  • 3
  • 27
  • 28
  • 1
    I like this solution, but il will execute the macro on every line of the buffer, OP asked only from his cursor to the end. – NIEL Alexandre Jul 15 '20 at 09:57
  • 1
    @Porunga `:.,$norm @x` will work (the `.,$` means from current line `.` to last line `$`) – mattb Nov 15 '21 at 02:24
27

make recursive macro:

qa@aq

ex:

qa0gUwj@aq

It'll UPCASE first word from current line to the end of file with single @a. But make sure "a register is empty:

let @a=""
Maxim Kim
  • 6,192
  • 2
  • 29
  • 28
  • 4
    Excellent. This approach is more useful than the `%:normal` approach as it honours any line movements within the macro, so is useful where the lines are not identical in form. I find it easier to create a macro 'a' to define the action and then have macro 'b' do the recursive bit, i.e. qb@a@bq. – Paul Ruane Sep 24 '14 at 09:46
  • 4
    Avoid this if your macro involves a search, as it will continue from top. `ctrl-c` to stop if that happens! – Iain Ballard Nov 20 '14 at 15:55
  • 5
    @IainBallard, `:set nowrapscan` makes it fail and stop when reaching the end of the file. – ericbn Aug 18 '16 at 21:51
  • 1
    to clear register a, simply do `qaq` before the macro: `qaqqa0gUwj@aq`. Additionally, if you forget to put the `@q` at the end, you can always do `qA` to start appending a register, and then do `@q`. I usually record to reg q for some reason because it's easy (esp for recursive macros) and usually end up doing `qqqqqgUiwj@qq` instead of two macros. The first three `q`s clear reg q, the second two `q`s start recording. See http://vim.wikia.com/wiki/Record_a_recursive_macro – dylnmc Jan 11 '18 at 22:29
  • i think to clear the `a` register one could simple type `qaq` – Peter Perháč Jun 06 '18 at 20:36
  • I agree that `:set nowrapscan` prevents an infinite loop over the file, since it makes `n` **fail** when the EOF is reached, but I have a curiosity: @MaximKim, what about the macro in your example? What does prevent the infinite loop? – Enlico Jul 26 '18 at 09:42
14

999999@x, unless you have a very large buffer...

Peter
  • 127,331
  • 53
  • 180
  • 211
  • 8
    why not just `%:normal @x` or `1,$:normal @x`? – Nathan Fellman Jan 04 '10 at 13:11
  • 3
    this is definitely the more elegant solution - I guess `99999@x` is just the natural thing you come up with when you're in a hurry :) – Peter Jan 04 '10 at 20:44
  • 2
    Won't 9999999@x run it many times on the last line unless you have exactly 999999 lines? – David Oneill Oct 29 '10 at 15:14
  • 3
    Just tried it. Hangs the session. Probably continues running it for a million times without stopping at the end of line. – CDR Nov 11 '11 at 18:16
  • Apparently if it reaches the end of the file it doesn't continue running the macro. However depending on what your macro does, this may have unintended consequences. The normal command seems to be the way to go. – deltaray Jan 30 '13 at 15:48
  • 1
    At least in some cases this won't behave as expected. Consider having a macro with **find next** / **find prev** in it. So, once the macro repeat reaches the end of file, it will continue from the beginning. [vim doc on the subject](http://vimdoc.sourceforge.net/htmldoc/usr_27.html#27.2). To prevent search wrapping, one might want to use **:set nowrapscan**. This is an example I could come up with, but that doesn't mean there are no more. **:%normal @x** is a way to go as @ryan_s suggested in his answer above. – Sergey M Oct 09 '13 at 18:54
  • this just wraps around for me - doesn't seem like a solution – baxx Apr 17 '15 at 18:01
  • This will fail in case my recording has search and replace pattern and the replaced pattern also contains the pattern to be searched – Chirag Parekh May 30 '17 at 12:41
  • thanks, first time i managed to hang vim :D – SanD Aug 28 '23 at 12:34
-1

From vim wiki, shortened version: qqqqq to record the macro @qq to finish recording @q to execute it (and will run till end of the file)

  • 1
    Please, May you add a reference to the documentation? – aturegano Oct 20 '16 at 09:46
  • 2
    @aturegano I think the author meant to use a recursive macro. See [The Vim Wiki: Record a Recursive Macro](http://vim.wikia.com/wiki/Record_a_recursive_macro) for more info. – dylnmc Jan 11 '18 at 22:30