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

How do I execute a macro while ignoring custom mappings that could change behavior?

Answer

:normal! @q

Explanation

Recorded macros can become fragile when your config defines mappings that shadow built-in keys. If a macro was recorded with raw keystrokes but later executed in an environment with remaps, behavior may diverge silently. :normal! is the reliable path: it executes keys in their unmapped form, so the macro behaves like it did when originally captured.

How it works

  • :normal executes Normal-mode keys from the command line
  • ! tells Vim to ignore user mappings during execution
  • @q replays the macro stored in register q
  • Combined as :normal! @q, you get deterministic replay even in heavily customized setups

Example

Assume you mapped j to move by display lines:

nnoremap j gj

A macro recorded earlier that depends on raw j can drift after this mapping change. Running it through:

:normal! @q

forces the built-in meaning of keys during replay, so the macro follows original movement semantics instead of your remap.

Tips

  • Use this inside scripted loops (:argdo, :cdo, :bufdo) when consistency matters
  • For per-line application over a range, pair with :[range]normal! @q
  • If your macro still misbehaves, inspect its literal contents with :reg q to catch accidental control keys

Next

How do I make @ characters count as part of filenames for gf and path motions?