Record and Replay Macros for Repetitive Edits
Automate repetitive text transformations by recording keystroke macros and replaying them across lines or files.
Prerequisites
- -Neovim or Vim installed
Steps
Record a macro
Start recording keystrokes into register 'a', perform the edit, then stop recording.
Use 'qq' to record into register 'q' for quick macros. The register letter is arbitrary.
Replay the macro once
Execute the macro stored in register 'a' on the current line.
Replay the macro on multiple lines
Run the macro 20 times or across a visual selection of lines.
Select lines visually and use :'<,'>normal @a to run the macro on each selected line.
View and edit a macro
Paste the macro contents into the buffer, edit them, and yank back to the register.
Paste with "ap, edit the keystrokes, then select and yank back with "ay to update the macro.
Run a macro across all matching lines
Use the :global command to execute a macro on every line matching a pattern.
Chain macros with recursive recording
Create a macro that calls itself to process until the end of the file.
The recursive macro stops when any command fails (like j at the end of file). Always clear the register first with qaq.
Full Script
FAQ
Discussion
Loading comments...