vimtricks.wiki Concise Vim tricks, one at a time.

How do I replay the last executed macro with a single key in Neovim?

Answer

Q

Explanation

In Neovim, the Q key is mapped to replay the last executed macro — the same as @@ in both Vim and Neovim. This makes it trivial to repeat a macro multiple times: just keep pressing Q. In classic Vim, Q instead enters Ex mode (a legacy mode), which is rarely used, so Neovim repurposed it for this far more ergonomic function.

How it works

  • Record a macro as usual: q{register}{...commands...}q
  • Play it once: @{register}
  • Repeat it any number of times: keep pressing Q

Q is equivalent to @@, which replays whichever macro was last run, regardless of which register it was stored in.

Example

" Record a macro in register q that adds a semicolon to the end of the line
qq A;<Esc> q

" Run it on the current line
@q

" Repeat on each subsequent line just by pressing Q
Q  " runs @q again
Q  " runs again
Q  " runs again

With a count prefix, 5Q runs the last macro 5 times in succession.

Tips

  • @@ is the Vim-compatible equivalent if you switch between Vim and Neovim
  • In Neovim, Q in visual mode runs the last macro on each selected line
  • If you need Vim's old Ex mode behavior, it is available as gQ in both Vim and Neovim
  • Neovim's default mappings can be seen with :help default-mappings

Next

How do I inspect a Vim register's full details including its type (charwise, linewise, or blockwise) in Vimscript?