109

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.

1
  • 15@x not working on Windows gVIM Apr 28, 2013 at 4:55

5 Answers 5

119

Personally I would do

VG:normal @x

Edit: changed register to the one you specified.

4
  • 1
    Nice one. I like that this solution operates on only the selected area.
    – Kevin
    Aug 18, 2009 at 17:51
  • I mapped this to nnoremap <C-@> VG:norm@q<CR>
    – PAS
    Mar 4, 2018 at 1:49
  • I tried this with the macro "1z=]s" but it only spell-fixes the first word of each line, skipping the rest.
    – elig
    Mar 12, 2019 at 22:11
  • @elig you probably want to make a recursive macro that will find missing words on all lines.
    – raman
    Apr 4, 2019 at 18:06
77

You can do (in command mode)

:%normal @x
2
  • 2
    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. Jul 15, 2020 at 9:57
  • 2
    @Porunga :.,$norm @x will work (the .,$ means from current line . to last line $)
    – mattb
    Nov 15, 2021 at 2: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=""
6
  • 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, 2014 at 9:46
  • 4
    Avoid this if your macro involves a search, as it will continue from top. ctrl-c to stop if that happens! Nov 20, 2014 at 15:55
  • 5
    @IainBallard, :set nowrapscan makes it fail and stop when reaching the end of the file.
    – ericbn
    Aug 18, 2016 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 qs clear reg q, the second two qs start recording. See vim.wikia.com/wiki/Record_a_recursive_macro
    – dylnmc
    Jan 11, 2018 at 22:29
  • i think to clear the a register one could simple type qaq Jun 6, 2018 at 20:36
15

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

9
  • 8
    why not just %:normal @x or 1,$:normal @x? Jan 4, 2010 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 4, 2010 at 20:44
  • 2
    Won't 9999999@x run it many times on the last line unless you have exactly 999999 lines? Oct 29, 2010 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, 2011 at 18:16
  • 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. 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 9, 2013 at 18:54
-2

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)

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.